uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
8.08k stars 574 forks source link

On upgrade websockets how can to send additional headers? #720

Closed polearnik closed 2 years ago

polearnik commented 2 years ago

I Try next code but upgrade is not sended

upgrade: (res, req, context) => {
    console.log('An Http connection wants to become WebSocket, URL: ' + req.getUrl() + '!');
res.writeHeader('Test',123)
    /* This immediately calls open handler, you must not use res after this call */
    res.upgrade({
        url: req.getUrl()
      },
      /* Spell these correctly */
      req.getHeader('sec-websocket-key'),
      req.getHeader('sec-websocket-protocol'),
      req.getHeader('sec-websocket-extensions'),
      context);

  },
e3dio commented 2 years ago

Add res.writeStatus('101') before adding any headers in upgrade handler, see https://github.com/uNetworking/uWebSockets.js/discussions/331 Also you are writing a number 123 instead of a string, need a string