Skip to content

Instantly share code, notes, and snippets.

@mrunalp
Created January 6, 2014 20:43
Show Gist options
  • Save mrunalp/8289535 to your computer and use it in GitHub Desktop.
Save mrunalp/8289535 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
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("Received: '" + message.utf8Data + "'");
}
});
function sendNumber() {
if (connection.connected) {
var number = Math.round(Math.random() * 0xFFFFFF);
connection.sendUTF(number.toString());
setTimeout(sendNumber, 1000);
}
}
sendNumber();
});
//client.connect('ws://localhost:8080/', 'echo-protocol');
var uri = process.argv[2];
console.log("Connecting to " + uri);
client.connect(uri, 'echo-protocol');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment