Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs - how to get cookie in ws (einaros)

In socketio, I can easily get cookie with socket.handshake.headers.cookie. How could I do the same thing in ws?

like image 433
user3828771 Avatar asked Mar 13 '26 04:03

user3828771


2 Answers

You can try this one: ws.upgradeReq.headers.cookie.

like image 106
Lewis Avatar answered Mar 15 '26 17:03

Lewis


I was using client.upgradeReq.headers.cookie, but that broke suddenly. I found cookies at request.headers.cookie:

wss.on('connection', function connection(client, request))
{
  ...
  var cookies = {};
  //Not working any more
  //if(client.upgradeReq.headers.cookie) request.headers.cookie.split(';')...
  //This works
  if(request.headers.cookie) request.headers.cookie.split(';').forEach(function(cookie)
  {
    var parts = cookie.match(/(.*?)=(.*)$/);
    var name = parts[1].trim();
    var value = (parts[2] || '').trim();
    cookies[ name ] = value;
  });
  ...
like image 25
Jon Hulka Avatar answered Mar 15 '26 18:03

Jon Hulka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!