Socket.io 和 RESTFul 如何协同工作?

发布于 2024-11-15 10:28:47 字数 608 浏览 6 评论 0原文

(我对RESTFul不太熟悉,如果我的概念有误,请纠正我)

在RESTFul架构中,我们将每个操作映射到一个URL。如果我点击“发布文章”,可能它实际上是 URL http://example.com/ 和一些数据 action=post&content=blahblah

如果我想发布,但不刷新整个网页,我可以使用javascript的XMLHTTPRequest。我发布它,然后获取它的内容并将其插入到我的页面中的 div 中。这些动作都是异步的。

然后我知道有一个名为 WebSocket 的东西,它是包装器 socket.io。它使用“消息”在客户端和服务器之间进行通信。当我点击“post”时,客户端只需调用 socket.send(data) 并等待服务器的 client.send(data) 。这很神奇。但是网址呢?

可以同时使用这两种模型而不重复自己吗?换句话说,每个动作都有它的URL,其中一些动作可以与用户实时交互(通过socket.io?)

此外,我应该这样做吗?在交互性很强的网页程序(例如游戏)中,RESTFul 仍然有意义吗?

(I'm not familiar to RESTFul, please correct me if my concept is wrong)

In RESTFul architecture, we map every action to an URL. If I click "post a article", may it's actually URL http://example.com/ and some data action=post&content=blahblah.

If I want to post, but not refresh the whole web page, I can use javascript's XMLHTTPRequest. I post it and then get it's content and insert it to a div in my page. These action is all asynchronous.

Then I know there is something named WebSocket and it's wrapper socket.io. It use "message" to communicate between client and server. When I click "post" the client just call socket.send(data) and wait for server's client.send(data). It's magical. But how about URL?

It's possible to use the two model both without repeating myself? In other word, every action has it's URL, and some of them can interact with user real-timely(by socket.io?)

Moreover, should I do this? In a very interactive web program(ex. games), the RESTFul is still meaningful?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅冇旳回忆 2024-11-22 10:28:47

您正在为通过 http 映射到 REST 的操作定义处理程序。 POST 和 GET 通常指的是对实体的更新和查询。您绝对没有理由不能为这些可在两种上下文中使用的 CRUD 操作的通用版本定义一个处理程序。我通常这样做的方法是将“路由”的概念引入实时传输,并将它们映射回相同的 CRUD 处理程序。

您有一个会话,您可以施加相同的 ACL 等。

 +---------------------------------+
 |                                 |
 |      BROWSER                    |
 |                                 |
 +--+--^-------------------+---^---+
    |  |                   |   |
    |  |                   |   |
 +--v--+---+            +--v---+---+
 |         |            |          |
 | HTTP    |            | SOCKET.IO|
 +--+---^--+            +--+---^---+
    |   |                  |   |
 +--v---+------------------v---+---+
 |                                 |
 |        ROUTING/PUBSUB           |
 +-+--^-------+--^-------+--^------+
   |  |       |  |       |  |
 +-v--+--+  +-v--+--+  +-v--+-+
 |       |  |       |  |      |
 | USERS |  | ITEMS |  |ETC   |
 +-------+  +-------+  +------+
     ENTITY CRUD HANDLERS

You're defining a handler for actions that map to REST over http. POST and GET generally refer to update and query over an entity. There's absolutely no reason you can't just define a handler for generic versions of these CRUD operations that can be used in both contexts. The way I generally do this is by introducing the concept of a 'route' to the real-time transport, and mapping those back to the same CRUD handlers.

You have a session, you can impose the same ACL, etc.

 +---------------------------------+
 |                                 |
 |      BROWSER                    |
 |                                 |
 +--+--^-------------------+---^---+
    |  |                   |   |
    |  |                   |   |
 +--v--+---+            +--v---+---+
 |         |            |          |
 | HTTP    |            | SOCKET.IO|
 +--+---^--+            +--+---^---+
    |   |                  |   |
 +--v---+------------------v---+---+
 |                                 |
 |        ROUTING/PUBSUB           |
 +-+--^-------+--^-------+--^------+
   |  |       |  |       |  |
 +-v--+--+  +-v--+--+  +-v--+-+
 |       |  |       |  |      |
 | USERS |  | ITEMS |  |ETC   |
 +-------+  +-------+  +------+
     ENTITY CRUD HANDLERS
碍人泪离人颜 2024-11-22 10:28:47

我最近在我的博客上发布了此内容

< strong>为 WebSocket 设计 CRUD API

在构建 Weld 时,我们同时使用两者REST 和 WebSocket (Socket.io)。关于 WebSocket 的三个观察:

  1. 由于 WebSocket 的格式非常自由,因此您可以按照自己的意愿命名事件,但最终将无法进行调试。
  2. WebSocket 没有 HTTP 的请求/响应形式,因此有时很难判断事件来自或去往何处。
  3. 如果 WebSocket 能够适应应用程序中现有的 MVC 结构,最好使用与 REST API 相同的控制器,那就太好了。

我的解决方案:

  • 我的服务器上有两个路由文件:routes-rest.jsroutes-sockets.js
  • 我的事件如下示例所示:“应用程序服务器/用户/创建”
  • 我使用正斜杠(“/”)使事件看起来像路由路径。
  • 第一个字符串是目标(如果这实际上是路径,则为“主机名”)。
  • 第二个字符串是型号
  • 第三个字符串是CRUD动词:即创建、读取、更新、删除。

I posted this on my blog recently:

Designing a CRUD API for WebSockets

When building Weld, we are using both REST and WebSockets (Socket.io). Three observations on WebSockets:

  1. Since WebSockets are so free-form, you can name events how you want but it will eventually be impossible to debug.
  2. WebSockets don’t have the request/response form of HTTP so sometimes it can be difficult to tell where an event is coming from, or going to.
  3. It would be nice if the WebSockets could fit into the existing MVC structure in the app, preferably using the same controllers as the REST API.

My solution:

  • I have two routing files on my server: routes-rest.js and routes-sockets.js
  • My events look like this example: "AppServer/user/create".
  • I use forward slashes (“/”) to make the events look like routing paths.
  • The first string is the target (~”host name” if this actually was a path).
  • The second string is the model.
  • The third string is the CRUD verb: i.e. create, read, update, delete.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文