是否可以通过 websockets 进行 ReST? [英] Is ReST over websockets possible?

查看:40
本文介绍了是否可以通过 websockets 进行 ReST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划开发一个基于 Web 的聊天应用程序,它接收 ReSTful 请求,将它们转换为 XMPP 并将它们传送到 XMPP 服务器.

I am planning to develop a web based chat application which takes in ReSTful requests, translate them to XMPP and deliver them to an XMPP server.

在这种基于聊天的应用程序中使用 websockets 看起来很有前景,因为事件(或响应)可以异步传递.但是如果我使用websockets作为从浏览器传输请求的底层协议,这仍然可以被认为是一种ReSTful设计吗?如果是,Websocket 消息中的 URI、动词(GET、POST...)和参数如何表示?将它们包装在 xml/json 中并发送?

Using websockets for this kind of chat based application looked promising as the events (or responses) can be delivered asynchronously. But if I use websockets as the underlying protocol for transferring the requests from the browser, can this still be considered as a ReSTful design? If yes, how are the URIs, verbs (GET, POST...), parameters represented in the websocket message? Wrap them in an xml/json and send it?

此外,ReSTful 架构声明不会在服务器上存储会话状态.但是在这种情况下,当创建 XMPP 客户端会话时,此会话的状态将存储在服务器上(违反无状态约束)

Also, ReSTful architecture states that no session state will be stored on the server. But here in this case when an XMPP client session is created, the state of this session will be stored on the server (violating the stateless constraint)

推荐答案

REST 是一种不强加协议的架构风格.所以是的,如果您愿意,您可以使用 Web Sockets 进行 REST、使用 HTTP 的 REST 和使用 FTP 的 REST.

REST is an architectural style that does not impose a protocol. So yes, you can do REST with Web Sockets, REST with HTTP and REST with FTP if you like.

使用 HTTP 的主要原因是通过 HTTP 与任何组件或编程语言进行通信非常容易且相当简单,还因为 HTTP 支持具有多个中介的分布式环境:代理、防火墙...;因此,您可以在任何拓扑上部署您的服务,并且任何人都可以访问它.

The main reason to use HTTP is that it is easy and fairly simple to communicate with any component or programming language via HTTP and also because HTTP supports distributed environments with multiple intermediaries: proxies, firewalls...; So you can deploy your service on any topology and anyone will be able to access it.

我的咆哮:如果您是 RESTliban 并且 Roy Fielding 的论文是事实的来源,那么动词永远不会被视为语义的一部分.URI 是语义.对不同动作使用不同动词是 REST over HTTP 的优雅演变,但不是真相"的一部分.您可以查看 Roy 在第 6 章中评估的基于 HTTP 的场景 他的论文.没有提到动词.请注意,这是一个评估场景,而不是规范.

My rant: If you are a RESTliban and Roy Fielding’s dissertation is the source of truth, verbs are never acknowledged as part of the semantic. URIs are the semantic. The usage of different verbs for different actions has been an elegant evolution of REST over HTTP, but not part of the "truth". You can check the scenario of rest over HTTP evaluated by Roy in chapter six of his dissertation. No mention to verbs. And notice it is an evaluation scenario, not the specification.

TLDR;

如果您需要通过 Internet 进行实时双向通信并且客户端是 Web 浏览器,则最好的选择是 Web Sockets.然后,您可以在 Web 套接字之上实现应用级协议,以实现 RESTful Web 服务.

If you need realtime two way communications via the internet and the client is a web browser, the best choice is Web Sockets. You could then implement an application level protocol on top of web sockets to implement a RESTful Web Service.

这篇关于是否可以通过 websockets 进行 ReST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆