2012-09-29 9 views
5

私はUbuntuのとRackspaceの上にインストールされている私は、単純なサーバーアプリを試してみました場合はSocket.IONodeJS - Socket.IOセットアップ:静的なコンテンツを提供していません何のハンドシェイク(Rackspaceのクラウドサーバー上のUbuntu)

でのNode.jsとしようクライアントのリクエストを使用する私は手ぶれではなく「静的コンテンツを提供」しました。ブラウザでデバッグ中に私が見ることができる「こんにちはSを...」と、サーバー側で:

# node socket-server.js 
    info - socket.io started 
    debug - served static content /socket.io.js 
    debug - served static content /socket.io.js 

私は問題の外観を開始する場所がわからないんだけど(同じスクリプトは、ローカルの開発に動作します)

なぜnode.jsが静的なコンテンツだけを提供し、ハンドシェイクをしないのですか? #iptablesのは、ここで

​​

を-L単純なサーバーアプリ 'ソケットserver.js' です:ここで

// Require HTTP module (to start server) and Socket.IO 
var http = require('http'), io = require('socket.io'); 

// Start the server at port 8866 
var server = http.createServer(function(req, res){ 

     // Send HTML headers and message 
     res.writeHead(200,{ 'Content-Type': 'text/html' }); 
     res.end('<h1>Hello Socket Lover!</h1>'); 
}); 
server.listen(8866); 

// Create a Socket.IO instance, passing it our server 
var socket = io.listen(server); 

// Add a connect listener 
socket.on('connection', function(client){ 

     // Create periodical which ends a message to the client every 5 seconds 
     var interval = setInterval(function() { 
       client.send('This is a message from the server! ' + new Date().getTime()); 
     },5000); 

     // Success! Now listen to messages to be received 
     client.on('message',function(event){ 
       console.log('Received message from client!',event); 
     }); 
     client.on('disconnect',function(){ 
       clearInterval(interval); 
       console.log('Server has disconnected'); 
     }); 

}); 

は(SERVERDOMAINが交換されたシンプルなクライアントである

iptablesは8866ポートを許可します)実際のドメインとします。クライアントコードで

<!DOCTYPE html> 
<html> 
<head> 
<script src="http://SERVERDOMAIN:8866/socket.io/socket.io.js"></script> 
<script> 

    // Create SocketIO instance 
    var socket = io.connect('SERVERDOMAIN:8866'); 
    // Add a connect listener 
    socket.on('connect',function() { 
     log('<span style="color:green;">Client has connected to the server!</span>'); 
    }); 
    // Add a connect listener 
    socket.on('message',function(data) { 
     log('Received a message from the server: ' + data); 
    }); 
    // Add a disconnect listener 
    socket.on('disconnect',function() { 
     log('<span style="color:red;">The client has disconnected!</span>'); 
    }); 

    // Sends a message to the server via sockets 
    function sendMessageToServer(message) { 
     socket.send(message); 
     log('<span style="color:#888">Sending "' + message + '" to the server!</span>'); 
    } 

    // Outputs to console and list 
    function log(message) { 
     var li = document.createElement('li'); 
     li.innerHTML = message; 
     document.getElementById('message-list').appendChild(li); 
    } 

</script> 
</head> 
<body> 

<p>Messages will appear below (and in the console).</p><br /> 
<ul id="message-list"></ul> 
<ul style="margin:20px 0 0 20px;"> 
    <li>Type <code>socket.disconnect()</code> to disconnect</li> 
    <li>Type <code>socket.connect()</code> to reconnect</li> 
    <li>Type <code>sendMessageToServer('Your Message')</code> to send a message to the server</li> 
</ul> 

</body> 
</html> 
+0

あなたのページにエラーがある場合、 "debug - served static content /socket.io.js"の後に出力が停止します - あなたのクライアントサイドのコードが動作していますか? – supernova

答えて

3

この

を試してみてください
var socket = io.connect('http://SERVERDOMAIN:8866'); 

この問題は主に不正なURLに関連しています。

+0

アドバイスありがとうございますが、助けにはなりませんでした。 – Trebla

+0

すばらしい答え@beNerd !!これは私のために働いた。ありがとう:) –

0

私は完全に新しいサーバーのインストールに終わり、すべてが正常に動作しています。

私はUbuntuの12.04 LTS(正確なセンザンコウ)を使用し、私はFlashファイルでsocket.io.jsのローカルコピーを使用しているクライアントのためにsocket.io

でのNode.jsをインストールしています。

;-)

1

私はWiFi接続上のデスクトップブラウザと一緒にアンドロイド(携帯電話とタブレット)デバイスのカップルと私のsocket.ioアプリケーションをテストしていました。上記beNerdの答えに基づいて、私は私が

io.connect('http://localhost:3000'); 

io.connect('http://192.168.5.3:3000'); // ip address of machine where the node.js app is running. 

に変更し、それが働きました。

答えがWifi接続でNodeJSとSocket.IOアプリケーションをテストするユーザーにとって有益であると考えてください。

関連する問題