question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

chrome 16 kills the example server

See original GitHub issue

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.<anonymous> (/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.<anonymous> (native) at Server.emit (events.js:81:20) at Socket.<anonymous> (http.js:1035:14) at Socket._onReadable (net.js:683:27) at IOWatcher.onReadable as callback

using this example code:


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?

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:30 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
Sinkthorcommented, Aug 17, 2016

Hello I’m having the same problem and obviously there is a solution. I did the things you said above and I though it was a good idea to way you try to know the protocol’s name but it appears in the console: undefined… In the code console I have this error:

C:\Users\User\Desktop\RaphaelClc\Notification\node_modules\websocket\lib\WebSocketRequest.js:289
            throw new Error('Specified protocol was not requested by the client.');
            ^
Error: Specified protocol was not requested by the client.
    at WebSocketRequest.accept (C:\Users\User\Desktop\RaphaelClc\Notification\node_modules\websocket\lib\WebSocketRequest.js:289:19)
    at WebSocketServer.<anonymous> (C:\Users\User\Desktop\RaphaelClc\Notification\bin\www:45:24)
    at emitOne (events.js:77:13)
    at WebSocketServer.emit (events.js:169:7)
    at WebSocketServer.handleUpgrade (C:\Users\User\Desktop\RaphaelClc\Notification\node_modules\websocket\lib\WebSocketServer.js:213:14)
    at emitThree (events.js:102:20)
    at Server.emit (events.js:175:7)
    at onParserExecuteCommon (_http_server.js:400:14)
    at HTTPParser.onParserExecute (_http_server.js:368:5)

In the browser’s extension develop tools I’m using (MQTT2Chrome) I have this error: WebSocket connection to 'ws://localhost:3000/mqtt' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received

I think it’s related but haven’t figured out how to solve this. Here is my critical code (especially where is in capital text):

var WebSocketServer = require('websocket').server;
wss = new WebSocketServer({
  httpServer: server});
wss.on('connect', function(connection){
  console.log('connected');
  connection.send('yo!!');});
wss.on('request', function(req){
  console.log('request');
  console.log(req.httpRequest.headers['sec-websocket-protocol']);
  VAR CONNECTION = REQ.ACCEPT('MQTT', REQ.ORIGIN);            <-----------------------
  connection.on('message', function(message){
    console.log(message.utf8Data);});
  connection.on('close', function(reasonCode, description){
    console.log('connection closed', reasonCode, description);});});
wss.on('close', function(conn, reason, description){
  console.log('closing', reason, description);});
2reactions
ibccommented, Aug 17, 2016

This is not the proper place to ask questions regarding how the WebSocket protocol (and its “subprotocol negotiation”) works. Please, read the doc:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Killing all instances of Chrome on the command-line?
Try using pkill(1). pkill chrome.
Read more >
Popup that refreshes opener kills session in Google Chrome
The solution we took was to use a user extension js function to override the page's js function that was reloading the window...
Read more >
69227 - Loading large URLs kills the renderer - chromium
I'm using this in my Blipshot Chrome Extension, and I use data URI to store temporarly the image, allowing the user to save...
Read more >
What process of Google Chrome to kill to close window from ...
To kill the entire browser, you can run killall google-chrome-stable or (in this example) kill 2706 (note how the parent PID is 1,...
Read more >
How do I kill the Chrome driver processor by using selenium
This tutorial explains how to take screen shots in Java with simple example. This example uses java.awt.Robot class to capture the screen pixels ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found