何时在node.js中使用TCP和HTTP? [英] When to use TCP and HTTP in node.js?
问题描述
愚蠢的问题,但只是在这里确定:
Stupid question, but just making sure here:
我应该何时使用TCP over HTTP?有没有一个比另一个好的例子?
When should I use TCP over HTTP? Are there any examples where one is better than the other?
推荐答案
TCP是全双工双向通信。 HTTP使用请求/响应模型。让我们看看你是在写一个聊天或消息应用程序。 TCP可以更好地工作,因为您可以立即通知客户端。使用HTTP时,你必须做一些像长轮询这样的技巧。
TCP is full-duplex 2-way communication. HTTP uses request/response model. Let's see if you are writing a chat or messaging application. TCP will work much better because you can notify the client immediately. While with HTTP, you have to do some tricks like long-polling.
然而,TCP只是字节流。您必须在其上找到另一个协议来定义您的消息。您可以使用Google的ProtoBuffer。
However, TCP is just byte stream. You have to find another protocol over it to define your messages. You can use Google's ProtoBuffer for that.
这篇关于何时在node.js中使用TCP和HTTP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!