| 1234567891011121314151617181920212223242526272829303132333435363738 |
- const app = require('./app')
- const fs = require('fs')
- require.extensions['.nes'] = function(module, filename) {
- module.exports = fs.readFileSync(filename, 'binary')
- }
- const SMB = require('../common/smb')
- game = new SMB()
- console.log(game.start.toString())
- game.start()
- app.ws('/game', (ws, req) => {
- const send = (frame) => {
- try {
- ws.send(JSON.stringify(game.gameState()))
- ws.send(frame)
- } catch(err) {
- console.warn(err.message)
- teardown()
- }
- }
- const frameSub = game.frames.subscribe(send)
- const teardown = () => {
- console.log('Detaching')
- frameSub.unsubscribe()
- }
- ws.on('message', (msg) => {
- const data = JSON.parse(msg)
- game.command(data)
- })
- ws.on('close', teardown)
- })
- app.listen().catch((err) => {
- console.log(err.toString())
- process.exit(1)
- })
|