gpt4 book ai didi

android - 可以将 HTTP REST API 用于聊天应用程序吗?

转载 作者:IT老高 更新时间:2023-10-28 23:20:15 24 4
gpt4 key购买 nike

我们正在 Android 上构建一个聊天应用程序。我们正在考虑使用 HTTP REST API 发送出站消息。想知道与使用 WebSocket 或 XMPP(这似乎更像是传输聊天消息的事实上的标准)相比,它是一种好方法还是有什么缺点?

我能想到的一些优点/缺点是:

  • HTTP 端点很容易在服务器端水平扩展(这是主要问题)
  • 与 HTTP 相比,Websockets 的学习曲线更加陡峭
  • 与 WebSocket 相比,HTTP 消息的负载更大

根据本文档,似乎 Facebook 最初也使用 AJAX 来处理聊天消息:

https://www.erlang-factory.com/upload/presentations/31/EugeneLetuchy-ErlangatFacebook.pdf

最佳答案

我们可以使用 REST API 进行聊天消息传递,但恕我直言,XMPP 是更好的选择。让我们考虑一下 XMPP 可以提供什么。

XMPP 除了支持 TCP 传输外,还提供 HTTP(通过 pollingbinding)和 websocket 传输。阅读 XMPP via HTTP and WebSocket transports

从 XMPP 的角度了解每种传输方式的优缺点会很有趣。

XMPP could use HTTP in two ways: polling[18] and binding.

XMPP over HTTP 轮询

The polling method, now deprecated, essentially implies messages stored on a server-side database are being fetched (and posted) regularly by an XMPP client by way of HTTP 'GET' and 'POST' requests.


XMPP over HTTP 绑定(bind) (BOSH)

绑定(bind)方法被认为比轮询方法中的常规 HTTP 'GET' 和 'POST' 请求更有效,因为与其他 HTTP 轮询技术相比,它减少了延迟和带宽消耗

但是,这也带来了一个缺点,即套接字会长时间保持打开状态,等待客户端的下一个请求

The binding method, implemented using Bidirectional-streams Over Synchronous HTTP (BOSH),[19] allows servers to push messages to clients as soon as they are sent. This push model of notification is more efficient than polling, where many of the polls return no new data.

如果我们能理解 the BOSH technique作品。

The technique employed by BOSH, which is sometimes called "HTTP long polling", reduces latency and bandwidth consumption over other HTTP polling techniques. When the client sends a request, the connection manager does not immediately send a response; instead it holds the request open until it has data to actually send to the client (or an agreed-to length of inactivity has elapsed). The client then immediately sends a new request to the connection manager, continuing the long polling loop.

If the connection manager does not have any data to send to the client after some agreed-to length of time [12], it sends a response with an empty . This serves a similar purpose to whitespace keep-alives or XMPP Ping (XEP-0199) [13]; it helps keep a socket connection active which prevents some intermediaries (firewalls, proxies, etc) from silently dropping it, and helps to detect breaks in a reasonable amount of time.


XMPP over WebSocket 绑定(bind)

XMPP 支持 WebSocket 绑定(bind),这是一种更高效的传输方式

A perhaps more efficient transport for real-time messaging is WebSocket, a web technology providing for bi-directional, full-duplex communications channels over a single TCP connection. XMPP over WebSocket binding is defined in the IETF proposed standard RFC 7395.

说到学习曲线,是的,您可能很想使用 REST API,但现在有几个资源可以了解 Android and XMPP , 和 XMPP server softwares您可以使用它在 Internet 或局域网上运行自己的 XMPP 服务。在决定架构之前花费这些精力是值得的。

关于android - 可以将 HTTP REST API 用于聊天应用程序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29217209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com