diff --git a/.changeset/cool-papayas-flow.md b/.changeset/cool-papayas-flow.md new file mode 100644 index 00000000..4254f52c --- /dev/null +++ b/.changeset/cool-papayas-flow.md @@ -0,0 +1,5 @@ +--- +"@pluv/io": patch +--- + +Added the ability to evict (i.e. close) a connection to a room via `IORoom.evict` and `IORoom.evictAll`. diff --git a/packages/io/src/IORoom.ts b/packages/io/src/IORoom.ts index 6011c74c..30afe2d1 100644 --- a/packages/io/src/IORoom.ts +++ b/packages/io/src/IORoom.ts @@ -191,6 +191,32 @@ export class IORoom< this._uninitialize = uninitialize; } + /** + * @description Closes a connection with the specified sessionId + * @param sessionId The session id of the connection to close + */ + public async evict(sessionId: string): Promise { + const session = this._sessions.get(sessionId); + + if (session) session.state.quit = true; + + await this._emitQuitters(); + } + + /** + * @description Closes all connections to this room, effectively destroying + * the room + */ + public async evictAll(): Promise { + const sessions = Array.from(this._sessions.values()); + + sessions.forEach((session) => { + session.state.quit = true; + }); + + await this._emitQuitters(); + } + public getSize(): number { const currentTime = new Date().getTime();