2012-04-10 74 views
5

我試圖弄清楚我的代碼/設置有什麼問題now.Basically我想從客戶端發送消息到服務器。以下工作在所有瀏覽器除了 FF(11)和鉻(18.0.1025.152米)。Socket.io無法在Chrome和Firefox上向節點服務器發送消息

下面是一些信息:

  1. 我運行Windows 7企業版64位機
  2. 我使用Apache的Node.js從本地主機
  3. 我聽端口8080提供網頁與socket.io
  4. 它看起來像鉻和FF回退到XHR輪詢/ JSONP輪詢,而不是使用套接字
  5. 我可以從服務器推送的消息,但我不能從客戶端發出

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個切換協議): enter image description here

鉻XHR請求: enter image description here

更新: Iv'e加入

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

它永遠不會在鍍鉻上發火。

那麼有什麼可能是錯的?請幫助:)

+0

我有完全一樣的問題... socket.emit(「你好」)上的客戶端在Safari的工作,但不是在Firefox或Chrome ...安全限制可能?或者可能是一個socket.io錯誤。 – 2012-04-12 14:12:46

+0

它可能是「同源」政策,因爲只有在使用XHR時才發生故障?嘗試製作一個簡單的XHR到localhost:不同於端口的服務頁面,查看是否有任何安全異常。 – 2012-04-12 15:43:09

+0

沒有安全問題,我現在使用http://faye.jcoglan.com/node.html(faye)作爲節點JS上的套接字,它工作正常......看起來像是一個socket.io錯誤。 – 2012-04-15 09:32:31

回答

0

我相信你使用的客戶端錯誤的socket.io.js文件?

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

,你有你的客戶端的東西,即你應該使用一個文件夾中的這些文件: ../js/socket.io.min.js

關於需要的問題,您使用NPM?進入項目的NodeJS文件夾,然後運行以下命令:

npm install socket.io

這應該安裝到node_modules文件夾項目文件夾內,然後通過執行require('socket.io');你應該能夠訪問模塊。

如果不工作,你可能有節點的腐敗安裝。

PS:您使用的文件相同的端口?你似乎在尋找socket.io文件端口8080,然後連接上的NodeJS,套接字連接端口8080 ...

嘗試使用其他端口?

+0

即使我有同樣的問題,它不適用於Firefox和Chrome,但它在IE上運行得很好,請幫忙。 – Shekhar 2012-07-05 11:53:08