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

发布于 2024-11-25 17:57:39 字数 2782 浏览 2 评论 0原文

我有一个使用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 正常工作的代码进行了稍微修改,以包含连接和监听的新方式网络套接字。我的问题是:

如果使用chrome,socket.request曾经存在,因为它允许websockets,并且我可以直接访问cookie(我用来在不支持websockets的其他浏览器中访问cookie的xhr轮询技巧)。 现在,即使使用 Chrome,socket.request 也会给出不同的结果,而且我找不到 cookie。我是否忘记修改其他内容?如果没有,请告诉我如何获取 cookie。

非常感谢,

沙巴

I had an application that uses node.js and socket.io. It was working fine three months ago but when I tried to run it now it showed me that the module socket.io is not defined (don't know why?). So I installed it again using npm install socket.io. It turned out that socket.io new version was released with a different set of functions (or at least the way it is used). I fixed the changes but have still some problem. Here is what I do:

server:

    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(){
                    ...
            });
    });

client:

    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,
                            }));
                    }
            });
    }  

This is a slightly modified code of what was working fine with socket.io 0.6 to include the new way to connect and listen to the websocket. My problem is:

socket.request used to exist if using chrome since it allows for websockets and I could reach the cookies directly (the xhr-polling trick I used to reach cookies in other browsers that don't support websockets). Now socket.request is giving different results even with chrome and I can't find the cookies. Did I forget to modify anything else? If not, please me tell how to reach the cookies.

Thanks a lot,

Sabah

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小姐丶请自重 2024-12-02 17:57:39

您是否阅读过https://github.com/LearnBoost/Socket。 IO/wiki/Migration-0.6-to-0.7
它包含有关从 0.6 迁移到最新 0.7 的信息

Did you read https://github.com/LearnBoost/Socket.IO/wiki/Migrating-0.6-to-0.7 ?
It contains allot of information on migrating from 0.6 to the latest 0.7

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文