Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stefanyohansson/sz-json-rpc
JSON-RPC client that works on browser.
https://github.com/stefanyohansson/sz-json-rpc
browser json-rpc
Last synced: 16 days ago
JSON representation
JSON-RPC client that works on browser.
- Host: GitHub
- URL: https://github.com/stefanyohansson/sz-json-rpc
- Owner: StefanYohansson
- Created: 2016-12-12T22:08:55.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-04T13:53:50.000Z (almost 8 years ago)
- Last Synced: 2024-10-28T01:25:13.306Z (2 months ago)
- Topics: browser, json-rpc
- Language: JavaScript
- Homepage:
- Size: 157 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
sz-json-rpc
JSON-RPC that works on browser.#### Installation
```
$ npm install sz-json-rpc --save
```#### Example
* Using json-rpc adapter to send requests.
```javascript
var webSocket = new WebSocket('ws://localhost:5000');
var ws = new SzJsonRpc().ws(webSocket);
ws.on('open', () => {
ws.request('ping')((err, response) => {
if (err) throw err;
console.log('response', response);
});
});
ws.on('close', (reason) => {
// this will not show when onbeforeunload event call .close
console.log('close', reason);
});
window.onbeforeunload = function() {
ws.close();
}
```* Using connector to do some tricks like auto reconect, queue requests when socket isn't ready and dispatch when it is live.
```javascript
const conn = new SzJsonRpc().wsConnection(WebSocket, 'ws://localhost:5000', {
onWSConnect: (ws) => {
ws.send('login', {}, (err, response) => {
if(err)
console.error(err);
});
ws.send('ping', null, (err, response) => {
console.log(response);
});
}
});window.onbeforeunload = function() {
if (conn.socketReady())
conn.curSocket.close();
}
```