Skip to content

Instantly share code, notes, and snippets.

Created March 19, 2018 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7c169e995d13a943d3f494c88cac7a17 to your computer and use it in GitHub Desktop.
Save anonymous/7c169e995d13a943d3f494c88cac7a17 to your computer and use it in GitHub Desktop.
#!/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