【问题标题】:socket.io server not working outside of networksocket.io 服务器无法在网络外工作
【发布时间】:2020-04-15 05:15:43
【问题描述】:

你好!!!

在这个工具的帮助下,我成功地移植了 3000 端口。 https://www.yougetsignal.com/tools/open-ports/

我转发此端口的原因是因为我制作了一个聊天应用程序,它在 localhost 上运行得非常好。可悲的是,当我要求我的朋友打开我的网络聊天应用程序 (http://mywebsite.com:81/chat/) 时,javascript 内容并没有通过端口 3000 连接到我的服务器...

'这是我的一些代码:

server.js

const io = require('socket.io')(3000)

const users = {}

io.on('connection', socket => {
    // On message receive
    socket.on('msg', message => {
        //broadcast message
        socket.broadcast.emit('msg', {name: users[socket.id], msg: message});

        // If message starts with "/" AKA is a command
        if(message.startsWith("/")){
            message=message.split(" ");
            //message[0]= /help

            if(message[0] == "/say"){
                txt=message.splice(1).join(" "); // GET TEXT AFTER /help
                socket.emit('msg', {name: "TomatoBot", msg: txt});
            }else if(message[0] == "/help"){
                txt=message.splice(1).join(" "); // GET TEXT AFTER /help
                socket.emit('msg', {name: "TomatoBot", msg: "help dialog"});
            }else if(message[0] == "/list"){
                socket.emit('msg', {name: "TomatoBot", msg: users[socket.id]+" connected users:\n"});
            }else{
                //no commands found, throw error
                socket.emit('msg', {name: "TomatoBot", msg: "@"+users[socket.id]+"\nUnknown command: "+message[0]+". Type /help for a list of commands."});
            }
        }
    })

    // On user join
    socket.on('new', name => {
        users[socket.id] = name;
        socket.broadcast.emit('new', name);
    })

    // On user disconnect
    socket.on('disconnect', () => {
        socket.broadcast.emit('bye', users[socket.id]);
        delete users[socket.id];
    })
})

script.js(我的客户端脚本)

const socket = io(':3000'); // go from localhost:81 to localhost:3000!!
const form = document.getElementById('form');
const input = document.getElementById('input');
const msgbox = document.getElementById('msgbox');

const name = prompt('name?');
socket.emit('new', name);
appendMessage(name+' joined');
// etc etc.....

正如我已经说过的,连接和消息在 localhost 上通过,但是一旦我使用外部域/IP,就没有任何效果了!!当从外部世界直接访问 mywebsite.org:3000/socket.io/socket.io.js 时,连接确实有效!!

我在这里迷路了,一定是由于我的代码中的一些愚蠢的错误..

谢谢大家的帮助!!

【问题讨论】:

  • 来自网络外部的客户端脚本必须指定路由器的公共 IP 地址或映射到该公共 IP 地址的域。客户端不能只指定一个端口,除非它连接到自己,这肯定不是您想要从网络外部执行的操作。因此,您的客户端需要类似于 const socket = io('http://:somedomain.com:3000');io('http://123.456.789:3000');,这是您的路由器的公共 IP 地址,然后将端口转发到您专用网络上的实际服务器。

标签: node.js socket.io


【解决方案1】:

除了用于 http 服务器的端口之外,您不需要使用其他端口。

在服务器端

const http = require(`http`)
const io = require(`socket.io`)(http)

http.createServer(function (req, res) {
  // Use this server for http://mywebsite.com:81
}).listen(81)

io.on(`connection`, client => {
    // Use this socket for websocket
})

在客户端

const socket = io(`http://mywebsite.com:81`)

【讨论】:

  • 所以在客户端脚本我应该写const socket = io(':81'); ?
  • 我添加了我所有的 server.js。为了澄清事情,我在节点服务器旁边的端口 81 上运行 apache。
  • 我的权限被拒绝.. Error: listen EACCES: permission denied 0.0.0.0:81 在服务器控制台上。将端口更改为 82 仍然会导致崩溃
猜你喜欢
  • 2018-09-08
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 2023-02-09
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 2022-10-24
相关资源
最近更新 更多