Skip to content

Commit

Permalink
Replace websocket dependency with ws (#2589)
Browse files Browse the repository at this point in the history
* Replace `websocket` dependency with `ws`

* CHANGELOG
  • Loading branch information
tomaka authored Aug 8, 2022
1 parent a06fdec commit 7c45f55
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 261 deletions.
4 changes: 4 additions & 0 deletions bin/wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
- The GRANDPA warp sync implementation has been considerably refactored. It is possible that unintended changes in behaviour have accidentally been introduced. ([#2578](https://github.com/paritytech/smoldot/pull/2578))
- A warning is now printed if the `badBlocks` field in a chain specification is not empty. Bad blocks are not supported by the smoldot light client. ([#2585](https://github.com/paritytech/smoldot/pull/2585))

### Fix

- Fix WebSockets not working in the CommonJS bindings for NodeJS due to a problematic import. ([#2589](https://github.com/paritytech/smoldot/pull/2589)).

## 0.6.27 - 2022-07-29

### Changed
Expand Down
51 changes: 22 additions & 29 deletions bin/wasm-node/javascript/demo/demo.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
// This file launches a WebSocket server that exposes JSON-RPC functions.

import * as smoldot from '../dist/mjs/index-nodejs.js';
import { default as websocket } from 'websocket';
import * as http from 'node:http';
import { WebSocketServer } from 'ws';
import * as process from 'node:process';
import * as fs from 'node:fs';

Expand Down Expand Up @@ -86,36 +85,29 @@ client
});

// Start the WebSocket server listening on port 9944.
let server = http.createServer(function (_request, response) {
response.writeHead(404);
response.end();
});
server.listen(9944, function () {
console.log('JSON-RPC server now listening on port 9944');
console.log('Please visit one of:');
for (const chainId in chainSpecsById) {
console.log('- ' + chainId + ': https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944%2F' + chainId);
}
console.log('');
});
let wsServer = new websocket.server({
httpServer: server,
autoAcceptConnections: false,
let wsServer = new WebSocketServer({
port: 9944
});

wsServer.on('request', function (request) {
console.log('JSON-RPC server now listening on port 9944');
console.log('Please visit one of:');
for (const chainId in chainSpecsById) {
console.log('- ' + chainId + ': https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944%2F' + chainId);
}
console.log('');

wsServer.on('connection', function (connection, request) {
// Received a new incoming WebSocket connection.

// Note that we don't care too much about sanitizing input as this is just a demo.
const chainCfg = chainSpecsById[request.resource.substring(1)];
const chainCfg = chainSpecsById[request.url.substring(1)];

if (!chainCfg) {
request.reject(404);
connection.close();
return;
}

const connection = request.accept(request.requestedProtocols[0], request.origin);
console.log('(demo) New JSON-RPC client connected: ' + request.remoteAddress + '.');
console.log('(demo) New JSON-RPC client connected: ' + request.socket.remoteAddress + '.');

// Start loading the chain.
let chain = (async () => {
Expand All @@ -130,7 +122,7 @@ wsServer.on('request', function (request) {
const para = await client.addChain({
chainSpec: chainCfg.chainSpec,
jsonRpcCallback: (resp) => {
connection.sendUTF(resp);
connection.send(resp);
},
potentialRelayChains: [relay]
});
Expand All @@ -141,7 +133,7 @@ wsServer.on('request', function (request) {
relay: await client.addChain({
chainSpec: chainCfg.chainSpec,
jsonRpcCallback: (resp) => {
connection.sendUTF(resp);
connection.send(resp);
},
})
};
Expand All @@ -152,14 +144,15 @@ wsServer.on('request', function (request) {
});

// Receiving a message from the connection. This is a JSON-RPC request.
connection.on('message', function (message) {
if (message.type === 'utf8') {
connection.on('message', function (data, isBinary) {
if (!isBinary) {
const message = data.toString('utf8');
chain
.then(chain => {
if (chain.para)
chain.para.sendJsonRpc(message.utf8Data);
chain.para.sendJsonRpc(message);
else
chain.relay.sendJsonRpc(message.utf8Data);
chain.relay.sendJsonRpc(message);
})
.catch((error) => {
console.error("(demo) Error during JSON-RPC request: " + error);
Expand All @@ -172,7 +165,7 @@ wsServer.on('request', function (request) {

// When the connection closes, remove the chains that have been added.
connection.on('close', function (reasonCode, description) {
console.log("(demo) JSON-RPC client " + connection.remoteAddress + ' disconnected.');
console.log("(demo) JSON-RPC client " + request.socket.remoteAddress + ' disconnected.');
chain.then(chain => {
chain.relay.remove();
if (chain.para)
Expand Down
Loading

0 comments on commit 7c45f55

Please sign in to comment.