Socket.io 服务器似乎没有连接?
我正在学习 NodeTuts 上的教程。我正在尝试使用 socket.io 让聊天服务器正常工作。
我可以正常启动节点实例,并且在启动时收到“socket.io 准备就绪 - 接受连接”消息。当我将浏览器指向 localhost:8888 时,我也会从 template.html 获得 html 表单,这样一切看起来都不错。不过,我似乎从未建立过套接字连接,也从未看到“已收到连接”日志或“欢迎来到聊天服务器”消息。
有什么想法吗?
var fs, http, io, server, socket, sys;
http = require('http');
fs = require('fs');
sys = require('sys');
io = require('socket.io');
server = http.createServer(function(req, res) {
var rs;
console.log('server started');
res.writeHead(200, {
'Content-Type': 'text/html'
});
rs = fs.createReadStream(__dirname + '/template.html');
return sys.pump(rs, res);
});
socket = io.listen(server);
socket.on('connection', function(client) {
var username;
console.log('Connection received');
client.send('Welcome to the chat server');
client.send('Please enter a username');
return client.on('message', function(message) {
if (!username) {
username = message;
return client.send("Hi " + username + "!");
} else {
return socket.broadcast("" + username + ": " + message);
}
});
});
server.listen(8888);
这是template.html的JS部分
<script src="/socket.io/socket.io.js"></script>
<script>
$(function() {
socket = new io.Socket('localhost', {port: 8888});
socket.on('message', function(message) {
var data = message.data
data = data.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
console.log( data );
$('#log ul').append('<li>' + data + '</li>');
window.scrollBy(0, 1000000000000000);
entry_el.focus();
});
var entry_el = $('#entry');
entry_el.keypress(function(event) {
if (event.keyCode != 13) return;
var msg = entry_el.attr('value');
// if the message is not empty
if (msg) {
socket.send(msg);
entry_el.attr('value', '');
}
});
});
</script>
I'm working my way through the tutorials on NodeTuts. I'm trying to get a chat server working using socket.io.
I can start the node instance fine and I get the "socket.io ready - accepting connections" message when I do. When I point my browser at localhost:8888 I get served the html form from template.html also so that all looks good. I never seem to make a socket connection though and I never see the "Connection Received" log or the 'Welcome to the chat server' message.
Any ideas what the problem is?
var fs, http, io, server, socket, sys;
http = require('http');
fs = require('fs');
sys = require('sys');
io = require('socket.io');
server = http.createServer(function(req, res) {
var rs;
console.log('server started');
res.writeHead(200, {
'Content-Type': 'text/html'
});
rs = fs.createReadStream(__dirname + '/template.html');
return sys.pump(rs, res);
});
socket = io.listen(server);
socket.on('connection', function(client) {
var username;
console.log('Connection received');
client.send('Welcome to the chat server');
client.send('Please enter a username');
return client.on('message', function(message) {
if (!username) {
username = message;
return client.send("Hi " + username + "!");
} else {
return socket.broadcast("" + username + ": " + message);
}
});
});
server.listen(8888);
Here's the JS part of template.html
<script src="/socket.io/socket.io.js"></script>
<script>
$(function() {
socket = new io.Socket('localhost', {port: 8888});
socket.on('message', function(message) {
var data = message.data
data = data.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
console.log( data );
$('#log ul').append('<li>' + data + '</li>');
window.scrollBy(0, 1000000000000000);
entry_el.focus();
});
var entry_el = $('#entry');
entry_el.keypress(function(event) {
if (event.keyCode != 13) return;
var msg = entry_el.attr('value');
// if the message is not empty
if (msg) {
socket.send(msg);
entry_el.attr('value', '');
}
});
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您需要在客户端代码中调用
socket.connect();
,紧接在socket = new io.Socket('localhost', {port: 8888});< /代码>
I believe you need to call
socket.connect();
in your client-side code, right aftersocket = new io.Socket('localhost', {port: 8888});