humus-old/server/index.js
2019-11-22 02:15:36 -06:00

53 lines
1.0 KiB
JavaScript

// 3rd party.
import http from 'http';
// 2nd party.
import {SocketServer} from '@avocado/net/server/socket';
// Create fixtures.
import './create-fixtures';
// Start game server.
import Game from './game';
const socketServer = new SocketServer();
let game;
let httpServer;
import repl from 'repl';
const instance = repl.start({
prompt: 'humus> ',
input: process.stdin,
output: process.stdout
});
function createGame() {
game = new Game();
instance.context.game = game;
// Listen for connections.
httpServer = http.createServer();
httpServer.listen(8420, '0.0.0.0');
socketServer.on('connect', game.acceptConnection, game);
socketServer.open(httpServer);
}
function destroyGame(fn) {
// Stop listening.
socketServer.off('connect', game.acceptConnection);
socketServer.close(() => {
httpServer.close(() => {
game.destroy(fn);
})
});
}
createGame();
if (module.hot) {
module.hot.accept([
'./game',
], () => {
destroyGame(() => {
createGame();
});
});
}