www~ 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env node
  2. /**
  3. * Module dependencies.
  4. */
  5. var app = require('../app');
  6. var debug = require('debug')('localhost:server');
  7. var http = require('http');
  8. /**
  9. * Get port from environment and store in Express.
  10. */
  11. var port = normalizePort(process.env.PORT || '4000');
  12. app.set('port', port);
  13. /**
  14. * Create HTTP server.
  15. */
  16. var server = http.createServer(app);
  17. var io = require('socket.io')(server);
  18. /**
  19. * Listen on provided port, on all network interfaces.
  20. */
  21. server.listen(port);
  22. server.on('error', onError);
  23. server.on('listening', onListening);
  24. /**
  25. * Normalize a port into a number, string, or false.
  26. */
  27. function normalizePort(val) {
  28. var port = parseInt(val, 10);
  29. if (isNaN(port)) {
  30. // named pipe
  31. return val;
  32. }
  33. if (port >= 0) {
  34. // port number
  35. return port;
  36. }
  37. return false;
  38. }
  39. /**
  40. * Event listener for HTTP server "error" event.
  41. */
  42. function onError(error) {
  43. if (error.syscall !== 'listen') {
  44. throw error;
  45. }
  46. var bind = typeof port === 'string'
  47. ? 'Pipe ' + port
  48. : 'Port ' + port;
  49. // handle specific listen errors with friendly messages
  50. switch (error.code) {
  51. case 'EACCES':
  52. console.error(bind + ' requires elevated privileges');
  53. process.exit(1);
  54. break;
  55. case 'EADDRINUSE':
  56. console.error(bind + ' is already in use');
  57. process.exit(1);
  58. break;
  59. default:
  60. throw error;
  61. }
  62. }
  63. /**
  64. * Event listener for HTTP server "listening" event.
  65. */
  66. function onListening() {
  67. var addr = server.address();
  68. var bind = typeof addr === 'string'
  69. ? 'pipe ' + addr
  70. : 'port ' + addr.port;
  71. debug('Listening on ' + bind);
  72. }
  73. io.on('connection', client=>{
  74. client.on('updateLineChart', dataLineChart=>{
  75. io.emit('onUpdateLineChart', dataLineChart);
  76. });
  77. });