Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
GameServer.ts
Changed import "import socketio from 'socket.io' to import type { Socket as IOSocket } from 'socket.io';
import { Server as IOServer } from 'socket.io'; This lets us have more possibilities when extending base types for Socket and Server.
extended server and socket to type <ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData> reference document
Added interface SocketData which will be used as additional data for a socket. Currently it only holds manualDisconnect but might be useful later on for more custom data.
Changed type socketio.Socket throughout the file to IOSocket<any, any, any, SocketData> since we now have an extended Socket type.
Added a manualDisconnect listener to the socket that sets the manualDisconnect parameter in the socket.data. This will tell us if a user wilfully wishes to leave a game
Removed throw new Error because players can come to this state when they wish to reconnect to a game that they left but the game has removed them from its mapping (they took longer then 20s).
rewrote onSocketDisconnected function to check if the players socket has the manualDisconnect property set to true if it is set to true we remove the player from the userlobbymap and game. We cleanup the lobby if there are no users left.
Added a new timeout for all other types of disconnect which is 20s.