JavaScript WebSocket connection readyState = 0. Connection to server can not be established

I have some code for Node.js that I have been running with node.exe that looks like this:

// http://www.zhihua-lai.com/acm
// 09-Feb-2013

var sys = require('sys');
var net = require('net');
var sockets = [];

var svr = net.createServer(function(sock) {
    sys.puts('Connected: ' + sock.remoteAddress + ':' + sock.remotePort); 
    sock.write('Hello ' + sock.remoteAddress + ':' + sock.remotePort + '\n');
    sockets.push(sock);

    sock.on('data', function(data) {  // client writes message
        if (data == 'exit\n') {
            sys.puts('exit command received: ' + sock.remoteAddress + ':' + sock.remotePort + '\n');
            sock.destroy();
            var idx = sockets.indexOf(sock);
            if (idx != -1) {
                delete sockets[idx];
            }
            return;
        }
        var len = sockets.length;
        for (var i = 0; i < len; i ++) { // broad cast
            if (sockets[i] != sock) {
                if (sockets[i]) {
                    sockets[i].write(sock.remoteAddress + ':' + sock.remotePort + ':' + data);
                }
            }
        }
    });

    sock.on('end', function() { // client disconnects
        sys.puts('Disconnected: ' + data + data.remoteAddress + ':' + data.remotePort + '\n');
        var idx = sockets.indexOf(sock);
        if (idx != -1) {
            delete sockets[idx];
        }
    });
});

var svraddr = '127.0.0.1';
var svrport = 8080;

svr.listen(svrport, svraddr);
sys.puts('Server Created at ' + svraddr + ':' + svrport + '\n');

Once I have the server running I can open two more terminal clients and use NetCat or telnet. When I type information into either of the client windows the information is broadcast to both NetCat windows. The trouble I am having is when I try to create a JavaScript connection to the server.

<!DOCTYPE html>
<html>
<head>
<title>sample</title>
<script type="text/javascript">
var connection = new WebSocket('ws://localhost:8080');
console.log(connection);
// When the connection is open, send some data to the server
connection.onopen = function () {
    connection.send("a"); // Send the message 'Ping' to the server
    console.log("send data...");
};
</script>
<body>
</body>
</html>

What happens is that the readyState of connection is 0 indicating that Google Chromes JavaScript could not open a connection to the server. I have made sure that Windows Firewall is off to avoid any connection troubles and the server is working fine with NetCat. I would like to take Nodejs further now and get JavaScript to connect to the server but I'm not sure yet how that is going to happen or what needs to change with the code.

Thanks in advance...

While websockets use tcp/ip, a websocket server is not just a standard tcp server.

RFC6455 defines the websocket protocol which clients and servers must use. To open a connection, see the handshaking section. To later send/receive messages see the data framing section.

You'll need to either implement your own server which implements this protocol or find an existing third party server you can integrate. If you want to consider existing servers, ws looks promising.