www 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env node
  2. /**
  3. * Module dependencies.
  4. */
  5. const http = require('http');
  6. const SSE = require('sse');
  7. const url = require('url');
  8. const sseClient = require('../../models/sse_client');
  9. const order = require('../../db/models/order');
  10. const debug = require('debug')('shuiduoduo.server:server');
  11. require('../../utils/logger');
  12. const app = require('../app');
  13. const config = require('../config');
  14. const { clear } = require('console');
  15. /**
  16. * Get port from environment and store in Express.
  17. */
  18. const port = normalizePort(process.env.PORT || config.port || '3000');
  19. app.set('port', port);
  20. console.log(port);
  21. /**
  22. * Create HTTP server.
  23. */
  24. const server = http.createServer(app);
  25. /**
  26. * Listen on provided port, on all network interfaces.
  27. */
  28. server.listen(port, () => {
  29. const sse = new SSE(server);
  30. sse.on('connection', async client => {
  31. const req = url.parse(client.req.url, true);
  32. client.id = req.query.oid;
  33. // client.sendjson = data => {
  34. // client.send(JSON.stringify(data));
  35. // };
  36. const odata = await order.findOne({ where: { id: client.id } });
  37. if (!odata) {
  38. return client.close();
  39. }
  40. if (odata.status === config.ORDER_STATUS.已支付) {
  41. client.send('done');
  42. return client.close();
  43. }
  44. if (sseClient[client.id]) {
  45. sseClient[client.id].close();
  46. }
  47. sseClient[client.id] = client;
  48. client.on('close', () => {
  49. // clearTimeout(client.tick);
  50. delete sseClient[client.id];
  51. });
  52. client.tick = setTimeout(() => {
  53. if (sseClient[client.id]) {
  54. sseClient[client.id].close();
  55. }
  56. }, 1000 * 60 * 10);
  57. });
  58. });
  59. server.on('error', onError);
  60. server.on('listening', onListening);
  61. /**
  62. * Normalize a port into a number, string, or false.
  63. */
  64. function normalizePort(val) {
  65. const port = parseInt(val, 10);
  66. if (Number.isNaN(port)) {
  67. // named pipe
  68. return val;
  69. }
  70. if (port >= 0) {
  71. // port number
  72. return port;
  73. }
  74. return false;
  75. }
  76. /**
  77. * Event listener for HTTP server "error" event.
  78. */
  79. function onError(error) {
  80. if (error.syscall !== 'listen') {
  81. throw error;
  82. }
  83. const bind = typeof port === 'string' ? `Pipe ${port}` : `Port ${port}`;
  84. // handle specific listen errors with friendly messages
  85. switch (error.code) {
  86. case 'EACCES':
  87. console.error(`${bind} requires elevated privileges`);
  88. process.exit(1);
  89. break;
  90. case 'EADDRINUSE':
  91. console.error(`${bind} is already in use`);
  92. process.exit(1);
  93. break;
  94. default:
  95. throw error;
  96. }
  97. }
  98. /**
  99. * Event listener for HTTP server "listening" event.
  100. */
  101. function onListening() {
  102. const addr = server.address();
  103. const bind = typeof addr === 'string' ? `pipe ${addr}` : `port ${addr.port}`;
  104. console.log(`Listening on ${bind}`);
  105. }