-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTCPSocketBase.ios.js
83 lines (66 loc) · 1.76 KB
/
TCPSocketBase.ios.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
class TCPSocketBase {
CONNECTING: number;
OPEN: number;
CLOSING: number;
CLOSED: number;
onclose: ?Function;
onerror: ?Function;
onmessage: ?Function;
onopen: ?Function;
binaryType: ?string;
bufferedAmount: number;
extension: ?string;
protocol: ?string;
readyState: number;
host: ?string;
port: ?number;
constructor(host: string, port: number, protocols: ?any) {
this.CONNECTING = 0;
this.OPEN = 1;
this.CLOSING = 2;
this.CLOSED = 3;
if (!protocols) {
protocols = [];
}
this.connectToSocketImpl(host, port);
}
close(): void {
if (this.readyState === TCPSocketBase.CLOSING ||
this.readyState === TCPSocketBase.CLOSED) {
return;
}
if (this.readyState === TCPSocketBase.CONNECTING) {
this.cancelConnectionImpl();
}
this.closeConnectionImpl();
}
send(data: any): void {
if (this.readyState === TCPSocketBase.CONNECTING) {
throw new Error('INVALID_STATE_ERR');
}
if (typeof data === 'string') {
this.sendStringImpl(data);
} else if (data instanceof Uint8Array) {
this.sendByteArrayImpl(data);
} else {
throw new Error('Not supported data type');
}
}
closeConnectionImpl(): void {
throw new Error('Subclass must define closeConnectionImpl method');
}
connectToSocketImpl(): void {
throw new Error('Subclass must define connectToSocketImpl method');
}
cancelConnectionImpl(): void {
throw new Error('Subclass must define cancelConnectionImpl method');
}
sendStringImpl(): void {
throw new Error('Subclass must define sendStringImpl method');
}
sendByteArrayImpl(): void {
throw new Error('Subclass must define sendArrayBufferImpl method');
}
}
module.exports = TCPSocketBase;