来自socket.io的新的cookie值不工作? [英] new cookie value from socket.io doesn't work?

查看:131
本文介绍了来自socket.io的新的cookie值不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用socket.io id多个浏览器窗口/页。
它是使用 everyauth 的SNS身份验证过程,该项目基于 express



这是我的项目设计:



一个完整的ajax主页维护一个socket.io连接。无重新加载,无重定向等。



当用户尝试通过某个SNS帐户登录时,将打开一个弹出窗口,该窗口将由everyauth和SNS Auth管理。在弹出窗口中,发生很多重定向,但是主窗口是持久的,因为socket.io连接。



最后,成功授权后,everyauth重定向'authdone



在浏览器上,此弹出窗口不再有用,因此window.close();



但是,成功的登录信息可以在整个everyauth获得,但在我的情况下,cookie更重要,因为我不需要一个明确的框架会话管理,因为我打算做的是通过socket.IO 进行会话管理。

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

目前,我设法通过cookie在重定向到着陆页的弹出窗口中获取express sessionID(/ public / authdone.html),但是,这还不够。 我还需要在cookie中获取一个基于socket.IO的ID。



其实,我在这里感到困惑,并提出了一个问题。
在Socket.IO上添加cookie值

感谢Wes,我稍微找到一种方法来添加一个新的cookie值(在我的case,一些套接字客户端ID)如下:

  io.configure(function()
{
io.set('authorization',function(handshake,callback)
{
if(handshake.headers.cookie)
{
var cookie = handshake.headers.cookie;
var newS =;
+socketClientID =
+999999999999999; //最终为每个客户端随机化
cookie + = newS;
console.log(============== ======================= =);
console.log(cookie);
} else
{
return callback('Cookie not found',false);
}
callback(null,true);
});

在此块中,我添加一个cookie值为
socketClientID = 999999999999999 p>

因此,通过console.log输出的当前cookie是

  connect.sid = fc2iTJW3ETD7nIyxI5ZwpayK.S0qGhp1Ryw7Msg2r03yB6g822qVZIZTbZyWUSpQL0aU; 
socketClientID = 999999999999999

到目前为止很好,但是在everyauth重定向页面,
我还是得到

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

只能通过cookie获取表达sessionID(connect.sid),在socket.IO握手阶段添加的clientID cookie值没有反映。



我真的需要帮助。感谢。

解决方案

好的,我发现我需要在客户端配置这种类型的cookie。
$ b

从io.sockets.on('connection',function(client)...>发出socketClientID ...





从客户端发出并回调socketClientID。



我使用
https://github.com/carhartl/jquery-cookie
用于客户端Cookie管理。


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.

解决方案

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.

这篇关于来自socket.io的新的cookie值不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆