| 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/greeter', 'echo-protocol'); |
| |
| |
| |
| /*var W3CWebSocket = require('websocket').w3cwebsocket; |
| var client = new W3CWebSocket('ws://localhost:8080/', 'echo-protocol'); |
| |
| client.onerror = function() { |
| console.log('Connection Error'); |
| }; |
| |
| client.onopen = function() { |
| console.log('WebSocket Client Connected'); |
| |
| function sendNumber() { |
| if (client.readyState === client.OPEN) { |
| var number = Math.round(Math.random() * 0xFFFFFF); |
| client.send(number.toString()); |
| setTimeout(sendNumber, 1000); |
| } |
| } |
| sendNumber(); |
| }; |
| |
| client.onclose = function() { |
| console.log('echo-protocol Client Closed'); |
| }; |
| |
| client.onmessage = function(e) { |
| if (typeof e.data === 'string') { |
| console.log("Received: '" + e.data + "'"); |
| } |
| }; |
| */ |