我試圖弄清楚我的代碼/設置有什麼問題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