Skip to content

Commit 186ef2c

Browse files
author
ionut.stan
committed
addWebSocket and addRTCDataChannel have been made synchronous and renamed into addWebSocketSync and addRTCDataChannelSync mainly to force breaking compatibility (help catch incompatible signature faster).
1 parent c520505 commit 186ef2c

File tree

11 files changed

+97
-90
lines changed

11 files changed

+97
-90
lines changed

README.MD

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ webSocketServer.on(
270270
"connection",
271271
async (webSocket) =>
272272
{
273-
const nWebSocketConnectionID = await wsJSONRPCRouter.addWebSocket(webSocket);
273+
const nWebSocketConnectionID = wsJSONRPCRouter.addWebSocketSync(webSocket);
274274
// Do something with nWebSocketConnectionID and webSocket here, like register them as a pair with an authorization plugin.
275275

276276
// const clientForThisConnection = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
@@ -300,7 +300,7 @@ await new Promise((fnResolve, fnReject) => {
300300
const wsJSONRPCRouter = new JSONRPC.BidirectionalWebsocketRouter(jsonrpcServer);
301301

302302

303-
const nWebSocketConnectionID = await wsJSONRPCRouter.addWebSocket(webSocket);
303+
const nWebSocketConnectionID = wsJSONRPCRouter.addWebSocketSync(webSocket);
304304

305305

306306
// Obtain single client. See above section "Extending the client" for the TestClient class (subclass of JSONRPC.Client).
@@ -389,15 +389,12 @@ __Site B (ES5 browser). WebSocket client (connects to the above WebSocket TCP se
389389
ws.addEventListener("open", function(event){
390390
var wsJSONRPCRouter = new JSONRPC.BidirectionalWebsocketRouter(jsonrpcServer);
391391

392-
wsJSONRPCRouter.addWebSocket(ws)
393-
.then(function(nWebSocketConnectionID){
394-
clientOfBidirectionalWS = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
392+
var nWebSocketConnectionID = wsJSONRPCRouter.addWebSocketSync(ws);
395393

396-
clientOfBidirectionalWS.rpc("ping", ["Calling from html es5 client, websocket transport with bidirectional JSONRPC."])
397-
.then(console.log)
398-
.catch(console.log)
399-
;
400-
})
394+
clientOfBidirectionalWS = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
395+
396+
clientOfBidirectionalWS.rpc("ping", ["Calling from html es5 client, websocket transport with bidirectional JSONRPC."])
397+
.then(console.log)
401398
.catch(console.log)
402399
;
403400
});

builds/browser/es5/jsonrpc.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builds/browser/es5/jsonrpc.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builds/browser/es7/jsonrpc.min.js

Lines changed: 59 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builds/browser/es7/jsonrpc.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jsonrpc-bidirectional",
33
"description": "Bidirectional JSONRPC over web sockets or HTTP with extensive plugin support.",
4-
"version": "4.3.1",
4+
"version": "5.0.0",
55
"scripts": {
66
"build": "node build.js",
77
"test": "node --expose-gc --max-old-space-size=1024 tests/main.js",
@@ -77,4 +77,4 @@
7777
"node_modules/es6-promise/dist/es6-promise.auto.min.js",
7878
"node_modules/es6-promise/dist/es6-promise.auto.min.js.map"
7979
]
80-
}
80+
}

src/BidirectionalWebRTCRouter.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ module.exports =
1717
class BidirectionalWebRTCRouter extends JSONRPC.RouterBase
1818
{
1919
/**
20+
* This function must be synchronous, otherwise it will allow of race conditions where critical plugins (if any) haven't been initialized yet.
21+
*
2022
* Returns the connection ID.
2123
*
2224
* RTCDataChannel instances which will emit an error or close event will get automatically removed.
@@ -27,7 +29,7 @@ class BidirectionalWebRTCRouter extends JSONRPC.RouterBase
2729
*
2830
* @returns {number}
2931
*/
30-
async addRTCDataChannel(dataChannel)
32+
addRTCDataChannelSync(dataChannel)
3133
{
3234
if(dataChannel.readyState === "closed")
3335
{
@@ -37,7 +39,7 @@ class BidirectionalWebRTCRouter extends JSONRPC.RouterBase
3739
// @TODO: test cases for the above, somehow.
3840

3941
// "closed" would not recover and should never be added, because it would not get cleaned up.
40-
console.log("[" + process.pid + "] addRTCDataChannel ignoring closed dataChannel.");
42+
console.log("[" + process.pid + "] addRTCDataChannelSync ignoring closed dataChannel.");
4143

4244
return;
4345
}
@@ -74,6 +76,8 @@ class BidirectionalWebRTCRouter extends JSONRPC.RouterBase
7476
dataChannel.addEventListener(
7577
"error",
7678
(error) => {
79+
console.error(error);
80+
7781
this.onConnectionEnded(nConnectionID);
7882

7983
if(dataChannel.readyState === "open")

src/BidirectionalWebsocketRouter.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module.exports =
2121
class BidirectionalWebsocketRouter extends JSONRPC.RouterBase
2222
{
2323
/**
24+
* This function must be synchronous, otherwise it will allow of race conditions where critical plugins (if any) haven't been initialized yet.
25+
*
2426
* Returns the connection ID.
2527
*
2628
* WebSocket instances which will emit an error or close event will get automatically removed.
@@ -31,7 +33,7 @@ class BidirectionalWebsocketRouter extends JSONRPC.RouterBase
3133
*
3234
* @returns {number}
3335
*/
34-
async addWebSocket(webSocket)
36+
addWebSocketSync(webSocket)
3537
{
3638
if(webSocket.readyState === JSONRPC.WebSocketAdapters.WebSocketWrapperBase.CLOSED)
3739
{
@@ -41,7 +43,7 @@ class BidirectionalWebsocketRouter extends JSONRPC.RouterBase
4143
// @TODO: test cases for the above, somehow.
4244

4345
// WebSocket.CLOSED would not recover and should never be added, because it would not get cleaned up.
44-
console.log("[" + process.pid + "] addWebSocket ignoring closed webSocket.");
46+
console.log("[" + process.pid + "] addWebSocketSync ignoring closed webSocket.");
4547
return;
4648
}
4749

tests/Browser/index.html

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,13 @@
115115
ws.addEventListener("open", function(event){
116116
var wsJSONRPCRouter = new JSONRPC.BidirectionalWebsocketRouter(jsonrpcServer);
117117

118-
wsJSONRPCRouter.addWebSocket(ws)
119-
.then(function(nWebSocketConnectionID){
120-
clientOfBidirectionalWS = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
121-
122-
clientOfBidirectionalWS.rpc("ImHereForTheParty", ["Lynch", "Lynch does the harlem shake", /*bDoNotAuthorizeMe*/ false])
123-
.then(function(objTeamMember){
124-
// When calling with this exact string, the node's ping() method will call back into the JSONRPC server hosted on this page (node becomes client, browser becomes server).
125-
clientOfBidirectionalWS.rpc("ping", ["Calling from html es5 client, bidirectional websocket mode."]).then(genericTestsPromiseCatch).catch(genericTestsPromiseCatch);
126-
})
127-
.catch(genericTestsPromiseCatch)
128-
;
118+
var nWebSocketConnectionID = wsJSONRPCRouter.addWebSocketSync(ws);
119+
clientOfBidirectionalWS = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
120+
121+
clientOfBidirectionalWS.rpc("ImHereForTheParty", ["Lynch", "Lynch does the harlem shake", /*bDoNotAuthorizeMe*/ false])
122+
.then(function(objTeamMember){
123+
// When calling with this exact string, the node's ping() method will call back into the JSONRPC server hosted on this page (node becomes client, browser becomes server).
124+
clientOfBidirectionalWS.rpc("ping", ["Calling from html es5 client, bidirectional websocket mode."]).then(genericTestsPromiseCatch).catch(genericTestsPromiseCatch);
129125
})
130126
.catch(genericTestsPromiseCatch)
131127
;

tests/BrowserWebRTC/WebRTC.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474

7575
const wsJSONRPCRouter = new JSONRPC.BidirectionalWebsocketRouter(jsonrpcServer);
7676

77-
const nWebSocketConnectionID = await wsJSONRPCRouter.addWebSocket(ws);
77+
const nWebSocketConnectionID = wsJSONRPCRouter.addWebSocketSync(ws);
7878
jsonrpcClient = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
7979

8080
const objTeamMember = await jsonrpcClient.rpc("ImHereForTheParty", ["Lynch", "Lynch does the harlem shake", /*bDoNotAuthorizeMe*/ false]);
@@ -100,7 +100,7 @@
100100

101101

102102
const webRTCJSONRPCRouter = new JSONRPC.BidirectionalWebRTCRouter(jsonrpcServer);
103-
const nWebRTCConnectionID = await webRTCJSONRPCRouter.addRTCDataChannel(rtcDataChannel);
103+
const nWebRTCConnectionID = webRTCJSONRPCRouter.addRTCDataChannelSync(rtcDataChannel);
104104
jsonrpcClientRTC = webRTCJSONRPCRouter.connectionIDToSingletonClient(nWebRTCConnectionID, JSONRPC.Client);
105105

106106
console.log(await jsonrpcClientRTC.rpc("ping", ["Do you think we have a connection?"]));

0 commit comments

Comments
 (0)