Node.js + socket.io:服务器上的应用程序无法正常工作 [英] Node.js + socket.io: app on server not working correctly
问题描述
我是 node.js 和 socket.io 的新手,并尝试使用 http://socket.io/#how-to-use.(没有本地主机)
I'm new to node.js and socket.io and tried to connect the server to the client with the example from http://socket.io/#how-to-use. (no localhost)
服务器:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(80);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html'+err);
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.on('message', function(msg){
console.log('Got text: '+msg);
socket.broadcast.send(msg);
});
socket.on('disconnect', function () { });
});
客户:
<html><head><script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
socket.on('connect', function () {
alert('connected.');
socket.on('message', function (msg) {
// my msg
alert('message received: '+msg);
});
socket.send('hi');
});
</script>
</head><body>This is the content :)</body>
</html>
谷歌浏览器显示在控制台中:
Google Chrome displays in the console:
Unexpected response code: 502
此外,在收到每条消息后,Chrome 都会添加
Also, after receiving every message, Chrome adds
GET http://[myServer]/socket.io/1/?t=1352313105809 socket.io.js:1659
Socket.handshake socket.io.js:1659
Socket.connect socket.io.js:1699
maybeReconnect
到控制台.问题出在哪里?
to the console. Wheres the problem?
推荐答案
How-To 页面中的示例都使用端口 80,这是服务网站的常用端口.
The examples from the How-To page all use port 80, which is common for serving websites.
但是,您在示例中使用了端口 8080.
However, you use port 8080 in your example.
检查您的 Web 浏览器的控制台是否加载了 socket.io 脚本.您可能需要提供 http://localhost:8080/socket.io/socket.io.js
作为显式 url 并使用 io.connect('http://localhost:8080');
Check your web browser's console if it even loads the socket.io script.
You may need to provide http://localhost:8080/socket.io/socket.io.js
as explicit url and connect with io.connect('http://localhost:8080');
如果上述方法不起作用,请分享一些有关您的网络服务器运行在哪个端口上的见解.
If the above does not work, please share some insight on what port your web server runs on.
在您的(更新后的)示例中,没有实际处理任何服务器端传入消息的代码.
There is no code to actually handle any incoming messages server-side in your (updated) example.
`socket.on('message', function(msg){
console.log('Got text: '+msg);
socket.send(msg);
});
至少应该将消息发送回客户端 - 只有在客户端收到消息时才会触发警报.node.js 控制台是否输出任何传入或发送的消息?连接后,我的 node.js 控制台的几行如下所示.
should at the very least send the message back to the client - your alert is only triggered when the client receives a message. Does the node.js console output any incoming or sent messages? A few lines of my node.js console look like the following upon connecting.
debug - client authorized
info - handshake authorized ...
debug - setting request GET /socket.io/1/...
debug - set heartbeat interval for client ...
debug - client authorized for
debug - websocket writing 1::
debug - sending data ack packet
这篇关于Node.js + socket.io:服务器上的应用程序无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!