-
Notifications
You must be signed in to change notification settings - Fork 102
/
Connection.ts
39 lines (28 loc) · 1.01 KB
/
Connection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import WebSocketClient from '@gamestdio/websocket';
import * as msgpack from 'notepack.io';
import { Protocol } from './Protocol';
export class Connection extends WebSocketClient {
private _enqueuedCalls: any[] = [];
constructor(url, query: any = {}) {
super(url);
this.binaryType = 'arraybuffer';
}
public onOpenCallback(event) {
super.onOpenCallback();
if (this._enqueuedCalls.length > 0) {
for (const [method, args] of this._enqueuedCalls) {
this[method].apply(this, args);
}
}
}
public send(data: any): void {
if (this.ws.readyState === WebSocket.OPEN) {
return super.send( msgpack.encode(data) );
} else {
console.warn(`colyseus.js: trying to send data while in ${ this.ws.readyState } state`);
// WebSocket not connected.
// Enqueue data to be sent when readyState == OPEN
this._enqueuedCalls.push(['send', [data]]);
}
}
}