NodeJS + Socket.io:客户端将不会连接到家庭networking中的Ubuntu服务器上

我在家庭networking中build立了一个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)中input地址时,我看到'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服务器或其他日志没有连接,浏览器中没有消息。

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

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

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

服务器文件(app.js)和客户端文件(index.html)应位于安装节点的目录中。 节点服务器将客户端文件发送到浏览器,因此不需要将客户端文件放在网站的“普通”文件所在的/ 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>