Skip to content

Commit a768f1f

Browse files
committed
remove entries from clientInfo and roomInfo on disconnect
1 parent 0572210 commit a768f1f

2 files changed

Lines changed: 58 additions & 7 deletions

File tree

src/server/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { createGameReducer } from '../core/reducer';
1414
const PING_TIMEOUT = 20 * 1e3;
1515
const PING_INTERVAL = 10 * 1e3;
1616

17-
function Server({ games, db }) {
17+
function Server({ games, db, _clientInfo, _roomInfo }) {
1818
const app = new Koa();
1919
const io = new IO({
2020
ioOptions: {
@@ -29,8 +29,8 @@ function Server({ games, db }) {
2929
db = new InMemory();
3030
}
3131

32-
const clientInfo = new Map();
33-
const roomInfo = new Map();
32+
const clientInfo = _clientInfo || new Map();
33+
const roomInfo = _roomInfo || new Map();
3434

3535
for (const game of games) {
3636
const nsp = app._io.of(game.name);
@@ -71,7 +71,7 @@ function Server({ games, db }) {
7171
// Get clients connected to this current game.
7272
const roomClients = roomInfo.get(gameID);
7373
for (const client of roomClients.values()) {
74-
const playerID = clientInfo.get(client).playerID;
74+
const { playerID } = clientInfo.get(client);
7575
const newState = Object.assign({}, state, {
7676
G: game.playerView(state.G, state.ctx, playerID),
7777
});
@@ -116,7 +116,11 @@ function Server({ games, db }) {
116116
});
117117

118118
socket.on('disconnect', () => {
119-
clientInfo.delete(socket.id);
119+
if (clientInfo.has(socket.id)) {
120+
const { gameID } = clientInfo.get(socket.id);
121+
roomInfo.get(gameID).delete(socket.id);
122+
clientInfo.delete(socket.id);
123+
}
120124
});
121125
});
122126
}

src/server/index.test.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,56 @@ const game = Game({});
6161

6262
test('basic', () => {
6363
const server = Server({ games: [game] });
64-
const io = server.context.io;
6564
expect(server).not.toBe(undefined);
66-
io.socket.receive('disconnect');
65+
});
66+
67+
test('connect / disconnect', async () => {
68+
const toObj = m => {
69+
let o = {};
70+
m.forEach((value, key) => {
71+
o[key] = value;
72+
});
73+
return o;
74+
};
75+
76+
const _clientInfo = new Map();
77+
const _roomInfo = new Map();
78+
79+
const server = Server({ games: [game], _clientInfo, _roomInfo });
80+
const io = server.context.io;
81+
82+
io.socket.id = '0';
83+
await io.socket.receive('sync', 'gameID', '0', 2);
84+
io.socket.id = '1';
85+
await io.socket.receive('sync', 'gameID', '1', 2);
86+
87+
expect(toObj(_clientInfo)).toEqual({
88+
'0': { gameID: 'gameID', playerID: '0' },
89+
'1': { gameID: 'gameID', playerID: '1' },
90+
});
91+
expect(toObj(_roomInfo.get('gameID'))).toEqual({ '0': '0', '1': '1' });
92+
93+
// 0 disconnects.
94+
95+
io.socket.id = '0';
96+
await io.socket.receive('disconnect');
97+
98+
expect(toObj(_clientInfo)).toEqual({
99+
'1': { gameID: 'gameID', playerID: '1' },
100+
});
101+
expect(toObj(_roomInfo.get('gameID'))).toEqual({ '1': '1' });
102+
103+
// unknown player disconnects.
104+
105+
io.socket.id = 'unknown';
106+
await io.socket.receive('disconnect');
107+
108+
// 1 disconnects.
109+
110+
io.socket.id = '1';
111+
await io.socket.receive('disconnect');
112+
expect(toObj(_clientInfo)).toEqual({});
113+
expect(toObj(_roomInfo.get('gameID'))).toEqual({});
67114
});
68115

69116
test('sync', async () => {

0 commit comments

Comments
 (0)