add listenOnClientConnectionChange to rtmt
This commit is contained in:
parent
b1a73167eb
commit
80d9401733
@ -1,8 +1,10 @@
|
|||||||
export type Bytes = Buffer
|
export type Bytes = Buffer
|
||||||
export type OnMessage = (clientID: string, message: Object) => any
|
export type OnMessage = (clientID: string, message: Object) => any
|
||||||
|
export type OnClientConnectionChange = (clientID: string, isOnline : boolean) => any
|
||||||
|
|
||||||
export interface RTMT {
|
export interface RTMT {
|
||||||
sendMessage: (clientID: string, channel: string, message: Object) => any;
|
sendMessage: (clientID: string, channel: string, message: Object) => any;
|
||||||
listenMessage: (channel: string, callback: OnMessage) => any;
|
listenMessage: (channel: string, callback: OnMessage) => any;
|
||||||
isClientOnline(clientID: string): boolean;
|
isClientOnline(clientID: string): boolean;
|
||||||
|
listenOnClientConnectionChange(onUserConnectionChange: OnClientConnectionChange): void;
|
||||||
}
|
}
|
||||||
@ -1,5 +1,5 @@
|
|||||||
import { decode, encode } from "./encode_decode_message"
|
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 WebSocket from "ws"
|
||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
@ -17,6 +17,8 @@ export class RTMTWS implements RTMT {
|
|||||||
|
|
||||||
private pingInterval?: NodeJS.Timeout = undefined;
|
private pingInterval?: NodeJS.Timeout = undefined;
|
||||||
|
|
||||||
|
onUserConnectionChange?: OnClientConnectionChange;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.messageChannels = new Map<String, OnMessage>()
|
this.messageChannels = new Map<String, OnMessage>()
|
||||||
this.wsServer = null
|
this.wsServer = null
|
||||||
@ -32,6 +34,7 @@ export class RTMTWS implements RTMT {
|
|||||||
const regexResult = req.url.split(RegExp("\/\?userKey="));
|
const regexResult = req.url.split(RegExp("\/\?userKey="));
|
||||||
const clientID = regexResult[1]
|
const clientID = regexResult[1]
|
||||||
this.clients.set(clientID, ws)
|
this.clients.set(clientID, ws)
|
||||||
|
this.onUserConnectionChange?.(clientID, true);
|
||||||
ws.on("message", (messageBytes: string) => {
|
ws.on("message", (messageBytes: string) => {
|
||||||
this.onWebSocketMessage(clientID, messageBytes)
|
this.onWebSocketMessage(clientID, messageBytes)
|
||||||
})
|
})
|
||||||
@ -39,6 +42,7 @@ export class RTMTWS implements RTMT {
|
|||||||
ws.on("close", (code: number, reason: string) => {
|
ws.on("close", (code: number, reason: string) => {
|
||||||
console.log("WS Closed! code : " + code + " reason : " + reason);
|
console.log("WS Closed! code : " + code + " reason : " + reason);
|
||||||
this.clients.delete(clientID)
|
this.clients.delete(clientID)
|
||||||
|
this.onUserConnectionChange?.(clientID, false);
|
||||||
})
|
})
|
||||||
|
|
||||||
ws.on("error", (err: Error) => {
|
ws.on("error", (err: Error) => {
|
||||||
@ -130,4 +134,8 @@ export class RTMTWS implements RTMT {
|
|||||||
isClientOnline(clientID: string): boolean {
|
isClientOnline(clientID: string): boolean {
|
||||||
return this.clients.has(clientID);
|
return this.clients.has(clientID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
listenOnClientConnectionChange(onUserConnectionChange: OnClientConnectionChange) {
|
||||||
|
this.onUserConnectionChange = onUserConnectionChange;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user