iosignal 1.0.0 → 1.2.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.
@@ -13,7 +13,6 @@ export class IO extends IOCore{
13
13
 
14
14
  browserVisiblePing(){
15
15
  if (document.visibilityState === 'visible') {
16
- console.log('[Remote connection check] visibilityState is visible.')
17
16
  this.ping()
18
17
  }
19
18
  }
@@ -0,0 +1,77 @@
1
+ import { MBP } from 'meta-buffer-pack'
2
+ import { PAYLOAD_TYPE, IOMsg } from "./constants.js";
3
+ const encoder = new TextEncoder()
4
+
5
+
6
+ export function getSignalPack( tag, ...args ){
7
+ if( typeof tag !== 'string') throw TypeError('tag should be string.')
8
+ let tagEncoded = encoder.encode( tag)
9
+ let payload = parsePayload( args )
10
+
11
+ let sigPack;
12
+ if( payload.type == PAYLOAD_TYPE.EMPTY ){
13
+ sigPack = MBP.pack(
14
+ MBP.MB('#MsgType','8', IOMsg.SIGNAL) ,
15
+ MBP.MB('#tagLen','8', tagEncoded.byteLength),
16
+ MBP.MB('#tag', tagEncoded),
17
+ MBP.MB('#payloadType', '8', payload.type )
18
+ )
19
+ }else if( payload.type == PAYLOAD_TYPE.MBA ){
20
+ sigPack = MBP.pack(
21
+ MBP.MB('#MsgType','8', IOMsg.SIGNAL) ,
22
+ MBP.MB('#tagLen','8', tagEncoded.byteLength),
23
+ MBP.MB('#tag', tagEncoded),
24
+ MBP.MB('#payloadType', '8', payload.type ),
25
+ MBP.MBA(...args)
26
+ )
27
+ }else {
28
+ sigPack = MBP.pack(
29
+ MBP.MB('#MsgType','8', IOMsg.SIGNAL) ,
30
+ MBP.MB('#tagLen','8', tagEncoded.byteLength),
31
+ MBP.MB('#tag', tagEncoded),
32
+ MBP.MB('#payloadType', '8', payload.type ),
33
+ MBP.MB('#payload', payload.buffer )
34
+ )
35
+ }
36
+ return sigPack
37
+ }
38
+
39
+
40
+ export function parsePayload( args){
41
+ let type, pack;
42
+ if( args.length == 0){
43
+ type = PAYLOAD_TYPE.EMPTY
44
+ pack = null
45
+ }else if( args.length == 1){
46
+ if( typeof args[0] === 'string' || typeof args[0] === 'number'){
47
+ type = PAYLOAD_TYPE.TEXT
48
+ pack = encoder.encode( args[0] + ".") // add null area.
49
+ pack[pack.byteLength - 1 ] = 0 // set null.
50
+
51
+ }else if( ArrayBuffer.isView( args[0]) || args[0] instanceof ArrayBuffer ){ //one buffer
52
+ type = PAYLOAD_TYPE.BINARY
53
+ pack = MBP.B8( args[0 ] )
54
+ }else if(typeof args[0] === 'object'){
55
+ type = PAYLOAD_TYPE.OBJECT
56
+ pack = encoder.encode( JSON.stringify( args[0]) )
57
+ }else{
58
+ throw new Error('unknown payload arguments')
59
+ }
60
+ }else{ // args 2 and more
61
+ let containsBuffer = false
62
+ args.forEach( item =>{
63
+ if( ArrayBuffer.isView( item ) || item instanceof ArrayBuffer ) containsBuffer = true;
64
+ })
65
+
66
+ if( containsBuffer ){
67
+ type = PAYLOAD_TYPE.MBA;
68
+ }else{
69
+ type = PAYLOAD_TYPE.MJSON;
70
+ // args is array
71
+ pack = encoder.encode( JSON.stringify( args ) )
72
+ }
73
+
74
+ }
75
+
76
+ return { type: type, buffer: pack }
77
+ }
@@ -1,7 +1,8 @@
1
1
  import { MBP } from 'meta-buffer-pack'
2
2
  import { ServerRemote } from './ServerRemote.js'
3
3
  import { serverOption } from './serverOption.js'
4
- import { IOMsg, PAYLOAD_TYPE, CLIENT_STATE } from '../common/constants.js'
4
+ import { IOMsg, CLIENT_STATE } from '../common/constants.js'
5
+ import { getSignalPack } from '../common/payload.js';
5
6
  import { FileLogger } from './FileLogger.js'
6
7
  import { Metric } from './Metric.js'
7
8
 
@@ -142,8 +143,6 @@ export class Manager{
142
143
 
143
144
 
144
145
  serverSignal(obj) {
145
- // obj.event obj.data
146
- console.log('server signal', obj)
147
146
  let sigPack = MBP.pack(
148
147
  MBP.MB('#MsgType', '8', IOMsg.SERVER_SIGNAL),
149
148
  MBP.MB('#signalObject', obj)
@@ -172,90 +171,10 @@ export class Manager{
172
171
  }
173
172
 
174
173
 
175
- parsePayload(args) {
176
- // console.log( 'parsePayload args', args )
177
- let type, pack;
178
- if (args.length == 0) {
179
- type = PAYLOAD_TYPE.EMPTY
180
- pack = null
181
- } else if (args.length == 1) {
182
- if (typeof args[0] === 'string' || typeof args[0] === 'number') {
183
- type = PAYLOAD_TYPE.TEXT
184
- pack = encoder.encode(args[0] + ".") // add null area.
185
- pack[pack.byteLength - 1] = 0 // set null.
186
-
187
- } else if (ArrayBuffer.isView(args[0]) || args[0] instanceof ArrayBuffer) { //one buffer
188
- type = PAYLOAD_TYPE.BINARY
189
- pack = MBP.B8(args[0])
190
- } else if (typeof args[0] === 'object') {
191
- type = PAYLOAD_TYPE.OBJECT
192
- pack = encoder.encode(JSON.stringify(args[0]))
193
- } else {
194
- //
195
- console.log('unknown type payload arguments')
196
- }
197
- } else { // args 2 and more
198
- let containsBuffer = false
199
- args.forEach(item => {
200
- if (ArrayBuffer.isView(item) || item instanceof ArrayBuffer) containsBuffer = true;
201
- // console.log('payload item', item )
202
- })
203
-
204
- if (containsBuffer) {
205
- type = PAYLOAD_TYPE.MBA;
206
- // pack
207
- } else {
208
- type = PAYLOAD_TYPE.MJSON;
209
- // args is array
210
- pack = encoder.encode(JSON.stringify(args))
211
- }
212
-
213
- }
214
-
215
- return { type: type, buffer: pack }
216
-
217
- }
218
-
219
- get_signal_pack(tag, ...args) {
220
- if (typeof tag !== 'string') throw TypeError('tag should be string.')
221
- let tagEncoded = encoder.encode(tag)
222
- let payload = this.parsePayload(args)
223
-
224
- let sigPack;
225
- if (payload.type == PAYLOAD_TYPE.EMPTY) {
226
- sigPack = MBP.pack(
227
- MBP.MB('#MsgType', '8', IOMsg.SIGNAL),
228
- MBP.MB('#tagLen', '8', tagEncoded.byteLength),
229
- MBP.MB('#tag', tagEncoded),
230
- MBP.MB('#payloadType', '8', payload.type)
231
- )
232
- } else if (payload.type == PAYLOAD_TYPE.MBA) {
233
- sigPack = MBP.pack(
234
- MBP.MB('#MsgType', '8', IOMsg.SIGNAL),
235
- MBP.MB('#tagLen', '8', tagEncoded.byteLength),
236
- MBP.MB('#tag', tagEncoded),
237
- MBP.MB('#payloadType', '8', payload.type),
238
- MBP.MBA(...args)
239
- )
240
- } else {
241
- sigPack = MBP.pack(
242
- MBP.MB('#MsgType', '8', IOMsg.SIGNAL),
243
- MBP.MB('#tagLen', '8', tagEncoded.byteLength),
244
- MBP.MB('#tag', tagEncoded),
245
- MBP.MB('#payloadType', '8', payload.type),
246
- MBP.MB('#payload', payload.buffer)
247
- )
248
- }
249
- return sigPack
250
- }
251
-
252
-
253
-
254
-
255
174
  // signaling to the cid subscribers.
256
175
  // example. sending cid close signal.
257
176
  deligateSignal(remote, tag, ...args) {
258
- let sigPack = this.get_signal_pack(tag, ...args)
177
+ let sigPack = getSignalPack(tag, ...args)
259
178
  this.sender(tag, remote, sigPack)
260
179
  }
261
180
 
@@ -9,22 +9,8 @@ export class Server extends EventEmitter {
9
9
 
10
10
  constructor(options, authManager , remoteManager) {
11
11
  super();
12
- // console.log('Server input options', options )
13
- this.manager = new Manager(this, authManager, remoteManager)
14
12
  this.apiNames = new Set()
15
13
 
16
- if(options.port != 0){
17
- this.startWSServer(options)
18
- }
19
- if (options.congPort) {
20
- this.startCongServer(options)
21
- }
22
-
23
-
24
- }
25
-
26
-
27
- startWSServer(options) {
28
14
  if (options.timeout) {
29
15
  let pingT = parseInt(options.timeout)
30
16
  if (pingT && pingT >= 1000) serverOption.timeout = pingT
@@ -35,13 +21,44 @@ export class Server extends EventEmitter {
35
21
  if (monitorT && monitorT >= 1000) serverOption.monitorPeriod = monitorT
36
22
  }
37
23
 
24
+ if (options.fileLogger) {
25
+ serverOption.fileLogger.connection.use = true;
26
+ serverOption.fileLogger.auth.use = true;
27
+ serverOption.fileLogger.attack.use = true;
28
+ }
29
+
30
+ if (options.showMessage) {
31
+ serverOption.showMessage = options.showMessage
32
+ }
33
+
34
+ if (options.showMetric) {
35
+ serverOption.showMetric = options.showMetric
36
+ }
37
+
38
+ if (options.timeout) {
39
+ serverOption.timeout = options.timeout
40
+ }
41
+
38
42
  if (options.port) {
39
- let port = parseInt(options.port)
40
- if (port) serverOption.port = port
43
+ serverOption.port = parseInt(options.port)
41
44
  }
45
+
46
+ if (options.congPort) {
47
+ serverOption.congPort = parseInt(options.congPort)
48
+ }
49
+
50
+ this.manager = new Manager(this, authManager, remoteManager)
51
+ if (serverOption.port) this.startWSServer(serverOption.port)
52
+ if( serverOption.congPort) this.startCongServer(serverOption.congPort)
53
+
54
+ // console.log('serverOption:', serverOption)
55
+ }
56
+
57
+
58
+ startWSServer(port) {
42
59
 
43
- console.log('WebSocketServer listen:', options.port)
44
- this.wss = new WebSocketServer(options)
60
+ console.log('opening WebSocket Server:', port)
61
+ this.wss = new WebSocketServer({ port })
45
62
  // this.wss.setMaxListeners(0)
46
63
 
47
64
  this.wss.on('error', (err) => {
@@ -63,8 +80,8 @@ export class Server extends EventEmitter {
63
80
 
64
81
  }
65
82
 
66
- startCongServer(options) {
67
- console.log('congServer listen:', options.congPort)
83
+ startCongServer( congPort ) {
84
+ console.log('opening CongSocket Server:', congPort)
68
85
 
69
86
  this.congServer = net.createServer((socket) => {
70
87
  this.manager.addRemote(socket)
@@ -74,7 +91,7 @@ export class Server extends EventEmitter {
74
91
  if(err.code == 'EADDRINUSE'){
75
92
  process.exit()
76
93
  }
77
- }).listen(serverOption.congPort, () => {
94
+ }).listen( congPort, () => {
78
95
  // console.log('congsocket server bound' );
79
96
  });
80
97
  }
@@ -2,8 +2,8 @@
2
2
 
3
3
  export let serverOption = {
4
4
  clientTracking: false,
5
- port: 7777,
6
- congPort: null,
5
+ port: 0,
6
+ congPort: 0,
7
7
  timeout: 50000,
8
8
  showMessage: 'none',
9
9
  showMetric: 0,
@@ -1,8 +1,11 @@
1
- import { Server, serverOption } from 'iosignal'
1
+ import { Server } from 'iosignal'
2
2
 
3
- serverOption.showMetric = 2;
4
- serverOption.showMessage = 'message';
5
- const server = new Server( serverOption )
3
+ const options = {
4
+ port: 7777,
5
+ showMetric: 2,
6
+ showMessage: 'message'
7
+ }
8
+
9
+ const server = new Server( options )
6
10
 
7
- console.log( 'serverOption:', serverOption )
8
11
 
@@ -1,7 +1,7 @@
1
1
 
2
2
  const { IO } = require('iosignal')
3
3
 
4
- const io = new IO('wss://io.remocon.kr/ws')
4
+ const io = new IO('ws://localhost:7777')
5
5
 
6
6
  io.on('ready', ()=>{
7
7
  console.log('ready cid:', io.cid)
@@ -1,7 +1,7 @@
1
1
 
2
2
  import { IO } from "iosignal"
3
3
 
4
- const io = new IO('wss://io.remocon.kr/ws')
4
+ const io = new IO('ws://localhost:7777')
5
5
 
6
6
  io.on('ready', ()=>{
7
7
  console.log('ready cid:', io.cid)
@@ -0,0 +1,15 @@
1
+
2
+ import { IO } from "iosignal"
3
+
4
+ const io = new IO('ws://localhost:7777')
5
+
6
+ io.on('ready', async ()=>{
7
+ let res_echo = await io.req('reply', 'echo', 'hello' )
8
+ let res_date = await io.req('reply', 'date' )
9
+ let res_unixtime = await io.req('reply', 'unixtime' )
10
+
11
+ if( res_echo.ok ) console.log( res_echo.body )
12
+ if( res_date.ok ) console.log( res_date.body )
13
+ if( res_unixtime.ok ) console.log( res_unixtime.body )
14
+
15
+ });
@@ -0,0 +1,19 @@
1
+
2
+ import { IO } from "iosignal"
3
+
4
+ const io = new IO('ws://localhost:7777')
5
+
6
+ io.on('ready', ()=>{
7
+ console.log('ready cid:', io.cid)
8
+ });
9
+
10
+ io.on('change', ( evt )=>{
11
+ console.log('\nchange event:', evt )
12
+ console.log('io.state<Number>', io.state )
13
+ console.log('io.stateName<String>:', io.stateName )
14
+ });
15
+
16
+
17
+ io.on('error',err=>{
18
+ console.log('err', err)
19
+ })
@@ -1,8 +1,10 @@
1
- import { Server, serverOption , Auth_Env } from '../index.js'
2
-
3
- serverOption.showMetric = 2;
4
- serverOption.showMessage = 'message';
1
+ import { Server, Auth_Env } from '../index.js'
5
2
 
3
+ const options = {
4
+ port: 7777,
5
+ showMetric: 2,
6
+ showMessage: 'message'
7
+ }
6
8
  // You can use environment variable BOHO_AUTH
7
9
 
8
10
  // $ BOHO_AUTH=id1.key1.255,id2.key2.200 node app.js
@@ -12,6 +14,6 @@ serverOption.showMessage = 'message';
12
14
  let authInfo = 'id1.key1.255,id2.key2.200'
13
15
 
14
16
  let authManager = new Auth_Env( authInfo )
15
- const server = new Server( serverOption, authManager )
16
- // console.log( 'serverOption:', serverOption )
17
+ const server = new Server( options, authManager )
18
+
17
19
 
@@ -0,0 +1,12 @@
1
+ import { Server, Auth_File } from '../index.js'
2
+
3
+ const options = {
4
+ port: 7777,
5
+ showMetric: 2,
6
+ showMessage: 'message'
7
+ }
8
+
9
+ // const server = new Server( options, new Auth_File('../authInfo.json') ) // JSON version. cannot add comments.
10
+ const server = new Server( options, new Auth_File('../auth_file.mjs') ) // JS version. it support comments.
11
+
12
+
@@ -0,0 +1,6 @@
1
+ import { Server ,api_reply } from 'iosignal'
2
+
3
+ const server = new Server( { port: 7777 } )
4
+
5
+ // api response module
6
+ server.api('reply', api_reply)
@@ -0,0 +1,7 @@
1
+ import { Server ,api_sudo , Auth_Env } from 'iosignal'
2
+
3
+ let authManager = new Auth_Env('admin.adminkey.255')
4
+ const server = new Server( { port: 7777 } ,authManager )
5
+
6
+ // api response module
7
+ server.api('sudo', api_sudo )
@@ -1,8 +1,9 @@
1
- let { Server, serverOption } = require('iosignal')
2
-
3
- serverOption.showMetric = 2;
4
- // serverOption.port = 7777 // default. websocket port 7777 will be open.
5
- serverOption.congPort = 8888 // additional tcp cong port open. for Arduino like devices.
6
- const server = new Server( serverOption )
7
- console.log( 'serverOption:', serverOption )
1
+ let { Server } = require('iosignal')
8
2
 
3
+ const server = new Server(
4
+ {
5
+ port: 7777, // WebSocket
6
+ congPort: 8888, // CongSocket
7
+ showMetric: 2, // show clients cid(state) info
8
+ showMessage: 'message' // show raw signal buffer
9
+ })
@@ -1,12 +1,12 @@
1
- import { Server, serverOption } from '../index.js'
1
+ import { Server } from '../index.js'
2
2
 
3
- // serverOption.showMetric = 2;
4
- // serverOption.showMessage = 'message';
5
- // serverOption.port = '6666'
6
- // serverOption.congPort = '6667'
3
+ const server = new Server(
4
+ { port: 7777, // WebSocket
5
+ congPort: 8888, // CongSocket
6
+ showMetric: 2,
7
+ showMessage: 'message'
8
+ } )
7
9
 
8
- const server = new Server( serverOption )
9
- console.log( 'serverOption:', serverOption )
10
10
 
11
11
  // text message handler (No binary message)
12
12
  // print text message
@@ -0,0 +1,12 @@
1
+ import { Server } from '../index.js'
2
+
3
+ const server = new Server({
4
+ port: 7777 ,
5
+ congPort: 8888,
6
+ showMessage: 'message'
7
+ } )
8
+
9
+ server.on('text_message', (data, client)=>{
10
+ console.log( 'text_message', data ,'from', client.cid )
11
+ client.send('from server. your cid is '+ client.cid + ".")
12
+ })
@@ -1,9 +0,0 @@
1
- import { Server, serverOption , Auth_File } from '../index.js'
2
-
3
- serverOption.showMetric = 2;
4
- serverOption.showMessage = 'message';
5
-
6
- // const server = new Server( serverOption, new Auth_File('../authInfo.json') ) // JSON version. cannot add comments.
7
- const server = new Server( serverOption, new Auth_File('../auth_file.mjs') ) // JS version. it support comments.
8
- // console.log( 'serverOption:', serverOption )
9
-
@@ -1,10 +0,0 @@
1
- import { Server, serverOption ,api_reply } from 'iosignal'
2
- serverOption.showMessage = 'message';
3
-
4
- // no authManager
5
- serverOption.port = 7777
6
- const rs = new Server( serverOption )
7
- // api response module
8
- rs.api('reply', api_reply)
9
-
10
- // console.log( 'serverOption:', serverOption )
@@ -1,11 +0,0 @@
1
- import { Server, serverOption ,api_sudo , Auth_Env } from 'iosignal'
2
- serverOption.showMessage = 'message';
3
-
4
- // authManager
5
- serverOption.port = 7777
6
- let authManager = new Auth_Env('admin.adminkey.255')
7
- const rs = new Server( serverOption ,authManager )
8
- // api response module
9
- rs.api('sudo', api_sudo )
10
-
11
- // console.log( 'serverOption:', serverOption )