humus-old/server/index.js
2019-04-25 00:09:28 -05:00

45 lines
899 B
JavaScript

// 3rd party.
import http from 'http';
// 2nd party.
import {SocketServer} from '@avocado/net/server/socket';
// Import directly for HMR hierarchy.
import 'register-traits';
// Start game server.
import Game from './game';
const socketServer = new SocketServer();
let game;
let httpServer;
function createGame() {
game = new 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',
'register-traits'
], () => {
destroyGame(() => {
createGame();
});
});
}