1 | export default class ConnectionManager {
|
2 | private webSocket: any
|
3 |
|
4 | constructor(path?: string) {
|
5 | this.webSocket = new WebSocket(path)
|
6 | }
|
7 |
|
8 | send(payload: any) {
|
9 | if (this.webSocket.readyState === 1) {
|
10 | this.webSocket.send(payload)
|
11 | }
|
12 | }
|
13 |
|
14 | on(event: "open" | "close" | "message", callback: any) {
|
15 | if (event === "open") {
|
16 | this.webSocket.onopen = callback
|
17 | } else if (event === "message") {
|
18 | this.webSocket.onmessage = (evt) => callback(evt.data)
|
19 | }
|
20 | }
|
21 |
|
22 | close() {
|
23 | this.webSocket.close()
|
24 | }
|
25 | }
|