通过 WebSockets 连接到 socket.io 时如何发送 cookie? [英] How to send cookies when connecting to socket.io via WebSockets?

查看:41
本文介绍了通过 WebSockets 连接到 socket.io 时如何发送 cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与 socket.io 握手 WebSocket 连接时,有没有办法轻松传递身份验证 cookie?我目前必须单独进行,如下所示:

Is there a way to easily pass an authentication cookie when handshaking a WebSocket connection to socket.io? I currently have to do it separately, like so:

socket = new io.Socket(document.location.hostname);
socket.addEvent("connect", function()
{
    // Send PHP session ID, which will be used to authenticate
    var sessid = readCookie("PHPSESSID");
    this.send("{'action':'authenticate','sessionid':'"+sessid+"'}");
});

推荐答案

WebSockets 确实支持 cookie,因为它们基于 HTTP,但是快速浏览 Socket.IO 的源代码发现不支持此内置

WebSockets do have support for cookies since they are based on HTTP, but a quick browse through the source of Socket.IO revealed that there is no support for this built in.

因此,在这种情况下,直接使用 cookie 不是可行的解决方案,而且,由于您使用的是 Socket.IO,因此不能保证用户实际上会通过 WebSocket 进行连接.

So using cookies directly is not a feasible solution in this case, also, since you're using Socket.IO, it's not guaranteed that users will actually connect via a WebSocket.

如果连接使用flash socket,真的很难让Flash发送浏览器的cookie而不是自己设置的,所以即使你直接发送cookie,它也不会在浏览器中设置烧瓶套接字连接的情况.

In the case that a connection uses a flash socket, it's really hard to make Flash send the Browser's cookies instead of it's own set, so even if you would send a cookie directly, it wouldn't get set in the Browser in case of a flask socket connection.

目前 Socket.IO 中不支持此功能,因此闪存套接字将失败.

Currently there's no support for this built into Socket.IO, so flash sockets will just fail.

您可以在这个问题中了解相关信息,这里有一个问题关于 flash cookie 问题.

You can read about that in this issue, and here's a question about the flash cookie problem.

最好的解决方案仍然是让它成为你自己协议的一部分.

Best solution is still to make it part of your own protocol.

这篇关于通过 WebSockets 连接到 socket.io 时如何发送 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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