server.js 483 B

123456789101112131415161718192021222324252627
  1. const app = require('./app')
  2. const Game = require('./game')
  3. game = new Game()
  4. game.start()
  5. app.ws('/game', (ws, req) => {
  6. const send = (frame) => {
  7. try {
  8. ws.send(frame)
  9. } catch(err) {
  10. console.warn(err)
  11. teardown()
  12. }
  13. }
  14. const teardown = () => {
  15. console.log('Detaching')
  16. game.off('frame', send)
  17. }
  18. game.on('frame', send)
  19. ws.on('close', () => teardown)
  20. })
  21. app.listen().catch((err) => {
  22. console.log(err.toString())
  23. process.exit(1)
  24. })