Javascript Node.js-Socket.io只能通过localhost访问
当我运行Node.js脚本时,我只能使用localhost:8083访问它,而从另一个设备使用机器的IP地址会导致“无法访问此站点”。我正在使用以下Node.js服务器脚本:Javascript Node.js-Socket.io只能通过localhost访问,javascript,node.js,sockets,socket.io,Javascript,Node.js,Sockets,Socket.io,当我运行Node.js脚本时,我只能使用localhost:8083访问它,而从另一个设备使用机器的IP地址会导致“无法访问此站点”。我正在使用以下Node.js服务器脚本: var app = require('express')(), server = require('http').createServer(app), io = require('socket.io').listen(server), ent = require('ent'), // Blocks
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
ent = require('ent'), // Blocks HTML characters (security equivalent to htmlentities in PHP)
fs = require('fs');
// Loading the page index.html
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket, username) {
// When the username is received it’s stored as a session variable and informs the other people
socket.on('new_client', function(username) {
username = ent.encode(username);
socket.username = username;
socket.broadcast.emit('new_client', username);(err) {});
});
// When a message is received, the client’s username is retrieved and sent to the other people
socket.on('message', function (message) {
message = ent.encode(message);
socket.broadcast.emit('message', {username: socket.username, message: message});
});
});
console.log('Chat Socket.io running on port 8083');
server.listen(8083);
<script src = "http://code.jquery.com/jquery-1.10.1.min.js" > </script>
<script src="/socket.io / socket.io.js "></script>
<script>
// Connecting to socket.io
var socket = io.connect("10.254.17.115:8083");
// The username is requested, sent to the server and displayed in the title
var username = prompt('What\'s your username?');
socket.emit('new_client', username);
document.title = username + ' - ' + document.title;
// When a message is received it's inserted in the page
socket.on('message', function(data) {insertMessage(data.username, data.message)})
// When a new client connects, the information is displayed
socket.on('new_client', function(username) {
$('#chat_zone').prepend(
'<p><audio class="background" autoplay><source src="new-user.mp3" type="audio/wav"></audio><em>' + username + ' has joined the chat!</em></p>');
})
// When the form is sent, the message is sent and displayed on the page
$('#chat_form').submit(function() {
var message = $('#message').val();
socket.emit('message', message); // Sends the message to the others
insertMessage(username, message); // Also displays the message on our page
$('#message').val('').focus(); // Empties the chat form and puts the focus back on it
return false; // Blocks 'classic' sending of the form
});
// Adds a message to the page
function insertMessage(username, message) {
$('#chat_zone').prepend('<p><strong>' + username + '</strong> ' + message + '</p>');
}
</script>
以下是客户端脚本:
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
ent = require('ent'), // Blocks HTML characters (security equivalent to htmlentities in PHP)
fs = require('fs');
// Loading the page index.html
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket, username) {
// When the username is received it’s stored as a session variable and informs the other people
socket.on('new_client', function(username) {
username = ent.encode(username);
socket.username = username;
socket.broadcast.emit('new_client', username);(err) {});
});
// When a message is received, the client’s username is retrieved and sent to the other people
socket.on('message', function (message) {
message = ent.encode(message);
socket.broadcast.emit('message', {username: socket.username, message: message});
});
});
console.log('Chat Socket.io running on port 8083');
server.listen(8083);
<script src = "http://code.jquery.com/jquery-1.10.1.min.js" > </script>
<script src="/socket.io / socket.io.js "></script>
<script>
// Connecting to socket.io
var socket = io.connect("10.254.17.115:8083");
// The username is requested, sent to the server and displayed in the title
var username = prompt('What\'s your username?');
socket.emit('new_client', username);
document.title = username + ' - ' + document.title;
// When a message is received it's inserted in the page
socket.on('message', function(data) {insertMessage(data.username, data.message)})
// When a new client connects, the information is displayed
socket.on('new_client', function(username) {
$('#chat_zone').prepend(
'<p><audio class="background" autoplay><source src="new-user.mp3" type="audio/wav"></audio><em>' + username + ' has joined the chat!</em></p>');
})
// When the form is sent, the message is sent and displayed on the page
$('#chat_form').submit(function() {
var message = $('#message').val();
socket.emit('message', message); // Sends the message to the others
insertMessage(username, message); // Also displays the message on our page
$('#message').val('').focus(); // Empties the chat form and puts the focus back on it
return false; // Blocks 'classic' sending of the form
});
// Adds a message to the page
function insertMessage(username, message) {
$('#chat_zone').prepend('<p><strong>' + username + '</strong> ' + message + '</p>');
}
</script>
//连接到socket.io
var插座=io.connect(“10.254.17.115:8083”);
//用户名被请求,发送到服务器并显示在标题中
var username=prompt('您的用户名是什么?');
emit('new_client',用户名);
document.title=用户名+'-'+document.title;
//当收到消息时,会将其插入页面中
on('message',函数(数据){insertMessage(data.username,data.message)})
//当新客户端连接时,将显示信息
socket.on('new_client',函数(用户名){
$(“#聊天区”)。前置(
“”+username+”已加入聊天!”;
})
//发送表单时,消息将被发送并显示在页面上
$('chat_form')。提交(函数(){
var message=$('#message').val();
emit('message',message);//将消息发送给其他人
insertMessage(用户名,消息);//也会在我们的页面上显示消息
$('#message').val('').focus();//清空聊天表单并将焦点放回该表单上
return false;//阻止表单的“classic”发送
});
//将消息添加到页面
函数insertMessage(用户名、消息){
$(“#聊天区”)。前置(“”+用户名+”“+消息+””);
}
编辑:修改了端口,但它们不是问题所在。当我将
var socket=io.connect
更改为io()
时,它工作正常。这可能是服务器(和/或网络)防火墙的问题,而不是节点的问题。当您需要在服务器上使用新端口时,您需要打开任何防火墙以允许该端口的流量
一个好的开始是将端口扫描实用程序(例如)下载到尝试连接的客户机上。然后将应用工具指向服务器,查看哪些端口处于打开状态
一旦确认这是端口问题,请在计算机防火墙上打开该端口。之后,如果您仍然无法联系机器,则网络防火墙可能会阻止流量到达服务器
如果你还没弄清楚,你会想从网络专家那里得到一些建议(不是我的专长),所以考虑下一个问题。此外,考虑对上述代码添加一些错误处理;如果不是防火墙问题的话,它可能会帮助您追踪到底是什么问题
希望有帮助,~Nate这可能是服务器(和/或网络)防火墙的问题,而不是节点的问题。当您需要在服务器上使用新端口时,您需要打开任何防火墙以允许该端口的流量 一个好的开始是将端口扫描实用程序(例如)下载到尝试连接的客户机上。然后将应用工具指向服务器,查看哪些端口处于打开状态 一旦确认这是端口问题,请在计算机防火墙上打开该端口。之后,如果您仍然无法联系机器,则网络防火墙可能会阻止流量到达服务器
如果你还没弄清楚,你会想从网络专家那里得到一些建议(不是我的专长),所以考虑下一个问题。此外,考虑对上述代码添加一些错误处理;如果不是防火墙问题的话,它可能会帮助您追踪到底是什么问题
希望有帮助,~Nate您不需要在客户端调用
connect
,当为客户端提供socket.io.js
服务时,它将尝试自动连接到提供文件的服务器,并调用io()代码>
编辑:
一旦为客户机提供了socket.io.js,您就只需要调用这些了<在本例中,代码>套接字
将是连接到服务器的套接字对象
看一个简单的例子。您可以看到,在客户端示例中,它们只执行
var socket=io()
重新连接到服务器。您不需要在客户端调用connect
,当客户端被服务socket.io.js
时,它将尝试自动连接到提供文件的服务器,并调用io()代码>
编辑:
一旦为客户机提供了socket.io.js,您就只需要调用这些了<在本例中,代码>套接字
将是连接到服务器的套接字对象
看一个简单的例子。您可以看到,在客户端示例中,它们只执行
var socket=io()代码>以连接回服务器。客户端和服务器端口应匹配(8081和8083不匹配):)客户端和服务器端口应匹配(8081和8083不匹配):)运行Socket.IO服务器的计算机是否允许在该端口上进行传入连接?可能防火墙正在阻止这些请求。等等,如果服务器在8083上运行,为什么客户端脚本试图连接到端口8081?
带有空格?运行Socket.IO服务器的计算机是否允许该端口上的传入连接?可能防火墙正在阻止这些请求。等等,如果服务器在8083上运行,为什么客户端脚本试图连接到端口8081?
带有空格?