In socketio, I can easily get cookie with socket.handshake.headers.cookie. How could I do the same thing in ws?
You can try this one: ws.upgradeReq.headers.cookie.
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;
});
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With