You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
826 B

4 years ago
  1. var koa = require('koa');
  2. var sockjs = require('sockjs');
  3. var http = require('http');
  4. var fs = require('fs');
  5. var path = require('path');
  6. // 1. Echo sockjs server
  7. var sockjs_opts = {sockjs_url: "http://cdn.jsdelivr.net/sockjs/1.0.1/sockjs.min.js"};
  8. var sockjs_echo = sockjs.createServer(sockjs_opts);
  9. sockjs_echo.on('connection', function(conn) {
  10. conn.on('data', function(message) {
  11. conn.write(message);
  12. });
  13. });
  14. // 2. koa server
  15. var app = koa();
  16. app.use(function *() {
  17. var filePath = __dirname + '/index.html';
  18. this.type = path.extname(filePath);
  19. this.body = fs.createReadStream(filePath);
  20. });
  21. var server = http.createServer(app.callback());
  22. sockjs_echo.installHandlers(server, {prefix:'/echo'});
  23. server.listen(9999, '0.0.0.0');
  24. console.log(' [*] Listening on 0.0.0.0:9999' );