Skip to content

Instantly share code, notes, and snippets.

@bas-vk
Created July 13, 2016 08:58
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 bas-vk/6424621d883530f8d5f7d7beb7f7a140 to your computer and use it in GitHub Desktop.
Save bas-vk/6424621d883530f8d5f7d7beb7f7a140 to your computer and use it in GitHub Desktop.
var WebSocketClient = require('websocket').client;
var client = new WebSocketClient();
client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
client.on('connect', function(connection) {
//console.log('WebSocket Client Connected');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log(message.utf8Data);
}
});
function sendRequest() {
if (connection.connected) {
var req1 = {
id: 1,
method: 'eth_accounts',
params: []
};
var req2 = {
id: 2,
method: 'eth_blockNumber',
params: []
};
var req3 = {
id: 3,
method: 'eth_getBlockByNumber',
params: ["0x0", false]
};
var combined = JSON.stringify(req1);
combined += JSON.stringify(req2);
connection.sendUTF(combined);
connection.sendUTF(JSON.stringify(req3));
}
}
sendRequest();
});
client.connect('ws://localhost:8546/', 'echo-protocol', 'http://localhost');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment