Created
January 6, 2014 20:43
-
-
Save mrunalp/8289535 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 | |
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