无法连接到我的VPS上的node.js服务器

时间:2017-04-23 16:31:48

标签: javascript node.js socket.io vps

嗯,我希望这个问题不会愚蠢或重复。我保证我已经研究和测试了很多。

我的VPS上有一个服务器,代码如下:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(8080);

io.on('connection', function(socket) {
  socket.on('hello', function() {
    console.log('Hello received');
  }
}

另一台机器中的客户端

<script>
  var socket = io.connect('http://[VPS Public IP]:8080');
  io.emit('hello');
</script>

这在使用私有IP地址和同一网络时有效。使用公共IP时,这不起作用。知道为什么会发生这种情况吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

Ok, after some more testing I've fixed my own problem.

I realized that VPS public IP (X.X.X.209) is different than the IP Address they give me to connect via SSH (X.X.X.221) using the Serial Console tool.

I was using this second IP Address X.X.X.221 when my real public IP is the first one. Once I've used X.X.X.209, it has connected without any problem.

Thank you for all your comments and ideas.