iosignal 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/client/IOCore.js
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
import { IOCore } from "./IOCore.js";
|
|
2
2
|
import { Buffer } from 'meta-buffer-pack'
|
|
3
|
-
export { Boho, RAND, BohoMsg, Meta, MetaSize
|
|
3
|
+
export { Boho, RAND, BohoMsg, Meta, MetaSize, sha256, MBP, Buffer } from 'boho'
|
|
4
4
|
export * from '../common/constants.js'
|
|
5
5
|
|
|
6
6
|
// Browser WebSocket
|
|
7
|
-
export class IO extends IOCore{
|
|
8
|
-
|
|
7
|
+
export class IO extends IOCore {
|
|
8
|
+
static binaryType = "arraybuffer"
|
|
9
|
+
|
|
10
|
+
constructor(url) {
|
|
9
11
|
super(url);
|
|
10
12
|
document.addEventListener('visibilitychange', this.browserVisiblePing.bind(this));
|
|
11
|
-
if(url) this.open();
|
|
13
|
+
if (url) this.open();
|
|
12
14
|
}
|
|
13
15
|
|
|
14
|
-
browserVisiblePing(){
|
|
16
|
+
browserVisiblePing() {
|
|
15
17
|
if (document.visibilityState === 'visible') {
|
|
16
18
|
this.ping()
|
|
17
19
|
}
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
|
|
22
|
+
|
|
21
23
|
close() {
|
|
22
|
-
if(this.socket
|
|
24
|
+
if (this.socket) {
|
|
23
25
|
this.socket.onclose = null
|
|
24
26
|
this.socket.onmessage = null
|
|
25
27
|
this.socket.onerror = null
|
|
@@ -30,66 +32,76 @@ export class IO extends IOCore{
|
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
|
|
33
|
-
stop(){
|
|
35
|
+
stop() {
|
|
34
36
|
this.close()
|
|
35
37
|
clearInterval(this.connectionCheckerIntervalID);
|
|
36
38
|
this.connectionCheckerIntervalID = null
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
40
42
|
keepAlive() {
|
|
41
|
-
if (
|
|
43
|
+
if (!this.socket || this.socket?.readyState === 3) { //closed
|
|
42
44
|
this.open();
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
createConnection(url) {
|
|
47
51
|
// Web Browser WebSocket
|
|
48
|
-
this.socket = new WebSocket
|
|
52
|
+
this.socket = new WebSocket(url);
|
|
53
|
+
this.socket.binaryType = IO.binaryType
|
|
54
|
+
|
|
49
55
|
this.stateChange('opening')
|
|
50
56
|
|
|
51
|
-
this.socket.binaryType = "arraybuffer"
|
|
52
57
|
this.socket.onopen = () => {
|
|
53
|
-
this.socket.
|
|
54
|
-
|
|
58
|
+
if (this.socket.binaryType == "arraybuffer") {
|
|
59
|
+
this.socket.onmessage = this.onWebSocketMessage.bind(this);
|
|
60
|
+
} else { // blob
|
|
61
|
+
this.socket.onmessage = this.onWebSocketMessageBlob.bind(this);
|
|
62
|
+
}
|
|
63
|
+
this.emit('open');
|
|
55
64
|
};
|
|
56
65
|
|
|
57
|
-
this.socket.onerror = (e)=>{
|
|
66
|
+
this.socket.onerror = (e) => {
|
|
58
67
|
this.emit('error', e)
|
|
59
68
|
}
|
|
60
69
|
|
|
61
|
-
this.socket.onclose = ()=>{
|
|
62
|
-
this.emit('close'
|
|
70
|
+
this.socket.onclose = () => {
|
|
71
|
+
this.emit('close');
|
|
63
72
|
}
|
|
64
73
|
}
|
|
65
74
|
|
|
66
|
-
onWebSocketMessage(
|
|
75
|
+
onWebSocketMessage(event) {
|
|
76
|
+
// event.data is arrayBuffer
|
|
67
77
|
this.rxCounter++;
|
|
68
78
|
this.lastTxRxTime = Date.now();
|
|
69
|
-
let buffer
|
|
70
|
-
|
|
71
|
-
// if( event.data instanceof ArrayBuffer ){
|
|
72
|
-
// //binary frame
|
|
73
|
-
// }else{
|
|
74
|
-
// //text frame
|
|
75
|
-
// }
|
|
76
|
-
buffer = Buffer.from( event.data )
|
|
79
|
+
let buffer = Buffer.from(event.data)
|
|
77
80
|
this.rxBytes += buffer.byteLength
|
|
81
|
+
this.emit('socket_data', buffer);
|
|
82
|
+
}
|
|
78
83
|
|
|
79
|
-
|
|
84
|
+
async onWebSocketMessageBlob(event) {
|
|
85
|
+
// event.data is Blob
|
|
86
|
+
this.rxCounter++;
|
|
87
|
+
this.lastTxRxTime = Date.now();
|
|
88
|
+
let ab = await event.data.arrayBuffer()
|
|
89
|
+
let buffer = Buffer.from(ab)
|
|
90
|
+
this.rxBytes += buffer.byteLength
|
|
91
|
+
this.emit('socket_data', buffer);
|
|
80
92
|
}
|
|
81
93
|
|
|
82
|
-
socket_send(data) {
|
|
83
|
-
if(
|
|
94
|
+
socket_send(data) {
|
|
95
|
+
if (this.socket?.readyState === 1) { //open
|
|
84
96
|
// console.log('websocket send', data)
|
|
85
|
-
this.socket.send(
|
|
97
|
+
this.socket.send(data)
|
|
86
98
|
this.txCounter++;
|
|
87
99
|
this.txBytes += data.byteLength
|
|
88
100
|
this.lastTxRxTime = Date.now();
|
|
89
|
-
}else{
|
|
101
|
+
} else {
|
|
90
102
|
console.log('.')
|
|
91
103
|
}
|
|
92
104
|
}
|
|
93
|
-
|
|
105
|
+
|
|
94
106
|
}
|
|
95
107
|
|