1

我已经在我的家庭网络中设置了一个 Ubuntu 10.04 服务器。我在上面安装了一个灯服务器、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)存储在

/var/www

那是我的 LAMP 服务器的主文件夹所在的位置。

客户端.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 服务器或其他日志上看不到任何连接,并且浏览器中没有任何消息。

嗯……我做错了什么?我已经尝试了几个小时,改变了各种各样的东西,但仍然没有结果。

4

1 回答 1

2

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

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

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

应用程序.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);
  });
});

索引.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>
于 2012-04-21T19:47:32.367 回答