Socket.io在Chrome和Firefox上无法向Node服务器发送消息

6
我已经尝试了一段时间,想找出我的代码/设置出了什么问题。基本上,我想要从客户端向服务器发送消息。以下代码在所有浏览器中都可以正常工作,但是在Firefox(11)和Chrome(18.0.1025.152 m)中无法正常工作。
以下是一些信息:
1.我在Windows 7企业版64位机器上运行Node.js 2.我使用apache从localhost提供页面 3.我使用socket.io监听8080端口 4.看起来Chrome和Firefox会回退到xhr-polling / jsonp-polling而不是使用sockets 5.我可以从服务器推送消息,但我无法从客户端发送
服务器代码:
var io = require('C:/Users/shlomis/node_modules/socket.io/lib/socket.io').listen(8080);

io.sockets.on('connection', function (socket) {
    socket.on('my event', function (msg) {
        console.log("DATA!!!");
    });
});

我找不到一种方法可以不使用完整路径来要求文件

客户端代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Hello World!</title>
    <meta charset="utf-8">

    <script src="http://localhost:8080/socket.io/socket.io.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var socket = io.connect('http://localhost:8080');
            $("#button").click(function() {
                socket.emit('my event' ,"Hello World!");
            })
        })
    </script>
</head>

<body>
<button type="button" id='button'>Send Message</button>
</body>

</html>

模块版本:

C:\Users\shlomis>npm ls
mukhin_chat@0.0.1 C:\Users\shlomis
├─┬ express@2.5.9
│ ├─┬ connect@1.8.6
│ │ └── formidable@1.0.9
│ ├── mime@1.2.4
│ ├── mkdirp@0.3.0
│ └── qs@0.4.2
└─┬ socket.io@0.9.5
  ├── policyfile@0.0.4
  ├── redis@0.6.7
  └─┬ socket.io-client@0.9.5
    ├─┬ active-x-obfuscator@0.0.1
    │ └── zeparser@0.0.5
    ├── uglify-js@1.2.5
    ├─┬ ws@0.4.12
    │ ├── commander@0.5.2
    │ └── options@0.0.3
    └── xmlhttprequest@1.2.2

Node日志:

C:\Users\shlomis>node C:\dev\wamp\www\AR\js\videoServer.js
   info  - socket.io started
   debug - served static content /socket.io.js
   debug - client authorized
   info  - handshake authorized 17502185141066845391
   debug - setting request GET /socket.io/1/websocket/17502185141066845391
   debug - set heartbeat interval for client 17502185141066845391
   debug - client authorized for
   debug - websocket writing 1::
   debug - setting request GET /socket.io/1/xhr-polling/17502185141066845391?t=1334041653716
   debug - setting poll timeout
   debug - discarding transport
   debug - cleared heartbeat interval for client 17502185141066845391

谷歌浏览器 WebSocket 请求(101 切换协议):enter image description here

谷歌浏览器 XHR 请求:enter image description here

更新:我已经添加了

socket.on('connect', function () {
     console.log("connected");
});

在chrome上它从未触发。

那么可能出了什么问题?请帮忙解决 :)


我遇到了完全相同的问题...在Safari中,客户端上的socket.emit('hello')可以工作,但在Firefox或Chrome中无法工作...可能是安全限制?或者可能是socket.io的一个bug。 - Tom Wells
可能是“同源策略”导致的,因为仅在使用XHR时出现故障。尝试向localhost:与提供页面不同的端口进行简单的XHR,请查看是否会出现任何安全异常。 - Gipsy King
没有安全问题,我现在使用http://faye.jcoglan.com/node.html(faye)来处理node JS上的套接字,它运行良好...看起来像是socket.io的一个bug。 - Shlomi Schwartz
哦,我也遇到了同样的问题 :( ... 它在几乎所有人身上都有效,但对我无效。 - nguyenkha
这个问题有解决方案吗?我也遇到了同样的问题.. :( - Manu
我的解决方案是切换到http://faye.jcoglan.com/node.html。 - Shlomi Schwartz
1个回答

0

我相信你正在使用错误的socket.io.js客户端文件?

https://github.com/LearnBoost/socket.io-client/tree/master/dist

您应该在客户端文件夹中使用这些文件,例如:

../js/socket.io.min.js

关于require的问题,您是否使用npm?进入nodeJS项目文件夹并运行以下命令:

npm install socket.io

这将安装到项目文件夹内的node_modules文件夹中,通过执行require('socket.io');,您应该能够访问该模块。

如果这不起作用,您可能有一个损坏的node安装。

PS:您是否为文件使用相同的端口?您似乎正在寻找端口8080上的socket.io文件,然后连接到端口8080上的nodeJS进行套接字连接...

尝试使用另一个端口?


我也遇到了同样的问题。它在Firefox和Chrome上无法工作,但在IE上却可以正常工作。请帮忙解决。 - Shekhar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接