2010-08-04 100 views
0

我有一個node.js服務器正在運行,當我導航到url時,我可以讓它顯示「hello world」或twitter feed。Node.js和Web套接字

問題是我無法在node.js實例和另一個頁面的客戶端上定義的websocket之間發生任何通信。

有沒有人有任何想法?

非常感謝。

+0

參見:http://stackoverflow.com/questions/3127864/node-js-websocket-location-issue/3443892#3443892 – Ryan 2010-08-09 21:19:18

回答

0

Look here適用於與Node.JS兼容的WebSocket庫。 Node.JS不提供開箱即用的WebSocket支持,您需要安裝額外的庫。 Socket.io就足夠了。

+0

感謝您的答覆。我現在得到了一些東西,當我刷新服務器時,我可以在我的shell窗口中看到請求正在進入。 但是唯一的websocket事件被觸發了 - 任何想法? 非常感謝 – dan 2010-08-04 15:16:44

+0

沒有代碼我沒有任何想法。 – fuwaneko 2010-08-05 05:01:16

1

是否在一些代理後面運行node.js?一些代理(例如ngnix)不支持http 1.1,而web 1.1需要http 1.1。

0

好了,所以我得到了這個工作(我認爲)

再次,客戶端代碼:

<script src="./Socket.IO/socket.io.js"></script> 
<script> 
    io.setPath('./Socket.IO/'); 

    var socket = new io.Socket('jayz.danstanhope.webfactional.com', { 'port': 80 }); 

    socket.on('connect', function() { 
     alert('connect'); 
    }); 
    socket.on('message', function (msg) { 
     alert('message' + msg); 
    }); 
    socket.on('close', function() { 
     alert('close'); 
    }); 
    socket.on('disconnect', function() { 
     alert('disconnect'); 
    }); 
    socket.connect(); 

</script> 

服務器端代碼:

var sys = require("sys") 
    , fs = require("fs") 
    , path = require("path") 
    , http = require("http"); 
var io = require('/home/danstanhope/webapps/htdocs/Socket.IO-node'); 

var server = http.createServer(function (req, res) { 
    //your normal server code 
    res.writeHead(200, { 'Content-Type': 'text/html' }); 
    res.write('Hello world'); 
    res.end(); 
}); 

server.listen(26970); 
server = io.listen(server); 
server.on('connection', function(client){ 
    sys.log('client connected'); 
}); 

當我刷新頁面在Chrome中,我可以看到正在Shell中寫入的日誌。

這是我看到:

[email protected] htdocs]$ node server.js 
9 Aug 19:19:37 - socket.io ready - accepting connections 
9 Aug 19:19:40 - Initializing client with transport "websocket" 
9 Aug 19:19:40 - Client 21789167495444417 connected 
9 Aug 19:19:40 - client connected 
9 Aug 19:19:40 - Client 21789167495444417 disconnected 

唯一的問題現在越來越任何的JavaScript的插座警報火。

任何想法?

感謝, 丹