diff --git a/src/rtmt/rtmt.ts b/src/rtmt/rtmt.ts index aa8ccdf..238cad0 100644 --- a/src/rtmt/rtmt.ts +++ b/src/rtmt/rtmt.ts @@ -1,8 +1,10 @@ export type Bytes = Buffer export type OnMessage = (clientID: string, message: Object) => any +export type OnClientConnectionChange = (clientID: string, isOnline : boolean) => any export interface RTMT { sendMessage: (clientID: string, channel: string, message: Object) => any; listenMessage: (channel: string, callback: OnMessage) => any; isClientOnline(clientID: string): boolean; + listenOnClientConnectionChange(onUserConnectionChange: OnClientConnectionChange): void; } \ No newline at end of file diff --git a/src/rtmt/rtmt_websocket.ts b/src/rtmt/rtmt_websocket.ts index 45d8529..5f63494 100644 --- a/src/rtmt/rtmt_websocket.ts +++ b/src/rtmt/rtmt_websocket.ts @@ -1,5 +1,5 @@ import { decode, encode } from "./encode_decode_message" -import { Bytes, OnMessage, RTMT } from "./rtmt" +import { Bytes, OnClientConnectionChange, OnMessage, RTMT } from "./rtmt" import WebSocket from "ws" import * as http from 'http'; @@ -17,6 +17,8 @@ export class RTMTWS implements RTMT { private pingInterval?: NodeJS.Timeout = undefined; + onUserConnectionChange?: OnClientConnectionChange; + constructor() { this.messageChannels = new Map() this.wsServer = null @@ -32,6 +34,7 @@ export class RTMTWS implements RTMT { const regexResult = req.url.split(RegExp("\/\?userKey=")); const clientID = regexResult[1] this.clients.set(clientID, ws) + this.onUserConnectionChange?.(clientID, true); ws.on("message", (messageBytes: string) => { this.onWebSocketMessage(clientID, messageBytes) }) @@ -39,6 +42,7 @@ export class RTMTWS implements RTMT { ws.on("close", (code: number, reason: string) => { console.log("WS Closed! code : " + code + " reason : " + reason); this.clients.delete(clientID) + this.onUserConnectionChange?.(clientID, false); }) ws.on("error", (err: Error) => { @@ -130,4 +134,8 @@ export class RTMTWS implements RTMT { isClientOnline(clientID: string): boolean { return this.clients.has(clientID); } + + listenOnClientConnectionChange(onUserConnectionChange: OnClientConnectionChange) { + this.onUserConnectionChange = onUserConnectionChange; + } } \ No newline at end of file