iosignal 1.5.0 → 1.6.1

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iosignal",
3
- "version": "1.5.0",
3
+ "version": "1.6.1",
4
4
  "description": "iosignal",
5
5
  "type": "module",
6
6
  "main": "./dist/iosignal.cjs",
@@ -352,7 +352,7 @@ export class IOCore extends EventEmitter {
352
352
  break;
353
353
 
354
354
  default:
355
- // console.log('## Unkown payloadtype', payloadType)
355
+ // console.log('## Unkown payloadtype', payloadType)
356
356
  }
357
357
 
358
358
  } catch (err) {
@@ -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 , sha256, MBP, Buffer } from 'boho'
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
- constructor(url ) {
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,81 @@ 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 ( !this.socket || this.socket?.readyState === 3 ) { //closed
43
+ if (!this.socket || this.socket?.readyState === 3) { //closed
42
44
  this.open();
43
45
  }
44
46
  }
45
47
 
46
- createConnection(url){
48
+
49
+
50
+ createConnection(url) {
47
51
  // Web Browser WebSocket
48
- this.socket = new WebSocket (url );
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.onmessage = this.onWebSocketMessage.bind(this) ;
54
- this.emit('open' );
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( event ) {
75
+ onWebSocketMessage(event) {
76
+ // event.data is arrayBuffer or text_message
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
- this.emit('socket_data', buffer );
84
+ async onWebSocketMessageBlob(event) {
85
+ // event.data is Blob or text_message
86
+ this.rxCounter++;
87
+ this.lastTxRxTime = Date.now();
88
+ let buffer;
89
+ if( event.data instanceof Blob){
90
+ let ab = await event.data.arrayBuffer()
91
+ buffer = Buffer.from(ab)
92
+ }else{
93
+ buffer = Buffer.from(event.data)
94
+ }
95
+ this.rxBytes += buffer.byteLength
96
+ this.emit('socket_data', buffer);
80
97
  }
81
98
 
82
- socket_send(data) {
83
- if( this.socket?.readyState === 1 ){ //open
99
+ socket_send(data) {
100
+ if (this.socket?.readyState === 1) { //open
84
101
  // console.log('websocket send', data)
85
- this.socket.send( data )
102
+ this.socket.send(data)
86
103
  this.txCounter++;
87
104
  this.txBytes += data.byteLength
88
105
  this.lastTxRxTime = Date.now();
89
- }else{
106
+ } else {
90
107
  console.log('.')
91
108
  }
92
109
  }
93
-
110
+
94
111
  }
95
112
 
@@ -33,6 +33,7 @@
33
33
  console.log('sha256.hash("hi")', IO.Boho.sha256.hash('hi'))
34
34
 
35
35
  var ESM_IO ;
36
+ // IO.binaryType = "blob"
36
37
  var io1 = new IO('ws://localhost:7777')
37
38
  var io2 = new IO('ws://localhost:7777')
38
39
  var io3 = new IO('ws://localhost:7777')
@@ -79,7 +80,7 @@
79
80
  addMessage('ws.error')
80
81
  }
81
82
  </script>
82
- <script type="module">
83
+ <!-- <script type="module">
83
84
  import { IO, Boho, MBP, Buffer, sha256 } from "../dist/iosignal.esm.js"
84
85
  ESM_IO = IO
85
86
  console.log('ESM:')
@@ -88,6 +89,6 @@
88
89
  console.log('MBP', MBP )
89
90
  console.log('Buffer', Buffer)
90
91
  console.log('sha256.hash("hi")', sha256.hash('hi'))
91
- </script>
92
+ </script> -->
92
93
  </body>
93
94
  </html>