Using an HTTP connection as a simple duplex socket in Node

拜拜、爱过 提交于 2019-12-13 04:33:50

问题


Say we're on a network where only HTTP is allowed, what's the simplest way to use the http module as net module (TCP) socket/stream? Between two Node servers, I'd like is to be able to send data using write() and receive this data with on('data',...) at the other end continuously. Currently, I can write once to each end then subsequent writes don't seem to send.


回答1:


HTTP in its very nature is uni-directional. The best you can get would be two uni-directional channels, without realtime communication.

You can use Server-Sent events(EventSource), which is a part of HTML5 standard. It uses HTTP to transport messages. You should also look at websockets which are similar to tcp sockets. They offer full-duplex realtime communication. They use a different websocket protocol, but can use same ports as http. If websocket is not blocked, you should use it.

Here is a comparison between the two : WebSockets vs. Server-Sent events/EventSource



来源:https://stackoverflow.com/questions/18200077/using-an-http-connection-as-a-simple-duplex-socket-in-node

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