mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-01 13:10:43 +00:00
move to new skeleton
This commit is contained in:
1 parent
972b255a48
commit
663c645cf1
25 files changed
+9
-9
No files matched your search
@@ -0,0 +1,36 @@
|
||||
// JSON-RPC over Websocket implementation
|
||||
var JSONRPC_TIMEOUT_MS = 1000;
|
||||
|
||||
var jsonrpc = function(url, onopen, onclose, onnotification) {
|
||||
var rpcid = 0, pending = {}, ws = new WebSocket(url);
|
||||
if (!ws) return null;
|
||||
ws.onclose = onclose;
|
||||
ws.onmessage = function(ev) {
|
||||
const frame = JSON.parse(ev.data);
|
||||
console.log('rcvd', frame, 'pending:', pending);
|
||||
if (frame.id !== undefined) {
|
||||
if (pending[frame.id] !== undefined) pending[frame.id](frame); // Resolve
|
||||
delete (pending[frame.id]);
|
||||
} else {
|
||||
if (onnotification) onnotification(frame);
|
||||
}
|
||||
};
|
||||
if (onopen) onopen();
|
||||
return {
|
||||
close: () => ws.close(),
|
||||
call: function(method, params) {
|
||||
const id = rpcid++, request = {id, method, params};
|
||||
ws.send(JSON.stringify(request));
|
||||
console.log('sent', request);
|
||||
return new Promise(function(resolve, reject) {
|
||||
setTimeout(function() {
|
||||
if (pending[id] === undefined) return;
|
||||
log('Timing out frame ', JSON.stringify(request));
|
||||
delete (pending[id]);
|
||||
reject();
|
||||
}, JSONRPC_TIMEOUT_MS);
|
||||
pending[id] = x => resolve(x);
|
||||
});
|
||||
},
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user