refactor: tidy
This commit is contained in:
parent
e9da3871a4
commit
c1758ef3d6
|
@ -19,16 +19,11 @@ function onUpgrade(request, socket, head) {
|
|||
}
|
||||
}
|
||||
|
||||
export default async function listen(server) {
|
||||
server.on('upgrade', onUpgrade);
|
||||
let engine;
|
||||
let onConnect;
|
||||
|
||||
class SocketServer extends Server {
|
||||
transmit(ws, packed) { ws.send(packed); }
|
||||
}
|
||||
|
||||
let onConnect;
|
||||
function makeOnConnect(engine) {
|
||||
return async (ws) => {
|
||||
function createOnConnect(engine) {
|
||||
onConnect = async (ws) => {
|
||||
ws.on('close', () => {
|
||||
engine.disconnectPlayer(ws);
|
||||
})
|
||||
|
@ -37,27 +32,40 @@ export default async function listen(server) {
|
|||
});
|
||||
await engine.connectPlayer(ws);
|
||||
};
|
||||
}
|
||||
wss.on('connection', onConnect);
|
||||
}
|
||||
|
||||
let engine;
|
||||
async function makeEngine(Engine) {
|
||||
const engine = new Engine(SocketServer);
|
||||
class SocketServer extends Server {
|
||||
transmit(ws, packed) { ws.send(packed); }
|
||||
}
|
||||
|
||||
async function createEngine(Engine) {
|
||||
engine = new Engine(SocketServer);
|
||||
await engine.load();
|
||||
engine.start();
|
||||
return engine;
|
||||
}
|
||||
}
|
||||
|
||||
engine = await makeEngine(Engine);
|
||||
wss.on('connection', onConnect = makeOnConnect(engine));
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.accept('./engine/engine.js', async ({default: Engine}) => {
|
||||
async function remakeServer(Engine) {
|
||||
if (onConnect) {
|
||||
wss.off('connection', onConnect);
|
||||
}
|
||||
if (engine) {
|
||||
for (const [connection] of engine.connectedPlayers) {
|
||||
connection.close();
|
||||
}
|
||||
engine = await makeEngine(Engine);
|
||||
wss.on('connection', onConnect = makeOnConnect(engine));
|
||||
});
|
||||
}
|
||||
createOnConnect(await createEngine(Engine));
|
||||
}
|
||||
|
||||
await remakeServer(Engine);
|
||||
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.accept('./engine/engine.js', async ({default: Engine}) => {
|
||||
await remakeServer(Engine);
|
||||
});
|
||||
}
|
||||
|
||||
export default async function listen(server) {
|
||||
server.on('upgrade', onUpgrade);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user