new WebsocketRouter(websocket, routerId)
Create a WebsocketRouter.
Parameters:
Name | Type | Description |
---|---|---|
websocket |
object | The websocket server that the router will be attahced to. |
routerId |
string | The id of the router 'usually its name'. Had to be known by the client user to be able to sucessfully connect. |
Methods
-
disconnect(error)
-
Forcefully disconnects all connected users (they got a notification).
Parameters:
Name Type Description error
Error The error that describes the reason for disconnect.
-
getRoomId()
-
Returns the websocket room id.
Returns:
The full id of the websocket room that is used to separate users of this router.
- Type
- string
-
getRouterId()
-
Returns the websocket router id.
Returns:
The router id of the router.
- Type
- string
-
onConnect(handleFn)
-
Sets the event handling function of user connect.
Parameters:
Name Type Description handleFn
function The function that needs to be called when a user connects. The function should handle two arguments. The first will be a WebsocketRouterUser object, while the second is a callback (highlighting with an error object if we accept the connection).
-
send(payload)
-
Broadcasts a message to all connected users.
Parameters:
Name Type Description payload
object The content object of the message (has to be stringifiable!).