开发者

Can I access a cookie from Socket.io?

I want to set some user information in a cookie an开发者_开发问答d be able to access it on connection, is this possible?


Using Socket.IO 0.8.7, you can access request headers via:

socket.handshake.headers

You can find more detail on this at https://github.com/LearnBoost/socket.io/wiki/Authorizing


client.request.headers.cookie leads to race conditions as the cookie always points to the last logged in user.

Please see: Socket.IO Authentication.


I got it, this works:

client.request.headers.cookie


After looking to the engine.io source code you can set cookies using:

var io = require('socket.io')(3000);

io.use((socket, next) => {
    socket.conn.transport.once('headers', (headers) => {

        headers['set-cookie'] ="sess=test;"; });

    next();
});

this code could conflict with the engine.io code that set the sid cookie. as both HTTP/1.1 and HTTP/2 headers are case-insensitive and engine.io use 'Set-Cookie' in the headers object adding a lowercase object name 'set-cookie' would avoid this problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜