I think this is what you're looking for. Quick example using your server code:
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 + "'");
}
});
connection.sendUTF("Hello world");
});
client.connect('ws://127.0.0.1:8082/echo', "", "http://localhost:8082");
To get this to work, you'll need to modify the WebsocketClient code in lib/WebSocketCLient.js
. Comment these lines out (lines 299-300 on my machine):
//this.failHandshake("Expected a Sec-WebSocket-Protocol header.");
//return;
For some reason the websocket library you provided doesn't seem to send the "Sec-Websocket-Protocol" header, or at least the client doesn't find it. I haven't done too much testing, but a bug report should probably be filed somewhere.
Here's an example using a Go client:
package main
import (
"fmt"
"code.google.com/p/go.net/websocket"
)
const message = "Hello world"
func main() {
ws, err := websocket.Dial("ws://localhost:8082/echo", "", "http://localhost:8082")
if err != nil {
panic(err)
}
if _, err := ws.Write([]byte(message)); err != nil {
panic(err)
}
var resp = make([]byte, 4096)
n, err := ws.Read(resp)
if err != nil {
panic(err)
}
fmt.Println("Received:", string(resp[0:n]))
}