Socket.io和RESTFul如何一起工作? [英] How can Socket.io and RESTFul work together?

查看:143
本文介绍了Socket.io和RESTFul如何一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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.

然后我知道有一个名为WebSocket的东西,它是包装器socket.io.它使用消息"在客户端和服务器之间进行通信.当我单击发布"时,客户端只需调用socket.send(data)并等待服务器的client.send(data).太神奇了.但是URL呢?

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?

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

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?)

此外,我应该这样做吗?在一个非常互动的网络程序(例如游戏)中,RESTFul仍然有意义吗?

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

推荐答案

您正在为通过http映射到REST的操作定义处理程序. POST和GET通常指的是对实体的更新和查询.绝对没有理由不能只为可以在两种情况下使用的这些CRUD操作的通用版本定义处理程序.我通常这样做的方法是将路由"的概念引入实时传输,然后将其映射回相同的CRUD处理程序.

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.

您有一个会话,可以使用相同的ACL,等等.

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

这篇关于Socket.io和RESTFul如何一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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