NodeJS + Socket.io:客户端不会在家庭网络中的Ubuntu服务器上连接

时间:2012-04-17 07:12:07

标签: node.js socket.io ubuntu-10.04

我在家庭网络中设置了一个Ubuntu 10.04服务器。我在上面安装了一个lamp-server,NodeJs和Socket.io。它似乎工作正常,但我不能从我的客户端连接到服务器。我认为我错过了存储客户端文件的位置和/或如何将IP地址和端口放入代码中。

我正在使用David Walsh(http://davidwalsh.name/websocket)的例子。我的服务器的IP地址是192.168.1.113。在客户端,我将 app.js 存储在

  

的/ usr / local / bin中

这就是安装节点的地方。

服务器代码

 // 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 )存储在

  

/无功/网络

这就是我的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服务器或其他日志没有连接,浏览器中没有消息。

嗯......我做错了什么?我已经尝试了几个小时,改变了所有类型的东西,但仍然没有结果。

1 个答案:

答案 0 :(得分:2)

在pmavik的帮助下(见评论),我的问题得到了解答。

我不知道的是如何正确地提供客户端文件(index.html)。我从www.socket.io复制了一个例子,添加了正确的ip-adress和端口,现在建立了连接。

服务器文件(app.js)和客户端文件(index.html)应该位于安装Node的目录中。节点服务器将客户端文件发送到浏览器,因此无需将客户端文件放在/ var / www目录中,网站的“正常”文件位于该目录中。

<强> app.js

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(8000);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
        }

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

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

<强>的index.html

<html>
<head></head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://192.168.1.113:8000');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>
</body>
</html>