【问题标题】:How to start socket.io server on specific public ip?如何在特定的公共 IP 上启动 socket.io 服务器?
【发布时间】:2017-06-23 00:10:33
【问题描述】:

我想在我的公共 IP 上加载我的套接字服务器,因为现在我只能在 LAN localhost:8080127.0.0.1:8080 上加载它。我想连接我自己的IP77.90.100.55:8080

我不知道为什么但我不能使用

io.connect('http://77.90.100.55:8888');

要连接,只有这个有效:

io.connect('http://localhost:8888');

当我尝试与 77.90.100.55 连接时,我在客户端收到此错误:

polling-xhr.js:264 GET http://77.90.100.55:3000/socket.io/?EIO=3&transport=polling&t=LpBv_sK net::ERR_CONNECTION_TIMED_OUT

服务器

var http = require("http").createServer(), io = require("socket.io").listen(http);

var port = process.env.PORT || 3000;

http.listen(port);


//================================  Connection stuff ================================//
io.sockets.on("connection", function(socket)
{
    socket.on('player connected', function(data)
    {   

    });

});

console.log('Server listening on port %s', http.address().port);

更新

我可以使用所有端口和公共 IP,并且可以通过命令行访问我的 IP:

Pinging 77.90.100.55 with 32 bytes of data:
Reply from 77.90.100.55: bytes=32 time<1ms TTL=64
Reply from 77.90.100.55: bytes=32 time<1ms TTL=64
Reply from 77.90.100.55: bytes=32 time<1ms TTL=64
Reply from 77.90.100.55: bytes=32 time<1ms TTL=64

Ping statistics for 77.90.100.55:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

我用start.bat运行节点服务器:

node C:\xamppp\htdocs\pure2d\server\server.js 2>error_log.log

【问题讨论】:

  • 这与node.js无关。确保您的网络配置为将 8888 从您机器的本地地址移植到公共地址。默认情况下,消费者路由器会阻止所有传入流量。
  • 我没有路由器,我的互联网提供商让我使用所有端口和公共 ip,我可以托管任何游戏,其他人可以连接,所有来自客户端的连接都很好,因为当我使用 localhost 时一切正常很好
  • 好的,尝试将io = require('socket.io').listen(http) 更改为io = require('socket.io')(http)。还要确保您的http 服务器有一个处理程序。参考this documentation
  • 将代码更改为 io = require('socket.io')(http) 无效
  • 哦,它正确读取process.env.port吗?通常,该端口通过process.env.PORT 传递,因此请确保它没有在端口 3000 上运行。

标签: node.js socket.io


【解决方案1】:

已修复 我使用网站http://whatismyipaddress.com/ 获取我的 ip,但我忘记了我有 wifi 路由器可以更改我的 ip,我只能从计算机 网络和共享面板检查该 ip,因为网站无法显示它。

【讨论】:

  • 您有动态 IP 地址吗?呃,我希望你早点提到这一点。
  • 无论如何感谢您的帮助,因为您帮助了我一些代码行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-29
  • 2013-04-21
  • 1970-01-01
  • 2017-10-07
  • 1970-01-01
相关资源
最近更新 更多