Skip to content

Commit ab18f00

Browse files
author
ionut.stan
committed
Reduced browser dist js file size by > 4x.
1 parent 62b14f1 commit ab18f00

File tree

10 files changed

+5690
-28901
lines changed

10 files changed

+5690
-28901
lines changed

README.MD

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const WebSocketServer = WebSocket.Server;
199199
```
200200
201201
202-
__Site A. WebSocket server (accepts incoming TCP connections):__
202+
__Site A. WebSocket server (accepts incoming TCP connections), JSONRPC server & client:__
203203
204204
```JavaScript
205205
const jsonrpcServer = new JSONRPC.Server();
@@ -224,12 +224,14 @@ webSocketServer.on(
224224
{
225225
const nWebSocketConnectionID = await wsJSONRPCRouter.addWebSocket(webSocket);
226226
// Do something with nWebSocketConnectionID and webSocket here, like register them as a pair with an authorization plugin.
227+
228+
// const clientForThisConnection = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
227229
}
228230
);
229231
```
230232
231233
232-
__Site B. WebSocket client (connects to the above WebSocket TCP server):__
234+
__Site B. WebSocket client (connects to the above WebSocket TCP server), JSONRPC server & client:__
233235
234236
```JavaScript
235237
const jsonrpcServer = new JSONRPC.Server();
@@ -258,3 +260,113 @@ const theOnlyClient = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketCo
258260

259261
await theOnlyClient.divide(3, 2);
260262
```
263+
264+
265+
__Site B (ES5 browser). WebSocket client (connects to the above WebSocket TCP server), JSONRPC server & client:__
266+
```html
267+
<!doctype html>
268+
<html>
269+
<head>
270+
<title>Tests</title>
271+
272+
<!-- These are external to jsonrpc.min.js intentionally, to reduce file size and reuse them for other libraries. -->
273+
<script type="text/javascript" src="/node_modules/babel-polyfill/dist/polyfill.min.js"></script>
274+
<script type="text/javascript" src="/node_modules/whatwg-fetch/fetch.js"></script>
275+
<script type="text/javascript" src="/node_modules/es6-promise/dist/es6-promise.auto.min.js"></script>
276+
277+
<script type="text/javascript" src="/node_modules/regenerator-runtime/runtime.js"></script>
278+
<script type="text/javascript" src="/builds/browser/es5/jsonrpc.min.js"></script>
279+
280+
<script>
281+
function TestEndpoint()
282+
{
283+
JSONRPC.EndpointBase.prototype.constructor.apply(
284+
this,
285+
[
286+
/*strName*/ "Test",
287+
/*strPath*/ location.protocol + "//" + location.host + "/api",
288+
/*objReflection*/ {},
289+
/*classReverseCallsClient*/ JSONRPC.Client
290+
]
291+
);
292+
}
293+
294+
TestEndpoint.prototype = new JSONRPC.EndpointBase("TestEndpoint", "/api", {});
295+
TestEndpoint.prototype.constructor = JSONRPC.EndpointBase;
296+
297+
TestEndpoint.prototype.ping = function(incomingRequest, strReturn){
298+
return new Promise(function(fnResolve, fnReject){
299+
fnResolve(strReturn);
300+
});
301+
};
302+
303+
304+
var client;
305+
var clientWS;
306+
var clientOfBidirectionalWS;
307+
308+
309+
function testSimpleClient()
310+
{
311+
client = new JSONRPC.Client("http://" + location.host + "/api");
312+
client.rpc("ping", ["Calling from html es5 client, http transport."]).then(genericTestsPromiseCatch).catch(genericTestsPromiseCatch);
313+
314+
315+
316+
clientWS = new JSONRPC.Client("http://" + location.host + "/api");
317+
318+
var ws = new WebSocket("ws://" + location.host + "/api");
319+
clientWS.addPlugin(new JSONRPC.Plugins.Client.WebSocketTransport(ws, /*bBidirectionalWebSocketMode*/ false));
320+
321+
ws.addEventListener("open", function(event){
322+
client.rpc("ping", ["Calling from html es5 client, websocket transport."])
323+
.then(console.log)
324+
.catch(console.log)
325+
;
326+
});
327+
}
328+
329+
330+
function testBidirectionalRPC()
331+
{
332+
var jsonrpcServer = new JSONRPC.Server();
333+
jsonrpcServer.registerEndpoint(new TestEndpoint());
334+
335+
// By default, JSONRPC.Server rejects all requests as not authenticated and not authorized.
336+
jsonrpcServer.addPlugin(new JSONRPC.Plugins.Server.AuthenticationSkip());
337+
jsonrpcServer.addPlugin(new JSONRPC.Plugins.Server.AuthorizeAll());
338+
339+
var ws = new WebSocket("ws://" + location.host + "/api");
340+
341+
ws.addEventListener("open", function(event){
342+
var wsJSONRPCRouter = new JSONRPC.BidirectionalWebsocketRouter(jsonrpcServer);
343+
344+
wsJSONRPCRouter.addWebSocket(ws)
345+
.then(function(nWebSocketConnectionID){
346+
clientOfBidirectionalWS = wsJSONRPCRouter.connectionIDToSingletonClient(nWebSocketConnectionID, JSONRPC.Client);
347+
348+
clientOfBidirectionalWS.rpc("ping", ["Calling from html es5 client, websocket transport with bidirectional JSONRPC."])
349+
.then(console.log)
350+
.catch(console.log)
351+
;
352+
})
353+
.catch(console.log)
354+
;
355+
});
356+
}
357+
358+
359+
window.addEventListener(
360+
"load",
361+
function(event){
362+
testSimpleClient();
363+
testBidirectionalRPC();
364+
}
365+
);
366+
</script>
367+
</head>
368+
<body>
369+
Open the developer tools console (F12 for most browsers, CTRL+SHIFT+I in Electron) to see errors or manually make calls.
370+
</body>
371+
</html>
372+
```

builds/browser/es5/jsonrpc.min.js

Lines changed: 2 additions & 2 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.

0 commit comments

Comments
 (0)