ws.upgradeReq is undefined

Created on 17 May 2017  路  5Comments  路  Source: websockets/ws

Hello,

I am using the WS module, version 3.0.0. When I connect to the server I created, upgradeReq is undefined.

var url = ws.upgradeReq.url;
                           ^

TypeError: Cannot read property 'url' of undefined
    at Server.onWebSocketOpen (/home/admin/test/src/Server.js:626:28)
    at emitTwo (events.js:106:13)
    at WebSocketServer.emit (events.js:192:7)
    at handleUpgrade (/home/admin/test/node_modules/ws/lib/WebSocketServer.js:88:16)
    at WebSocketServer.completeUpgrade (/home/admin/test/node_modules/ws/lib/WebSocketServer.js:270:5)
    at WebSocketServer.handleUpgrade (/home/admin/test/node_modules/ws/lib/WebSocketServer.js:197:10)
    at Server.WebSocketServer._ultron.on (/home/admin/test/node_modules/ws/lib/WebSocketServer.js:87:14)
    at emitThree (events.js:116:13)
    at Server.emit (events.js:195:7)
    at onParserExecuteCommon (_http_server.js:462:14)

I am creating the server like this:

    var httpServer = http.createServer();

    this.wsServer = new this.WebSocket.Server({
        server: httpServer, 
        perMessageDeflate: false,
        maxPayload: 4096
    });
    this.wsServer.on('error', this.onWebSocketError.bind(this));
    this.wsServer.on('connection', this.onWebSocketOpen.bind(this));
    httpServer.listen(3000, '0.0.0.0', this.onHttpServerOpen.bind(this));

Maybe I am missing something?

Most helpful comment

@Droppers in the definition of your onWebSocketOpen you can add the req object and you can upgrade to version 3 if you want.

Thing.prototype.onWebSocketOpen = function onWebSocketOpen(ws, req) {
  ws.upgradeReq = req;
  // ...
}

All 5 comments

Oh, I just noticed version 3.0.0 does not have upgradeReq anymore https://github.com/websockets/ws/pull/1099.

That is really a major breaking change..

It's one of the breaking changes of version 3. See discussion in #1099 and release notes.

That is unfortunate, I will use version 2.3.1 then.

Thanks.

I'll reopen so ppl hopefully see this and don't open new issues for this.

@Droppers in the definition of your onWebSocketOpen you can add the req object and you can upgrade to version 3 if you want.

Thing.prototype.onWebSocketOpen = function onWebSocketOpen(ws, req) {
  ws.upgradeReq = req;
  // ...
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nabeelio picture nabeelio  路  5Comments

robertmylne picture robertmylne  路  3Comments

quesurifn picture quesurifn  路  3Comments

NodePing picture NodePing  路  5Comments

pmcalabrese picture pmcalabrese  路  4Comments