问题
I am working on a chat server and it's working fine on local but whenever I try to run the project using ngrok then me and my friend are unable to chat.
var socket = io('http://localhost:7777', {
query: {
username: '<%= user %>'
}
});
Can anyone guide me how to make this public? Because the IP address changes everytime when connected to internet.
and
var app = express();
const chatServer = require('http').createServer(app);
chatServer.listen(7777);
I'm working in node.js for the first time
回答1:
If your server and client do work locally, as you mention, you have some additional challenges when running the server and client on different machines over the internet:
- Your client needs to know the server external IP address. So you need some way to make it configurable on the client, for example using command line arguments.
- If the local IP address of your server changes, you need to configure it to use a static IP address instead of using DHCP.
- You probably need to setup port forwarding on your internet router at the server side. You need to tell your router to forward all traffic on port 7777 to your server.
- You might need to configure your firewall to allow traffic on port 7777.
- Your ISP can and will change your external IP address occasionally. Not much you can do about that. That's why servers on the internet are usually accessed using their (domain) name, which is static.
来源:https://stackoverflow.com/questions/60301083/node-js-socket-io-based-one-to-one-chat-engine-working-fine-on-local-but-not-wo