-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- own module - also clean up correctly when user got "kicked"
- Loading branch information
1 parent
81c4a2f
commit 29f8575
Showing
3 changed files
with
128 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import getLogger from './getLogger'; | ||
|
||
const LOGGER = getLogger('socketRegistry'); | ||
|
||
LOGGER.transports[0].level = 'debug'; | ||
|
||
/** | ||
* we need do keep the mapping of a socket to userId, | ||
* so that we can retrieve the userId for any message (command) sent on this socket. | ||
* | ||
* we need do keep the mapping of a socket to room, | ||
* so that we can produce "user left" events on socket disconnect. | ||
* | ||
* maps socket IDs to userIds and roomIds | ||
*/ | ||
export default function socketRegistryFactory() { | ||
const registry = {}; | ||
|
||
return { | ||
registerSocketMapping, | ||
removeSocketMapping, | ||
isLastSocketForUserId, | ||
getMapping | ||
}; | ||
|
||
function getMapping(socketId) { | ||
return registry[socketId]; | ||
} | ||
|
||
function registerSocketMapping(socket, userId, roomId) { | ||
LOGGER.debug(`Registering socket ${socket.id} : user ${userId} and room ${roomId}`); | ||
|
||
if (registry[socket.id]) { | ||
LOGGER.warn(`Overriding old mapping for socket ${socket.id}`); | ||
} | ||
registry[socket.id] = { | ||
userId, | ||
roomId | ||
}; | ||
|
||
// also join sockets together in a socket.io "room" , so that we can emit messages to all sockets in that room | ||
socket.join(roomId); | ||
} | ||
|
||
function removeSocketMapping(socketId, userId, roomId) { | ||
LOGGER.debug(`Removing mapping: socket ${socketId} -> [user ${userId}, room ${roomId}]`); | ||
|
||
if (registry[socketId].userId !== userId) { | ||
LOGGER.warn( | ||
`socket to userId mapping mismatch: socket ${socketId} maps to user ${registry[socketId].userId}. expected user ${userId}` | ||
); | ||
} | ||
if (registry[socketId].roomId !== roomId) { | ||
LOGGER.warn( | ||
`socket to roomId mapping mismatch: socket ${socketId} maps to room ${registry[socketId].roomId}. expected room ${roomId}` | ||
); | ||
} | ||
|
||
delete registry[socketId]; | ||
} | ||
|
||
function isLastSocketForUserId(userId) { | ||
const socketsOfThatUser = Object.entries(registry).filter( | ||
(entry) => entry[1].userId === userId | ||
); | ||
if (socketsOfThatUser.length > 1) { | ||
LOGGER.debug( | ||
`User ${userId} has multiple open sockets: ${regEntriesToString(socketsOfThatUser)}` | ||
); | ||
} | ||
return socketsOfThatUser.length === 1; | ||
} | ||
|
||
function regEntriesToString(regEntries) { | ||
return regEntries | ||
.map((re) => `${re[0]} -> [user ${re[1].userId}, room ${re[1].roomId}]`) | ||
.join(' '); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters