new cookie value from socket.io doesn't work?

2019-07-04 05:29发布

问题:

I try to use socket.io id for multiple browser Window/Page. It's for SNS-auth-process using everyauth, and the project is based on express.

Here is my project design:

A full ajax main page maintaining a socket.io connection. No reload, no redirect etc.

When a user try to login via some SNS account, a popup window opens, which will be managed by everyauth and SNS Auth. Inside the popup window, many redirects occur, but the main window is persistent, so as the socket.io connection.

Finally, after the successful authorization, everyauth redirects 'authdone.html' in the popup window.

On the browser, this popup window is no longer useful, so window.close();

but, a successful login info can be obtained throughout the everyauth, but in my case, cookie is more important since I don't exactly need a session management with express framework because what I intend to do is session management through socket.IO.

app.get('*',
    function (req, res)
    { 
        console.log("----------get------------");
        console.log(req.url);
        if (req.url == '/public/authdone.html')
        {
            console.log("----------get /public/authdone.html------------");
            console.log(req.cookies);  
            // express(connect) sessionID(sid) cookie available here.
            //{ 'connect.sid': '2DLlayNx90wtTsyeq5w83N9g.Kc1ZFd8I7omf6u4fk4FFyKAt4dP1V6IufSf1ZtRudVI' }
        }
        onreq(req, res);
    });

Currently, I managed to obtain express sessionID via cookie on the popup window redirected to the landing page(/public/authdone.html), however, this is not enough. I also need to obtain a socket.IO oriented ID here in the cookie.

Actually, I confused here, and asked a question. Adding a cookie value on Socket.IO

Thanks to Wes, I slightly find a way to add a new cookie value(in my case, some socket client ID) as below:

io.configure(function ()
{
    io.set('authorization', function (handshake, callback)
    {
        if (handshake.headers.cookie)
        {
            var cookie = handshake.headers.cookie;
            var newS = ";" 
                + "socketClientID" + "=" 
                + "999999999999999";// eventually randomize for every client
            cookie += newS;
            console.log("=========================================");
            console.log(cookie);  
        } else
        {
            return callback('Cookie not found', false);
        }
        callback(null, true);
    });
});

In this block, I add a cookie value as "socketClientID=999999999999999"

So the current cookie output via console.log is

connect.sid=fc2iTJW3ETD7nIyxI5ZwpayK.S0qGhp1Ryw7Msg2r03yB6g822qVZIZTbZyWUSpQL0aU;
socketClientID=999999999999999

So far so good, however, on the everyauth redirected page, I still get

----------get /public/authdone.html------------  
{ 'connect.sid': 'fc2iTJW3ETD7nIyxI5ZwpayK.S0qGhp1Ryw7Msg2r03yB6g822qVZIZTbZyWUSpQL0aU' }

only express sessionID(connect.sid) can be obtained via the cookie, added clientID cookie value on the socket.IO handshake phase is not reflected.

I really need a help. thanks.

回答1:

Ok, I found I need to configure this kind of cookie form the client side.

emit socketClientID from io.sockets.on('connection', function (client)...

Or

emit from Client and callback the socketClientID.

I use https://github.com/carhartl/jquery-cookie for client Cookie management.