Socket.io 和 RESTFul 如何协同工作?
(我对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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在为通过 http 映射到 REST 的操作定义处理程序。 POST 和 GET 通常指的是对实体的更新和查询。您绝对没有理由不能为这些可在两种上下文中使用的 CRUD 操作的通用版本定义一个处理程序。我通常这样做的方法是将“路由”的概念引入实时传输,并将它们映射回相同的 CRUD 处理程序。
您有一个会话,您可以施加相同的 ACL 等。
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.
我最近在我的博客上发布了此内容:
< strong>为 WebSocket 设计 CRUD API
在构建 Weld 时,我们同时使用两者REST 和 WebSocket (Socket.io)。关于 WebSocket 的三个观察:
我的解决方案:
“应用程序服务器/用户/创建”
。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:
My solution:
"AppServer/user/create"
.