ホームネットワークにUbuntu 10.04サーバーをセットアップしました。その上にランプサーバー、NodeJs、およびSocket.ioをインストールしました。正常に動作しているように見えますが、クライアントからサーバーに接続できません。クライアントファイルをどこに保存するか、および/またはIPアドレスとポートをコードに入れる方法のポイントが欠けていると思います。
David Walsh ( http://davidwalsh.name/websocket ) の例を使用しています。サーバーの IP アドレスは 192.168.1.113 です。クライアントでapp.jsを保存します
/usr/ローカル/ビン
そこにノードもインストールされます。
サーバーコード
// Require HTTP module (to start server) and Socket.IO
var http = require('http'), io = require('socket.io');
// Start the server at port 8000
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(8000);
// Create a Socket.IO instance, passing it our server
var socket = io.listen(server);
// Add a connect listener
socket.on('connection', function(client){
// 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');
});
});
Node でサーバーを起動し、ブラウザー(http://192.168.1.113:8000)にアドレスを入力すると、「Hello Socket Lover!」と表示されます。だから私はこれがうまくいくと思います。
クライアントファイル ( client.html ) を
/var/www
これが、LAMP サーバーのホーム フォルダーの場所です。
Client.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script>
// Create SocketIO instance, connect
var socket = new io.Socket('http://192.168.1.113',{
port: 8000
});
socket.connect();
// Add a connect listener
socket.on('connect',function() {
console.log('Client has connected to the server!');
});
// Add a connect listener
socket.on('message',function(data) {
console.log('Received a message from the server!',data);
});
// Add a disconnect listener
socket.on('disconnect',function() {
console.log('The client has disconnected!');
});
// Sends a message to the server via sockets
function sendMessageToServer(message) {
socket.send(message);
}
</body>
</html>
このファイルをブラウザー(http://192.168.1.113/client.html)で開いても、何も起こりません。Ubuntu サーバーまたは他のログに接続が表示されず、ブラウザーにメッセージがありません。
うーん...何が間違っているのですか?私は何時間も試し、あらゆる種類のものを変更しましたが、まだ結果はありません.