2012-04-10 76 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