2012-04-09 36 views
1

我想使用socket.io和节点作为我的“推送通知功能”的一个图层,所以我运行了apache和node。如何使用socket.io发送邮件

我有我的服务器上执行以下代码(节点)

var app = require('http').createServer(handler) 
    , io = require('C:/path/to/file/socket.io').listen(app) 
    , fs = require('fs'); 

app.listen(8080); 

function handler(req, res) { 
    console.log(req); 
    fs.readFile('C:/path/to/file/index.html', 
     function (err, data) { 
      if (err) { 
       console.log(err); 
       res.writeHead(500); 
       return res.end('Error loading index.html'); 
      } 

      res.writeHead(200); 
      res.end(data); 
     }); 
} 

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

的页面,然后通过从本地主机的Apache服务没有8080口

和客户,我有以下代码上:

var socket = io.connect('http://localhost:8080'); 

并且当一个按钮被点击:

socket.emit('my event', {data:"some data"}); 

我在节点控制台上什么都看不到......为什么是这样?跨域问题?

更新: 它工作在Safari 5.1.5,甚至IE 9就好了,但不是在铬(18.0.1025.151)或Firefox(11.0)......我缺少什么?

这里是节点日志:

info - socket.io started 
    debug - served static content /socket.io.js 
    debug - client authorized 
    info - handshake authorized 4944162402088095824 
    debug - setting request GET /socket.io/1/websocket/4944162402088095824 
    debug - set heartbeat interval for client 4944162402088095824 
    debug - client authorized for 
    debug - websocket writing 1:: 
    debug - setting request GET /socket.io/1/xhr-polling/4944162402088095824?t=13 
33977095905 
    debug - setting poll timeout 
    debug - discarding transport 
    debug - cleared heartbeat interval for client 4944162402088095824 

回答

3

这应该做工精细,只要确保在您的index.html您有:

<script src="http://localhost:8080/socket.io/socket.io.js"></script> 

还,因为你服务你的网页通过Apache,你真的不需要你的节点文件中的处理程序和http服务器。 这应该只是罚款:

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

,为的index.html:

<!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> 

编辑:这个工作在两个Firefox和Chrome。

+0

感谢您的回复!显然我的问题是本地的,你的例子在safari和IE浏览器上运行良好,但不能在我的Chrome或Firefox上运行......我应该在哪里看? PS:我搞乱了WebRTC,所以在我的Chrome上启用了此功能(“启用MediaStream。Mac,Windows,Linux,Chrome操作系统 为WebRTC功能启用MediaStream,GetUserMedia和PeerConnection API。 。“),尝试禁用它没有成功......任何其他的想法? – 2012-04-09 13:51:17

+0

尝试重新安装铬...没有去:( 更多信息:我在Windows 7上运行节点,并使用完整路径到socket.io像这样“C:\ Users \ shlomis \ node_modules \ socket.io “(无法弄清楚)......这可能是相关的吗? – 2012-04-09 14:38:28

+0

这很奇怪......尝试在Firefox上打开Chrome开发工具或Firebug,然后打开”网络“选项卡,它可以为您提供更多信息关于引擎盖下发生了什么 – 2012-04-09 17:30:49