Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript socket.io从0.6升级到0.7会导致socket.request出现问题_Javascript_Cookies_Node.js_Http Headers_Socket.io - Fatal编程技术网

Javascript socket.io从0.6升级到0.7会导致socket.request出现问题

Javascript socket.io从0.6升级到0.7会导致socket.request出现问题,javascript,cookies,node.js,http-headers,socket.io,Javascript,Cookies,Node.js,Http Headers,Socket.io,我有一个使用node.js和socket.io的应用程序。三个月前它还可以正常工作,但当我现在尝试运行它时,它显示模块socket.io没有定义(不知道为什么?)。因此,我使用npm install socket.io再次安装了它。结果证明,socket.io新版本发布时带有一组不同的函数(或者至少是它的使用方式)。我已修复了更改,但仍有一些问题。以下是我的工作: 服务器: var http = require('http'); ... var server = http

我有一个使用node.js和socket.io的应用程序。三个月前它还可以正常工作,但当我现在尝试运行它时,它显示模块socket.io没有定义(不知道为什么?)。因此,我使用
npm install socket.io
再次安装了它。结果证明,socket.io新版本发布时带有一组不同的函数(或者至少是它的使用方式)。我已修复了更改,但仍有一些问题。以下是我的工作:

服务器:

    var http = require('http');
    ...
    var server = http.createServer ( function (request,response){
            console.log("Client request: " + request.url);
            ...
    });

    server.listen(port);

    var socketOptions = {
            transportOptions: {
                    'xhr-polling': {
                            closeTimeout: 12000000, //20 mins
                            timeout: 1200000  //20 mins
                    }
            }
    }

    var io = require('socket.io').listen(server,socketOptions);

    io.sockets.on("connection", function(socket) {
            console.log("WebSocket client connected");

            if (!socket.request) { // xhr polling => get cookie using websocket message
                    socket.json.send(JSON.stringify({
                            message: "resendcookie",
                            why: "Missing cookie",
                    }));
            } else {
                    console.dir(socket.request.headers.cookie);

                    ... <use the cookie> ...

            }

            io.sockets.on ("message", function(data) {
                    ...
            });

            io.sockets.on("close", function(){
                    ...
            });

            io.sockets.on("disconnect", function(){
                    ...
            });
    });
    var socket = io.connect("localhost:port", {
            rememberTransport   :  false,
            transportOptions    : {
                    "xhr-polling": {
                            closeTimeout: 600000,
                            duration: 600000
                    }
            }
    });

    ...

    function onload() {
            ...

            socket.on("connect", function(){
                    ...
            }

            socket.on("mesasge", function(data) {

                    var m =JSON.parse(data);
                    if (m.message === "resendcookie"){
                            socket.send(JSON.stringify({
                                    cookie: document.cookie,
                            }));
                    }
            });
    }  
这是对socket.io 0.6进行了轻微修改的代码,其中包含了连接和侦听websocket的新方法。我的问题是:

如果使用chrome,socket.request通常存在,因为它允许WebSocket,我可以直接访问Cookie(我使用xhr轮询技巧在不支持WebSocket的其他浏览器中访问Cookie)现在socket.request即使使用chrome也会给出不同的结果,我找不到cookies。我忘了修改其他东西了吗?如果没有,请告诉我如何到达饼干

非常感谢

沙巴

你读了吗?
它包含关于从0.6迁移到最新0.7的分配信息

感谢@3rdEden的回复。实际上,我使用这个页面进行迁移,它在客户端和服务器之间建立连接和发送消息方面帮助很大。在v0.7中,当客户端连接时发送到服务器的套接字参数不同,它不再包含包含cookie的http请求。因此,我的问题是如何读取cookies?我们删除了socket.request,因为它不可靠,在0.7中,我们引入了
socket.handshake
,其中包含我们在握手过程中收集的用户信息。您可以在此属性中找到标头、ip等。参见相关维基: