node.js Socket.io 和 RESTFul 如何协同工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6339393/
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
How can Socket.io and RESTFul work together?
提问by Lai Yu-Hsuan
(I'm not familiar to RESTFul, please correct me if my concept is wrong)
(我对RESTFul不熟悉,如果我的概念有误请指正)
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.
在 RESTFul 架构中,我们将每个动作映射到一个 URL。如果我点击“发表文章”,它可能实际上是 URLhttp://example.com/和一些数据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.
如果我想发布但不刷新整个网页,我可以使用 javascript 的 XMLHTTPRequest。我发布它,然后获取它的内容并将其插入到我页面中的 div 中。这些动作都是异步的。
Then I know there is something named WebSocketand 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?
然后我知道有一个命名的东西WebSocket,它是 wrapper socket.io。它使用“消息”在客户端和服务器之间进行通信。当我单击“发布”时,客户端只需调用socket.send(data)并等待服务器的client.send(data). 这很神奇。但是网址呢?
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?)
可以同时使用这两个模型而不重复我自己吗?换句话说,每个动作都有它的 URL,其中一些可以与用户实时交互(通过 socket.io?)
Moreover, should I do this? In a very interactive web program(ex. games), the RESTFul is still meaningful?
此外,我应该这样做吗?在交互性很强的网络程序(例如游戏)中,RESTFul 还有意义吗?
回答by Josh
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.
您正在为通过 http 映射到 REST 的操作定义处理程序。POST 和 GET 通常是指对实体进行更新和查询。绝对没有理由不能只为这些可在两种上下文中使用的 CRUD 操作的通用版本定义处理程序。我通常这样做的方法是将“路由”的概念引入实时传输,并将它们映射回相同的 CRUD 处理程序。
You have a session, you can impose the same ACL, etc.
您有一个会话,您可以强加相同的 ACL 等。
?+---------------------------------+
?|?????????????????????????????????|
?|??????BROWSER????????????????????|
?|?????????????????????????????????|
?+--+--^-------------------+---^---+
????|??|???????????????????|???|
????|??|???????????????????|???|
?+--v--+---+????????????+--v---+---+
?|?????????|????????????|??????????|
?|?HTTP????|????????????|?SOCKET.IO|
?+--+---^--+????????????+--+---^---+
????|???|??????????????????|???|
?+--v---+------------------v---+---+
?|?????????????????????????????????|
?|????????ROUTING/PUBSUB???????????|
?+-+--^-------+--^-------+--^------+
???|??|???????|??|???????|??|
?+-v--+--+??+-v--+--+??+-v--+-+
?|???????|??|???????|??|??????|
?|?USERS?|??|?ITEMS?|??|ETC???|
?+-------+??+-------+??+------+
?????ENTITY?CRUD?HANDLERS
回答by Tom S?derlund
I posted this on my blogrecently:
我最近在我的博客上发布了这个:
Designing a CRUD API for WebSockets
为 WebSockets 设计 CRUD API
When building Weld, we are using both REST and WebSockets (Socket.io). Three observations on WebSockets:
在构建Weld 时,我们同时使用 REST 和 WebSockets (Socket.io)。关于 WebSockets 的三个观察:
- Since WebSockets are so free-form, you can name events how you want but it will eventually be impossible to debug.
- 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.
- 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.
- 由于 WebSocket 的形式非常自由,您可以根据需要命名事件,但最终将无法调试。
- WebSockets 没有 HTTP 的请求/响应形式,因此有时很难判断一个事件来自哪里,或要去哪里。
- 如果 WebSockets 能够适应应用程序中现有的 MVC 结构,最好使用与 REST API 相同的控制器。
My solution:
我的解决方案:
- I have two routing files on my server: routes-rest.jsand 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.
- 我的服务器上有两个路由文件:routes-rest.js和routes- sockets.js
- 我的事件是这样的例子:
"AppServer/user/create"。 - 我使用正斜杠(“/”)使事件看起来像路由路径。
- 第一个字符串是目标(~“主机名”,如果这实际上是一个路径)。
- 第二个字符串是模型。
- 第三个字符串是CRUD 动词:即创建、读取、更新、删除。

