server.js 826 B

1234567891011121314151617181920212223242526272829
  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' );