From 0a13d92209cb4eb3e64c7b4ea0907b9381b41d4d Mon Sep 17 00:00:00 2001 From: xangelo Date: Tue, 7 Jun 2022 00:07:52 -0400 Subject: [PATCH] Retrieve a socket by their token/account_id --- src/lib/server.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib/server.ts b/src/lib/server.ts index 6615487..90ccf48 100644 --- a/src/lib/server.ts +++ b/src/lib/server.ts @@ -5,8 +5,7 @@ import { merge } from 'lodash'; import bodyParser from 'body-parser'; import { ExpressAdapter } from '@bull-board/express'; import http from 'http'; -import { Server } from 'socket.io'; -import {HttpError} from '../errors'; +import { Server, Socket } from 'socket.io'; type AuthInfo = { authInfo: { @@ -59,6 +58,19 @@ export class HttpServer { } } + getSocketFromAuthenticatedUser(authInfo: {token: string, accountId: string}): Socket | null { + let socket: Socket; + + this.ws.of('/').sockets.forEach(s => { + const auth = this.authFromUrl(s.request.headers['referer']); + if (auth.authInfo.accountId === authInfo.accountId && auth.authInfo.token === authInfo.token) { + socket = s; + } + }); + + return socket; + } + wrap(handler: HttpHandler, hxEvents: string) { const self = this; return async function (req: Request, res: Response) { -- 2.25.1