Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env node
// npm install websocket@1.0.25 --save
const WebSocketClient = require('websocket').client;
const client = new WebSocketClient();
let lastPing = new Date().getTime();
client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
client.on('connect', function(connection) {
console.log('Connected to Server...');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('Connection Closed');
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log(message.utf8Data);
}
});
connection.on('pong', function(){
console.log('[pingpong] response took', (new Date().getTime() - lastPing) + 'ms');
})
function send(message) {
if (connection.connected) {
connection.sendUTF(message);
}
}
// Send a ping every 10s
// to keep the connection live
setInterval(function(){
lastPing = new Date().getTime();
connection.ping();
}, 10000);
});
client.connect('wss://ws.radarrelay.com/0x/v0/ws');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.