Last active
August 29, 2015 14:07
-
-
Save irisli/7f45b8f5a597970c42cc to your computer and use it in GitHub Desktop.
live.stellard.org timeout test
This file contains hidden or 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
var WebSocket = require('ws') | |
, ws = new WebSocket('ws://live.stellar.org:9001'); | |
var start = null; | |
var accounts = ['gDSSa75HPagWcvQmwH7D51dT5DPmvsKL4q']; | |
ws.on('open', function() { | |
console.log('WebSocket connection opened.'); | |
start = new Date(); | |
setInterval(function() { | |
console.log(secondsSinceStart()); | |
console.log('Ping sent'); | |
ws.send(JSON.stringify({ "command" : "ping" })); | |
}, 1000); | |
console.log('Subscribing to ' + accounts); | |
console.log(JSON.stringify({ "command" : "subscribe", "accounts" : accounts })); | |
ws.send(JSON.stringify({ "command" : "subscribe", "accounts" : accounts })); | |
ws.send(JSON.stringify({ "command" : "server_info" })); | |
}); | |
ws.on('message', function(message) { | |
var parsed = JSON.parse(message); | |
if (typeof parsed.ledger_index !== 'undefined' && typeof parsed.transaction !== 'undefined') { | |
console.log('Received WS response. Ledger index: ' + parsed.ledger_index + '. ' + parsed.transaction.TransactionType + '@' + parsed.transaction.Destination); | |
} else if (typeof parsed.result !== 'undefined' && | |
typeof parsed.result.info !== 'undefined' && | |
typeof parsed.result.info.hostid !== 'undefined') { | |
console.log('hostid: ' + parsed.result.info.hostid); | |
} else if (message == '{"result":{},"status":"success","type":"response"}\n') { | |
console.log('Ping returned'); | |
} else { | |
console.log('received: %s', message); | |
} | |
}); | |
function secondsSinceStart() { | |
if (!start) { | |
return 0; | |
} | |
return Math.round(((new Date()) - start)/1000); | |
} | |
ws.on('close', function() { | |
console.log('WS disconnected at ' + secondsSinceStart() + ' seconds'); | |
process.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment