简体   繁体   English

我们可以在具有动态 IP 的服务器上建立持久的 socket.io 连接吗?

[英]Can we establish a persistent socket.io connection over a server with a dynamic IP?

As my app is currently under development, my local computer is temporarily acting as the server.由于我的应用程序目前正在开发中,我的本地计算机暂时充当服务器。 Using the service by no-ip.com, I have managed to establish internet connection to the NodeJS server at my home, which has been supported by socket.io.使用 no-ip.com 的服务,我设法与家中的 NodeJS 服务器建立了互联网连接,socket.io 已支持该连接。 However, although the HTTP connection is fine, every now and then the socket.io connection would fail until I restart the server.但是,尽管 HTTP 连接正常,但 socket.io 连接时不时会失败,直到我重新启动服务器。 I have been investigating the cause of this.我一直在调查这件事的原因。 I wonder whether when the dynamic IP of the server changes, the socket.io which is listening to the ports fails.不知道是不是当服务器的动态IP发生变化时,监听端口的socket.io失效了。 Could someone confirm this with me?有人可以和我确认一下吗?

Its definitely not the answer of your question but i think you're gonna like it !这绝对不是你问题的答案,但我想你会喜欢的! use NGROK download & install it once it's done使用 NGROK 下载并在完成后安装它

launch your dev server启动你的开发服务器

open cmd and type打开cmd并输入

c:\>ngrok http 3000 

3000 is your dev server PORT so if its something else change it 3000 是你的开发服务器端口,所以如果它有别的东西改变它

This will give you an address like that https://xxx232xx.ngrok.io这会给你一个像https://xxx232xx.ngrok.io这样的地址

use this address to access your app now from any connected device立即使用此地址从任何连接的设备访问您的应用程序

One last thing , when you use socket.io with https domains change your config and add the address最后一件事,当您将 socket.io 与 https 域一起使用时,请更改您的配置并添加地址

Example示例

   var ioSocket = io('https://xxx232xx.ngrok.io', {

      'reconnection delay': 2500,
      'secure':true,
      'max reconnection attempts': 10,
      'reconnection':true       

       });

Hope it helps !希望它有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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