Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. var Hover = require('hover-nodejs');
  2. var WebSocket = require('ws');
  3.  
  4. var hover = new Hover(0x42, 23, 24, 1);
  5.  
  6. //hover.debug = true;
  7.  
  8. hover.init().then(function() {
  9. // Ready to listen or do anything else
  10. console.log('Hover ready');
  11.  
  12. hover.listen(handleHoverEvent, 1);
  13.  
  14. }, function(error) {
  15. console.error('Could not init hover board');
  16. console.error(error);
  17. });
  18.  
  19. var handleHoverEvent = function(event) {
  20. console.log(event);
  21.  
  22. connections.forEach(function(c) {
  23. if(c.readyState === WebSocket.OPEN) {
  24. c.send(JSON.stringify({
  25. type: 'event',
  26. action: 'move',
  27. data: {
  28. direction: event
  29. }
  30. }));
  31. }
  32. });
  33.  
  34. };
  35.  
  36. // Web Sockets
  37.  
  38. var connections = [];
  39.  
  40. const wss = new WebSocket.Server({
  41. perMessageDeflate: false,
  42. port: 8000
  43. });
  44.  
  45. wss.on('connection', function connection(ws) {
  46. console.log('Device Connected');
  47. connections.push(ws);
  48. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement