From d98add598d594c24207d99ab07e3050dfa9e360e Mon Sep 17 00:00:00 2001 From: Federico Zacayan Date: Sun, 27 Mar 2022 12:57:12 -0300 Subject: [PATCH] Allowing to cast event to the rest of users Allowing to access to realm to share the events to the other connected users, let then know when a user connect or diconnect. /*Implementation*/ peerServer.on('connection', (client, realm) => { if (!realm) return realm.getClientsIds().filter(a => a != client.id).forEach(peerId => { const message = { type: 'CONNECTED', peerId: client.id } realm.getClientById(peerId).getSocket().send(JSON.stringify(message)) }) }); peerServer.on('disconnect', (client, realm) => { if (!realm) return realm.getClientsIds().filter(a => a != client.id).forEach(peerId => { const message = { type: 'DISCONNECTED', peerId: client.id } realm.getClientById(peerId).getSocket().send(JSON.stringify(message)) }) }); --- src/instance.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/instance.ts b/src/instance.ts index 0b8f522d1..e51decd69 100644 --- a/src/instance.ts +++ b/src/instance.ts @@ -42,7 +42,7 @@ export const createInstance = ({ app, server, options }: { config: customConfig }); - wss.on("connection", (client: IClient) => { + wss.on("connection", (client: IClient, realm: IRealm) => { const messageQueue = realm.getMessageQueueById(client.getId()); if (messageQueue) { @@ -62,7 +62,7 @@ export const createInstance = ({ app, server, options }: { messageHandler.handle(client, message); }); - wss.on("close", (client: IClient) => { + wss.on("close", (client: IClient, realm: IRealm) => { app.emit("disconnect", client); }); @@ -72,4 +72,4 @@ export const createInstance = ({ app, server, options }: { messagesExpire.startMessagesExpiration(); checkBrokenConnections.start(); -}; \ No newline at end of file +};