简体   繁体   中英

Can't connect to a node.js server on my VPS

Well, I hope this question isn't silly or repeated. I promise I have researched and tested a lot.

I've have a server on my VPS with this code:

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

And a client in another machine:

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

This works when using private IP addresses and the same net. When using public IP this doesn't work. Any idea of why is this happening?

Thank you.

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

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

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

Thank you for all your comments and ideas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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