43 lines
866 B
JavaScript
43 lines
866 B
JavaScript
import http from 'http';
|
|
import {Server} from '@avocado/server/socket';
|
|
// Import directly for HMR hierarchy.
|
|
import '../register-traits';
|
|
// Start game server.
|
|
import Game from './game';
|
|
const avocadoServer = new Server();
|
|
|
|
let game;
|
|
let httpServer;
|
|
|
|
function createGame() {
|
|
game = new Game();
|
|
// Listen for connections.
|
|
httpServer = http.createServer();
|
|
httpServer.listen(8420, '0.0.0.0');
|
|
avocadoServer.on('connect', game.acceptConnection, game);
|
|
avocadoServer.open(httpServer);
|
|
}
|
|
|
|
function destroyGame(fn) {
|
|
// Stop listening.
|
|
avocadoServer.off('connect', game.acceptConnection);
|
|
avocadoServer.close(() => {
|
|
httpServer.close(() => {
|
|
game.destroy(fn);
|
|
})
|
|
});
|
|
}
|
|
|
|
createGame();
|
|
|
|
if (module.hot) {
|
|
module.hot.accept([
|
|
'./game',
|
|
'../register-traits'
|
|
], () => {
|
|
destroyGame(() => {
|
|
createGame();
|
|
});
|
|
});
|
|
}
|