json 设计基于 Websocket 的 API 的良好实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4234274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:40:21  来源:igfitidea点击:

Good practices for the design of a Websocket based API

jsonapiwebsocket

提问by Julien Genestoux

We're currently implementing a WebSocketbased API in our application.

我们目前正在我们的应用程序中实现基于WebSocket的 API 。

So far, we offer a REST API, as well as an XMPP API and we expect to offer similar features, but we're not sure about the design of it.

到目前为止,我们提供了一个 REST API 和一个 XMPP API,我们希望提供类似的功能,但我们不确定它的设计。

We will use a JSONdata format, but that's pretty much the only thing we know for sure. Is there any good practices about this?

我们将使用一种JSON数据格式,但这几乎是我们唯一确定的。这方面有什么好的做法吗?

For example, the REST API has the great advantage of using HTTP verbs and URL resources to describe what's being done. Websocket don't have any of these. Once the connection is established, the URL become irrelevant.

例如,REST API 具有使用 HTTP 动词和 URL 资源来描述正在执行的操作的巨大优势。Websocket 没有这些。一旦建立连接,URL 就变得无关紧要了。

Does anyone know about existing websocket based APIs?

有谁知道现有的基于 websocket 的 API?

采纳答案by Wouter Dorgelo

Yes, there is: here.

是的,有:这里

[edit: dead community wiki link removed]

[编辑:已删除社区维基链接]

回答by Ben Lachman

Here are a couple websocket based APIs/protocols:

这里有几个基于 websocket 的 API/协议:

Pusher

推杆

Slack Real Time Messaging

Slack 实时消息传递

It seems most people that are client JS facing are using some kind of event/command paired with a data object. Additionally, Slack has the idea of a message idsuch that you can associate request and response messages.

似乎大多数面向客户端 JS 的人都在使用某种与数据对象配对的事件/命令。此外,Slack 有消息的概念,id以便您可以关联请求和响应消息。