2012-04-09 33 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