Skip to content

Commit

Permalink
remove entries from clientInfo and roomInfo on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
darthfiddler committed Feb 11, 2018
1 parent 0572210 commit a768f1f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createGameReducer } from '../core/reducer';
const PING_TIMEOUT = 20 * 1e3;
const PING_INTERVAL = 10 * 1e3;

function Server({ games, db }) {
function Server({ games, db, _clientInfo, _roomInfo }) {
const app = new Koa();
const io = new IO({
ioOptions: {
Expand All @@ -29,8 +29,8 @@ function Server({ games, db }) {
db = new InMemory();
}

const clientInfo = new Map();
const roomInfo = new Map();
const clientInfo = _clientInfo || new Map();
const roomInfo = _roomInfo || new Map();

for (const game of games) {
const nsp = app._io.of(game.name);
Expand Down Expand Up @@ -71,7 +71,7 @@ function Server({ games, db }) {
// Get clients connected to this current game.
const roomClients = roomInfo.get(gameID);
for (const client of roomClients.values()) {
const playerID = clientInfo.get(client).playerID;
const { playerID } = clientInfo.get(client);
const newState = Object.assign({}, state, {
G: game.playerView(state.G, state.ctx, playerID),
});
Expand Down Expand Up @@ -116,7 +116,11 @@ function Server({ games, db }) {
});

socket.on('disconnect', () => {
clientInfo.delete(socket.id);
if (clientInfo.has(socket.id)) {
const { gameID } = clientInfo.get(socket.id);
roomInfo.get(gameID).delete(socket.id);
clientInfo.delete(socket.id);
}
});
});
}
Expand Down
51 changes: 49 additions & 2 deletions src/server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,56 @@ const game = Game({});

test('basic', () => {
const server = Server({ games: [game] });
const io = server.context.io;
expect(server).not.toBe(undefined);
io.socket.receive('disconnect');
});

test('connect / disconnect', async () => {
const toObj = m => {
let o = {};
m.forEach((value, key) => {
o[key] = value;
});
return o;
};

const _clientInfo = new Map();
const _roomInfo = new Map();

const server = Server({ games: [game], _clientInfo, _roomInfo });
const io = server.context.io;

io.socket.id = '0';
await io.socket.receive('sync', 'gameID', '0', 2);
io.socket.id = '1';
await io.socket.receive('sync', 'gameID', '1', 2);

expect(toObj(_clientInfo)).toEqual({
'0': { gameID: 'gameID', playerID: '0' },
'1': { gameID: 'gameID', playerID: '1' },
});
expect(toObj(_roomInfo.get('gameID'))).toEqual({ '0': '0', '1': '1' });

// 0 disconnects.

io.socket.id = '0';
await io.socket.receive('disconnect');

expect(toObj(_clientInfo)).toEqual({
'1': { gameID: 'gameID', playerID: '1' },
});
expect(toObj(_roomInfo.get('gameID'))).toEqual({ '1': '1' });

// unknown player disconnects.

io.socket.id = 'unknown';
await io.socket.receive('disconnect');

// 1 disconnects.

io.socket.id = '1';
await io.socket.receive('disconnect');
expect(toObj(_clientInfo)).toEqual({});
expect(toObj(_roomInfo.get('gameID'))).toEqual({});
});

test('sync', async () => {
Expand Down

0 comments on commit a768f1f

Please sign in to comment.