Created
March 19, 2018 22:31
-
-
Save anonymous/7c169e995d13a943d3f494c88cac7a17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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