How to make request from server to devices behind router

℡╲_俬逩灬. 提交于 2019-12-06 15:54:55

Here are some of the options available. I'm assuming that the home network is a typical home installation where incoming connections from the internet are blocked by the home router/firewall by default.

Home Application Polls Server

Since the application running on the home network can connect out to the internet, you could turn the connection around so that the application on the home network connects out to your server on the internet. Depending upon the application and needs, the home application could make a simple HTTP connection to your server, once a day, once an hour, once every 5 minutes, etc... depending upon the situation. If the server has nothing for the home application, then it just returns a simple response that it has no data. If the server has some data for the home application, then it just returns that data when the incoming request comes in.

The main drawback of this scheme is that the server must wait until the next client polling interval before it can deliver data to the client.

Home Application Connects webSocket or socket.io connection the Server

Again, the home application connects to the server, but this time it creates a webSocket or socket.io connection. That connection can then be held open for as long as you like. Once this connection is open, the server is free to send the client data at any time. If the connection is interrupted at any time, the client just re-establishes a new connection. This costs pretty much nothing on the client side and a very small amount of resources on the server (a little memory per connection).

Properly configured servers can handle hundreds of thousands of simultaneous webSocket connections. Specially configured servers can even handle millions of connections.

Open Hole in Home Router/Firewall

In this case, the home router/firewall is configured to "port forward" incoming requests on a specific port (probably not port 80) to the home application. In this case, the home application must have previous registered its IP address with the server and have properly configured the home router/firewall to permit direct access on a specific port to the home application. Once that is all configured properly, the server can connect directly to the home application.

Because regular consumer end-users will often have trouble configuring the router/firewall to permit this access and troubleshooting is difficult, this is not a very commonly selected option.

Hybrid Polling

There is also a hybrid approach where the home client regularly sends a UDP packet to the server. This UDP packet causes the home router/firewall to open up a return path from that same server back to the home client. That return path will stay open for some period of time and during that time interval the server can send a UDP packet to the client and the router/firewall will let it in. This is a derivation of a scheme used by some peer-to-peer protocols in order to get through home routers/firewalls. Because this is UDP, not TCP the client/server are responsible for their own delivery reliability.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!