From: xangelo Date: Tue, 7 Jun 2022 04:07:52 +0000 (-0400) Subject: Retrieve a socket by their token/account_id X-Git-Url: https://git.xangelo.ca/?p=browser-rts.git;a=commitdiff_plain;h=0a13d92209cb4eb3e64c7b4ea0907b9381b41d4d Retrieve a socket by their token/account_id --- 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) {