Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot set property 'readyState' of undefined (when using upgrade listener) #1660

Closed
1 task done
Siedlerchr opened this issue Nov 12, 2019 · 2 comments
Closed
1 task done

Comments

@Siedlerchr
Copy link

  • I've searched for any related issues and avoided creating a duplicate
    issue.

I found the other issues #1591 and #1564 but I think the root cause here is different, althought he error is the same. Or at least the "if" clause has been removed again, which makes this error occur again.

Description

When I add the following snippet for attaching an upgrade listener for the connection, I encounter the following error message. I could narrow down the error to the upgrade request.

TypeError: Cannot set property 'readyState' of undefined
    at Socket.socketOnClose (<apppath>\node_modules\ws\lib\websocket.js:837:24)
    at Socket.emit (events.js:203:15)
    at TCP._handle.close (net.js:606:12)
    server.on('upgrade', (req, socket, head) => {
        if (true) {
            wss.handleUpgrade(req, socket, head, (ws) => {
                wss.emit('connection', ws, req);
            });
        }
    }); 

Reproducible in:

  • version: ws 7.2.0
  • Node.js version(s): 10.16.2
  • OS version(s): Windows 10

Steps to reproduce:

  1. Create a node js express http and websocket server with clientTracking = true
  2. Run/Execute
  3. connect to the socket using the testscript.
    See error

Expected result:

No error

Actual result:

Error from above is shown

Attachments:

Test html +js for connecting to the websocket server.

websockettest.html.txt

@lpinca
Copy link
Member

lpinca commented Nov 12, 2019

I can't reproduce:

'use strict';

const http = require('http');
const WebSocket = require('ws');

const data = `<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <script>
      (function () {
        var ws = new WebSocket('ws://localhost:8080');

        ws.onmessage = function(evt) {
          console.log(evt.data);
        };
      })();
    </script>
  </body>
</html>`;

const server = http.createServer();
const wss = new WebSocket.Server({ noServer: true });

wss.on('connection', function(ws) {
  ws.send('Hello');
});

server.on('request', (req, res) => {
  res.setHeader('Content-Type', 'text/html');
  res.end(data);
});

server.on('upgrade', (req, socket, head) => {
  if (true) {
    wss.handleUpgrade(req, socket, head, (ws) => {
      wss.emit('connection', ws, req);
    });
  }
});

server.listen(8080, () => {
  console.log('Listening on *:8080');
});

Make sure to specify the noServer option (new WebSocket.Server({ noServer: true })) and do not set the server option if you are using this pattern.

@Siedlerchr
Copy link
Author

Thanks for the quick response! Indeed, the noServer=true option fixed the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants