Worlize/WebSocket-Node — Issue #32: chrome 16 kills the example server
Author: timglabisch
Created: 2012-01-30 00:10:24+00:00
State: open

if i run in the chrome console:

> websocket = new WebSocket('ws://****.l:8081/echo-protocol');
> WebSocket
> Unexpected response code: 500

the server just crashes with this exception:

> Wed Jan 25 2012 22:12:42 GMT-0800 (PST) Server is listening on port 8080
>
> /home/dev/.node_libraries/.npm/websocket/1.0.4/package/lib/WebSocketRequest.js:240
> throw new Error("Specified protocol was not requested by the clien
> ^
> Error: Specified protocol was not requested by the client.
> at WebSocketRequest.accept (/home/dev/.node_libraries/.npm/websocket/1.0.4/package/lib/WebSocketRequest.js:240:19)
> at WebSocketServer. (/var/www/pimcore-current/website/lib/socket_server.js:36:30)
> at WebSocketServer.emit (events.js:81:20)
> at WebSocketServer.handleUpgrade (/home/dev/.node_libraries/.npm/websocket/1.0.4/package/lib/WebSocketServer.js:179:14)
> at Server. (native)
> at Server.emit (events.js:81:20)
> at Socket. (http.js:1035:14)
> at Socket._onReadable (net.js:683:27)
> at IOWatcher.onReadable [as callback] (net.js:177:10)

using this example code:

```javascript

var WebSocketServer = require('websocket').server;
var http = require('http');

var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8081, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});

wsServer = new WebSocketServer({
httpServer: server,
// You should not use autoAcceptConnections for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});

function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}

wsServer.on('request', function(request) {
if (!originIsAllowed(request.origin)) {
// Make sure we only accept requests from an allowed origin
request.reject();
console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
return;
}

var connection = request.accept('echo-protocol', request.origin);
console.log((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received Message: ' + message.utf8Data);
connection.sendUTF(message.utf8Data);
}
else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes(message.binaryData);
}
});
connection.on('close', function(reasonCode, description) {
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});
```

any ideas?