问题
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