-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebSockets.js
55 lines (48 loc) · 1.48 KB
/
WebSockets.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
function startWebSocket(cb, port) {
if (!port) {
return;
}
const ws = new WebSocket('ws://' + port + ':3500'); // wss localhost === 10.0.2.2
ws.onopen = () => {
// connection opened
// console.log('ws send ', 'something');
// console.log(Object.keys(ws));
// console.log(ws['CONNECTING']);
// console.log(ws['OPEN']);
// console.log(ws['CLOSING']);
// console.log(ws['CLOSED']);
// console.log(ws['readyState']);
// console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(ws)));
// ws.send('something'); // send a message
// Array [
// "constructor",
// "close",
// "send",
// "ping",
// "_close",
// "_unregisterEvents",
// "_registerEvents",
// "binaryType",
// ]
// console.log('open');
cb('open');
};
ws.onmessage = (e) => {
// a message was received
// console.log('ws message ', e.data);
cb('message', e.data);
};
ws.onerror = (e) => {
// an error occurred
// console.log('ws error ', e.message);
// console.log(Object.getOwnPropertyNames(Object.getPrototypeOf(ws)));
cb('error', e.message);
};
ws.onclose = (e) => {
// connection closed
// console.log('ws close ', e.code, e.reason);
cb('close', e.code + ' - ' + e.reason);
};
return ws;
}
export default startWebSocket;