sitegui / nodejs-websocket

A node.js module for websocket server and client
MIT License
736 stars 155 forks source link

Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received #24

Closed tonghae closed 8 years ago

tonghae commented 8 years ago

Greetings! Getting this on Chrome. Any workaround? Thanks.

sitegui commented 8 years ago

Hi @tonghae

I tried to reproduce this in my machine, but it worked fine (tested in current Firefox and Chrome).

Could you please provide more context on what you trying to do? A sample code that reproduces this or the error's stack trace would help.

sitegui commented 8 years ago

Since no one else reported this and I couldn't reproduce this, I'm closing this issue.

If you face this problem again, feel free to report in a new one ;)

Sinkthor commented 8 years ago

Hi I'm having the same issue... WebSocket connection to 'ws://localhost:3000/mqtt' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received

Do you have a tip?

sitegui commented 8 years ago

Hi @Sinkthor

Do you mind sharing an example of a code that triggers this error (both server and client-side)?

I recall a PR from @padarom trying to fix this, but I don't remember what happened to that. I'll look into this later tomorrow.

Sinkthor commented 8 years ago

Thank you for your help and time!! I'm using the Chrome extension -> MQTT2Chrome as my client. I think the problem might be from this code section or something relate with protocols.

var WebSockete = require('ws'); var test = new WebSockete('ws://localhost:3000/mqtt', [, 'mqtt']); var WebSocketServer = require('websocket').server; wsServer = new WebSocketServer({ ...httpServer: server}); wsServer.on('request', function(request){ ...var connection = request.accept(null, request.origin); ...console.log("CONNECTED AGAIN"); ...connection.on('message', function(message){ ......console.log('Received Message: ' +message); ......console.log('OR Received Message: ' + message.utf8Data); ......connection.send(message); ......connection.sendUTF(message.utf8Data);});});

sitegui commented 8 years ago

The issue is: you're using a feature (namely Sec-WebSocket-Protocol) not implemented by this module at this time.

It was in my mind for a while. I may take some time this week to put this together.

In fact, some discussion already took place on #30 , but the author closed the issue and I've forgot it.

Sinkthor commented 8 years ago

Hello I'm having the same problem and obviously there is a solution. 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);});
VoidVolker commented 8 years ago

Fixed: waiting for PR approving.

sitegui commented 8 years ago

I've just added support for protocol negotiation in v1.7.0, published to NPM

Thanks for all the help ;)