我试图弄清楚我的代码/设置有什么问题now.Basically我想从客户端发送消息到服务器。以下工作在所有浏览器除了 FF(11)和铬(18.0.1025.152米)。Socket.io无法在Chrome和Firefox上向节点服务器发送消息
下面是一些信息:
- 我运行Windows 7企业版64位机
- 我使用Apache的Node.js从本地主机
- 我听端口8080提供网页与socket.io
- 它看起来像铬和FF回退到XHR轮询/ JSONP轮询,而不是使用套接字
- 我可以从服务器推送的消息,但我不能从客户端发出
Server代码:
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
[email protected] C:\Users\shlomis
├─┬ [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ └── [email protected]
└─┬ [email protected]
├── [email protected]
├── [email protected]
└─┬ [email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
│ ├── [email protected]
│ └── [email protected]
└── [email protected]
节点的日志:
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
铬WS请求(101个切换协议):
铬XHR请求:
更新: Iv'e加入
socket.on('connect', function() {
console.log("connected");
});
它永远不会在镀铬上发火。
那么有什么可能是错的?请帮助:)
我有完全一样的问题... socket.emit(“你好”)上的客户端在Safari的工作,但不是在Firefox或Chrome ...安全限制可能?或者可能是一个socket.io错误。 – 2012-04-12 14:12:46
它可能是“同源”政策,因为只有在使用XHR时才发生故障?尝试制作一个简单的XHR到localhost:不同于端口的服务页面,查看是否有任何安全异常。 – 2012-04-12 15:43:09
没有安全问题,我现在使用http://faye.jcoglan.com/node.html(faye)作为节点JS上的套接字,它工作正常......看起来像是一个socket.io错误。 – 2012-04-15 09:32:31