jsonbird
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -1482,2 +1482,11 @@ 'use strict'; | ||
} | ||
/** | ||
* Resets all statistics which are reported by ping events. | ||
* | ||
* Currently this method only sets `pingConsecutiveFails` to 1 for the next `pingFail` event | ||
*/ | ||
resetPingStatistics() { | ||
this[PRIVATE].pingConsecutiveFails = 0; | ||
} | ||
} | ||
@@ -1484,0 +1493,0 @@ |
{ | ||
"name": "jsonbird", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "JSON-RPC 2.0 client/server/peer for any reliable transport. Inter-process communication. REST. WebSocket. WebWorker. Out of order messaging or in-order byte streams", | ||
@@ -5,0 +5,0 @@ "main": "lib/JSONBird.js", |
@@ -85,3 +85,4 @@ # JSONBird | ||
```javascript | ||
const JSONBird = require('jsonbird'); // e.g. browserify | ||
// this example should be bundled using browserify or webpack | ||
const JSONBird = require('jsonbird'); | ||
const {WebSocket} = window; | ||
@@ -95,22 +96,22 @@ | ||
const connect = () => { | ||
const ws = new WebSocket('ws://localhost:1234/'); | ||
ws.binaryType = 'arraybuffer'; | ||
const ws = new WebSocket('ws://localhost:1234/'); | ||
ws.binaryType = 'arraybuffer'; | ||
const rpcOnData = str => ws.send(str); | ||
const rpcOnData = str => ws.send(str); | ||
ws.onopen = () => { | ||
rpc.on('data', rpcOnData); | ||
ws.onopen = () => { | ||
rpc.on('data', rpcOnData); | ||
rpc.call('add', 10, 3) | ||
.then(result => rpc.call('subtract', result, 1)) | ||
.then(result => console.log('result:', result)) // 12 | ||
; | ||
}; | ||
ws.onclose = () => { | ||
rpc.removeListener('data', rpcOnData); | ||
}; | ||
ws.onmessage = e => { | ||
const data = Buffer.from(e.data); | ||
rpc.write(data); | ||
}; | ||
rpc.call('add', 10, 3) | ||
.then(result => rpc.call('subtract', result, 1)) | ||
.then(result => console.log('result:', result)) // 12 | ||
; | ||
}; | ||
ws.onclose = () => { | ||
rpc.removeListener('data', rpcOnData); | ||
}; | ||
ws.onmessage = e => { | ||
const data = Buffer.from(e.data); | ||
rpc.write(data); | ||
}; | ||
}; | ||
@@ -121,2 +122,67 @@ | ||
## WebWorker | ||
```javascript | ||
// this example should be bundled using browserify or webpack | ||
const JSONBird = require('jsonbird'); | ||
const {WebSocket} = window; | ||
const worker = new Worker('myWorker.js'); | ||
const rpc = new JSONBird({ | ||
// take advantage of the structured clone algorithm | ||
readableMode: 'object', | ||
writableMode: 'object', | ||
receiveErrorStack: true, | ||
sendErrorStack: true, | ||
}); | ||
worker.onmessage = e => rpc.write(e.data); | ||
rpc.on('data', object => worker.postMessage(object)); | ||
``` | ||
__myWorker.js__: | ||
```javascript | ||
// this example should be bundled using browserify or webpack | ||
const JSONBird = require('jsonbird'); | ||
const rpc = new JSONBird({ | ||
readableMode: 'object', | ||
writableMode: 'object', | ||
receiveErrorStack: true, | ||
sendErrorStack: true, | ||
}); | ||
self.onmessage = e => rpc.write(e.data); | ||
rpc.on('data', object => context.postMessage(object)); | ||
``` | ||
## Shared WebWorker | ||
```javascript | ||
// this example should be bundled using browserify or webpack | ||
const JSONBird = require('jsonbird'); | ||
const {WebSocket} = window; | ||
const worker = new SharedWorker('mySharedWorker.js'); | ||
const rpc = new JSONBird({ | ||
// take advantage of the structured clone algorithm | ||
readableMode: 'object', | ||
writableMode: 'object', | ||
receiveErrorStack: true, | ||
sendErrorStack: true, | ||
}); | ||
worker.port.onmessage = e => rpc.write(e.data); | ||
rpc.on('data', object => worker.port.postMessage(object)); | ||
``` | ||
__mySharedWorker.js__: | ||
```javascript | ||
// this example should be bundled using browserify or webpack | ||
const JSONBird = require('jsonbird'); | ||
const rpc = new JSONBird({ | ||
readableMode: 'object', | ||
writableMode: 'object', | ||
receiveErrorStack: true, | ||
sendErrorStack: true, | ||
}); | ||
self.onconnect = e => { | ||
const port = e.ports[0]; | ||
port.onmessage = e => rpc.write(e.data); | ||
rpc.on('data', object => port.postMessage(object)); | ||
}; | ||
``` | ||
# API Documentation |
111
README.md
@@ -85,3 +85,4 @@ # JSONBird | ||
```javascript | ||
const JSONBird = require('jsonbird'); // e.g. browserify | ||
// this example should be bundled using browserify or webpack | ||
const JSONBird = require('jsonbird'); | ||
const {WebSocket} = window; | ||
@@ -95,22 +96,22 @@ | ||
const connect = () => { | ||
const ws = new WebSocket('ws://localhost:1234/'); | ||
ws.binaryType = 'arraybuffer'; | ||
const ws = new WebSocket('ws://localhost:1234/'); | ||
ws.binaryType = 'arraybuffer'; | ||
const rpcOnData = str => ws.send(str); | ||
const rpcOnData = str => ws.send(str); | ||
ws.onopen = () => { | ||
rpc.on('data', rpcOnData); | ||
ws.onopen = () => { | ||
rpc.on('data', rpcOnData); | ||
rpc.call('add', 10, 3) | ||
.then(result => rpc.call('subtract', result, 1)) | ||
.then(result => console.log('result:', result)) // 12 | ||
; | ||
}; | ||
ws.onclose = () => { | ||
rpc.removeListener('data', rpcOnData); | ||
}; | ||
ws.onmessage = e => { | ||
const data = Buffer.from(e.data); | ||
rpc.write(data); | ||
}; | ||
rpc.call('add', 10, 3) | ||
.then(result => rpc.call('subtract', result, 1)) | ||
.then(result => console.log('result:', result)) // 12 | ||
; | ||
}; | ||
ws.onclose = () => { | ||
rpc.removeListener('data', rpcOnData); | ||
}; | ||
ws.onmessage = e => { | ||
const data = Buffer.from(e.data); | ||
rpc.write(data); | ||
}; | ||
}; | ||
@@ -121,2 +122,67 @@ | ||
## WebWorker | ||
```javascript | ||
// this example should be bundled using browserify or webpack | ||
const JSONBird = require('jsonbird'); | ||
const {WebSocket} = window; | ||
const worker = new Worker('myWorker.js'); | ||
const rpc = new JSONBird({ | ||
// take advantage of the structured clone algorithm | ||
readableMode: 'object', | ||
writableMode: 'object', | ||
receiveErrorStack: true, | ||
sendErrorStack: true, | ||
}); | ||
worker.onmessage = e => rpc.write(e.data); | ||
rpc.on('data', object => worker.postMessage(object)); | ||
``` | ||
__myWorker.js__: | ||
```javascript | ||
// this example should be bundled using browserify or webpack | ||
const JSONBird = require('jsonbird'); | ||
const rpc = new JSONBird({ | ||
readableMode: 'object', | ||
writableMode: 'object', | ||
receiveErrorStack: true, | ||
sendErrorStack: true, | ||
}); | ||
self.onmessage = e => rpc.write(e.data); | ||
rpc.on('data', object => context.postMessage(object)); | ||
``` | ||
## Shared WebWorker | ||
```javascript | ||
// this example should be bundled using browserify or webpack | ||
const JSONBird = require('jsonbird'); | ||
const {WebSocket} = window; | ||
const worker = new SharedWorker('mySharedWorker.js'); | ||
const rpc = new JSONBird({ | ||
// take advantage of the structured clone algorithm | ||
readableMode: 'object', | ||
writableMode: 'object', | ||
receiveErrorStack: true, | ||
sendErrorStack: true, | ||
}); | ||
worker.port.onmessage = e => rpc.write(e.data); | ||
rpc.on('data', object => worker.port.postMessage(object)); | ||
``` | ||
__mySharedWorker.js__: | ||
```javascript | ||
// this example should be bundled using browserify or webpack | ||
const JSONBird = require('jsonbird'); | ||
const rpc = new JSONBird({ | ||
readableMode: 'object', | ||
writableMode: 'object', | ||
receiveErrorStack: true, | ||
sendErrorStack: true, | ||
}); | ||
self.onconnect = e => { | ||
const port = e.ports[0]; | ||
port.onmessage = e => rpc.write(e.data); | ||
rpc.on('data', object => port.postMessage(object)); | ||
}; | ||
``` | ||
# API Documentation | ||
@@ -174,2 +240,3 @@ <a name="JSONBird"></a> | ||
* [.stopPinging()](#JSONBird+stopPinging) | ||
* [.resetPingStatistics()](#JSONBird+resetPingStatistics) | ||
* ["error" (error)](#JSONBird+event_error) | ||
@@ -683,2 +750,10 @@ * ["protocolError" (error)](#JSONBird+event_protocolError) | ||
**Kind**: instance method of <code>[JSONBird](#JSONBird)</code> | ||
<a name="JSONBird+resetPingStatistics"></a> | ||
### jsonBird.resetPingStatistics() | ||
Resets all statistics which are reported by ping events. | ||
Currently this method only sets `pingConsecutiveFails` to 1 for the next `pingFail` event | ||
**Kind**: instance method of <code>[JSONBird](#JSONBird)</code> | ||
<a name="JSONBird+event_error"></a> | ||
@@ -685,0 +760,0 @@ |
@@ -359,2 +359,48 @@ 'use strict'; | ||
it('should reset pingConsecutiveFails when resetPingStatistics() is called', () => { | ||
const rpc = new JSONBird({ | ||
sessionId: null, | ||
writableMode: 'object', | ||
readableMode: 'object', | ||
pingInterval: 4567, | ||
pingTimeout: 1234, | ||
setTimeout, | ||
clearTimeout, | ||
}); | ||
rpc.on('protocolError', error => console.error(error)); | ||
addPingEvents(rpc); | ||
readStream.pipe(rpc); | ||
rpc.pipe(writeStream); | ||
rpc.startPinging(); | ||
timerCalls[0].func(); // it is time to send a ping | ||
return writeWait.wait(1).then(() => { | ||
assert.strictEqual(timerCalls[1].timeout, 1234); | ||
timerCalls[1].func(); // the call took too long | ||
return pingEventWait.wait(1); | ||
}).then(() => { | ||
assert.lengthOf(pingEvents, 1); | ||
assert.strictEqual(pingEvents[0].consecutiveFails, 1); | ||
assert.strictEqual(timerCalls[2].timeout, 4567); | ||
timerCalls[2].func(); // it is time to send a ping | ||
return writeWait.wait(1); | ||
}).then(() => { | ||
rpc.resetPingStatistics(); | ||
assert.strictEqual(timerCalls[3].timeout, 1234); | ||
timerCalls[3].func(); // the call took too long | ||
return pingEventWait.wait(1); | ||
}).then(() => { | ||
assert.lengthOf(pingEvents, 2); | ||
assert.strictEqual(pingEvents[0].consecutiveFails, 1); | ||
rpc.stopPinging(); | ||
}); | ||
}); | ||
it('should stop pinging on finish', () => { | ||
@@ -361,0 +407,0 @@ const rpc = new JSONBird({ |
223269
4451
876