当通过WebSockets连接到socket.io时如何发送cookie? [英] How to send cookies when connecting to socket.io via WebSockets?
问题描述
有没有办法轻松传递验证cookie当握手WebSocket连接到socket.io?我现在必须单独做,像这样:
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.
所以直接使用cookies不是一个可行的解决方案case,同样,因为你使用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发送浏览器的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内置的内存,所以flash套接字只会失败。
Currently there's no support for this built into Socket.IO, so flash sockets will just fail.
你可以在< a href =https://github.com/LearnBoost/Socket.IO-node/issues#issue/101>此问题,这里有一个关于 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屋!