diff --git a/common/GameCommon.js b/common/GameCommon.js index 387f9db..d18bd6a 100644 --- a/common/GameCommon.js +++ b/common/GameCommon.js @@ -11,6 +11,10 @@ function setGame(gameId, game) { GAMES[gameId] = game } +function playerExists(gameId, playerId) { + return !!GAMES[gameId].players[playerId] +} + function addPlayer(gameId, playerId) { const ts = Util.timestamp() if (!GAMES[gameId].players[playerId]) { @@ -27,17 +31,22 @@ function addPlayer(gameId, playerId) { } else { changePlayer(gameId, playerId, { ts }) } - GAMES[gameId].evtInfos[playerId] = { - _last_mouse: null, - _last_mouse_down: null, + if (!GAMES[gameId].evtInfos[playerId]) { + GAMES[gameId].evtInfos[playerId] = { + _last_mouse: null, + _last_mouse_down: null, + } } } -function addSocket(gameId, socket) { - const sockets = GAMES[gameId].sockets +function socketExists(gameId, socket) { + return GAMES[gameId].sockets.includes(socket) +} - if (!sockets.includes(socket)) { - sockets.push(socket) +function addSocket(gameId, socket) { + if (!GAMES[gameId].sockets.includes(socket)) { + console.log('adding socket: ', gameId, socket.protocol) + GAMES[gameId].sockets.push(socket) } } @@ -447,7 +456,9 @@ function handleInput(gameId, playerId, input) { export default { setGame, exists, + playerExists, addPlayer, + socketExists, addSocket, removeSocket, get, diff --git a/game/WsWrapper.js b/game/WsWrapper.js index ca5edb1..add6722 100644 --- a/game/WsWrapper.js +++ b/game/WsWrapper.js @@ -46,6 +46,11 @@ export default class WsWrapper { ws.onmessage = (e) => { this.onmessage(e) } + ws.onerror = (e) => { + this.handle = null + this.reconnectTimeout = setTimeout(() => { this.connect() }, 1000) + this.onclose(e) + } ws.onclose = (e) => { this.handle = null this.reconnectTimeout = setTimeout(() => { this.connect() }, 1000) diff --git a/server/Game.js b/server/Game.js index 43caad3..3256b3f 100644 --- a/server/Game.js +++ b/server/Game.js @@ -45,7 +45,9 @@ export default { createGame, exists: GameCommon.exists, addPlayer: GameCommon.addPlayer, + playerExists: GameCommon.playerExists, addSocket: GameCommon.addSocket, + socketExists: GameCommon.socketExists, removeSocket: GameCommon.removeSocket, get: GameCommon.get, getSockets: GameCommon.getSockets, diff --git a/server/WebSocketServer.js b/server/WebSocketServer.js index 5d79c97..96d93d9 100644 --- a/server/WebSocketServer.js +++ b/server/WebSocketServer.js @@ -50,43 +50,19 @@ class WebSocketServer { return } - socket.isAlive = true - socket.on('pong', function () { - this.isAlive = true; - }) - socket.on('message', (data) => { - console.log(`ws| `, data) + console.log(`ws`, socket.protocol, data) this.evt.dispatch('message', {socket, data}) }) }) - this._interval = setInterval(() => { - this._websocketserver.clients.forEach((socket) => { - if (socket.isAlive === false) { - return socket.terminate() - } - socket.isAlive = false - this.evt.dispatch('close', {socket}) - socket.ping(() => { }) - }) - }, 30000) - this._websocketserver.on('close', () => { clearInterval(this._interval) }) } notifyOne(data, socket) { - if (socket.isAlive) { - socket.send(JSON.stringify(data)) - } - } - - notifyAll(data) { - this._websocketserver.clients.forEach((socket) => { - this.notifyOne(data, socket) - }) + socket.send(JSON.stringify(data)) } } diff --git a/server/index.js b/server/index.js index b053847..9be1bfe 100644 --- a/server/index.js +++ b/server/index.js @@ -109,6 +109,30 @@ wss.on('close', async ({socket}) => { } }) +const ensurePlayerGame = (gameId, clientId, socket) => { + Game.addPlayer(gameId, clientId) + Game.addSocket(gameId, socket) + Game.store(gameId) + const game = Game.get(gameId) + notify( + [Protocol.EV_SERVER_INIT, { + puzzle: game.puzzle, + players: game.players, + evtInfos: game.evtInfos, + }], + [socket] + ) +} + +const handlePlayerInput = (gameId, clientId, clientSeq, clientEvtData) => { + const changes = Game.handleInput(gameId, clientId, clientEvtData) + Game.store(gameId) + notify( + [Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes], + Game.getSockets(gameId) + ) +} + wss.on('message', async ({socket, data}) => { try { const proto = socket.protocol.split('|') @@ -121,24 +145,14 @@ wss.on('message', async ({socket, data}) => { if (!Game.exists(gameId)) { throw `[game ${gameId} does not exist... ]` } - Game.addPlayer(gameId, clientId) - Game.addSocket(gameId, socket) - Game.store(gameId) - notify( - [Protocol.EV_SERVER_INIT, Game.get(gameId)], - [socket] - ) + ensurePlayerGame(gameId, clientId, socket) } break; case Protocol.EV_CLIENT_EVENT: { const clientSeq = msg[1] const clientEvtData = msg[2] - const changes = Game.handleInput(gameId, clientId, clientEvtData) - Game.store(gameId) - notify( - [Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes], - Game.getSockets(gameId) - ) + ensurePlayerGame(gameId, clientId, socket) + handlePlayerInput(gameId, clientId, clientSeq, clientEvtData) } break; } } catch (e) {