update
This commit is contained in:
33
src/WebSocketHandler.ts
Normal file
33
src/WebSocketHandler.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import {WebSocketPayload} from "./WebSocketPayload";
|
||||
|
||||
export class WebSocketHandler {
|
||||
private static _ws: WebSocket;
|
||||
|
||||
static get ws(): WebSocket {
|
||||
return this._ws;
|
||||
}
|
||||
|
||||
static set ws(value: WebSocket) {
|
||||
this._ws = value;
|
||||
}
|
||||
|
||||
public static send(payload: WebSocketPayload): Promise<void> {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
if( this.ws && this.ws.readyState == 1 )
|
||||
{
|
||||
await this.ws.send(payload.toString());
|
||||
resolve();
|
||||
}
|
||||
else
|
||||
{
|
||||
reject("Websocket is not connected!");
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user