简体   繁体   English

Node.js-Socket.io仅可通过本地主机访问

[英]Node.js - Socket.io only accessible through localhost

When I run my Node.js script, I can only access it by using localhost:8083, while using the machine's IP address from another device results in a "This site cannot be reached". 当我运行Node.js脚本时,只能使用localhost:8083来访问它,而从另一台设备使用机器的IP地址会导致“无法访问此站点”。 I am using the following Node.js server script : 我正在使用以下Node.js 服务器脚本

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);

Here is the client script : 这是客户端脚本

<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>

Edit: Revised ports but they were not the issue. 编辑:修改端口,但它们不是问题。 When I changed the var socket = io.connect to io() it worked. 当我将var socket = io.connectio()它起作用了。

This is likely an issue with your server (and/or network) firewall, not with node. 这可能是服务器(和/或网络)防火墙的问题,而不是节点的问题。 Any time that you need to use a new port on your server, you'll need to open up any firewalls to allow traffic to that port. 任何时候需要使用服务器上的新端口时,都需要打开所有防火墙以允许流量通过该端口。

A good place to start would be to download a port scanning utility (eg nmap ) onto the client that is trying to connect. 一个不错的开始是将端口扫描实用程序(例如nmap )下载到尝试连接的客户端上。 Then point the utility to the server, to see what ports are open. 然后将实用程序指向服务器,以查看打开了哪些端口。

Once you can confirm that it's a port issue, work on opening up that port on the machine's firewall. 确认是端口问题后,请在计算机的防火墙上打开该端口。 After that, if you still cannot contact the machine, then a network firewall might be preventing the traffic from reaching the server. 此后,如果仍然无法连接到计算机,则网络防火墙可能会阻止流量到达服务器。

If you still can't figure it out, you'll want to get some advice from a networking expert (not my area of expertise), so consider posting a question under Network Engineering . 如果您仍然无法解决问题,则需要从网络专家(而不是我的专业领域)那里获得一些建议,因此请考虑在Network Engineering下发布问题。 Also, consider adding some error handling to the above code; 另外,请考虑在上述代码中添加一些错误处理; it might help you to track down what exactly the problem is, if not a firewall issue. 如果不是防火墙问题,它可能会帮助您找出问题的根源。

Hope that helps, ~ Nate 希望能有所帮助,〜Nate

You don't need to call connect on the client side, when the client is served socket.io.js , it will attempt to connect automatically to the server from which the file was served, with a call to io(); 您无需在客户端上调用connect ,当为客户端提供socket.io.js ,它将通过调用io();尝试自动连接到为其提供文件的服务器io();

Edit: 编辑:

var socket = io();

Once the client has been served the socket.io.js, this is all you need to call. 为客户端提供socket.io.js后,这就是您需要调用的所有内容。 socket will be your socket object that is connected to the server, in this example. 在此示例中, socket将是连接到服务器的套接字对象。

Take a look at this simple example. 看一下这个简单的例子。 You can see that in their client side examples, they only do var socket = io(); 您可以在他们的客户端示例中看到,他们仅执行var socket = io(); to connect back to the server. 连接回服务器。

客户端和服务器端口应匹配(8081和8083不匹配):)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM