Socket 编程与 HTML5 WebSocket 和 REST 结合的未来 [英] Future of Socket Programming and HTML5 WebSocket and REST Combined

查看:31
本文介绍了Socket 编程与 HTML5 WebSocket 和 REST 结合的未来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 PHP 创建了简单的 Socket Server 并用它制作了聊天应用程序.

I have created simple Socket Server in PHP and Make Chat Application with that.

套接字服务器在某个端口上工作,例如127.0.0.1:9999所有客户端都使用该 IP:PORT 和 Shake-hand 连接到服务器并相互通信.

Socket server working on some port e.g. 127.0.0.1:9999 All clients connect with that IP:PORT and Shake-hand to server and communicate with each other.

我的概念:

1) Browser => 通过 ( WebSocket ) 获取实时通知2) User => 发表评论(想要使用 REST 或简单将评论保存在数据库中)将我的帖子保存到数据库后,它应该在没有任何 HTTP 请求(仅通过 WebSocket)的情况下向所有用户 GUI 广播.

1) Browser => Get Real-time Notifications by ( WebSocket ) 2) User => Post Comments ( Want to Save Comment in Database either using REST or simple ) After saving my Post to Database It should be broadcast to all users GUI without any HTTP Request ( By Only WebSocket ).

所以,我的问题是,当用户在我的 WebSocketServer 中发表评论时如何处理第二部分.

So, My question is regarding, How to handle Second Part when User post comment inside my WebSocketServer.

推荐答案

我猜你可能想到的是这样的:

I guess what you might have in mind is something like this:

<代码>浏览器|+---->REST/HTTP --->网络服务器 (REST)|||(*)|||v+-<--- WebSocket <--- WebSocket 服务器

浏览器做 REST/HTTP 请求,同时维护一个 WebSocket 连接来接收实时通知.

The browser does REST/HTTP requests, and at the same time maintains a WebSocket connection to receive real-time notifications.

浏览器向您的 Web/REST 服务器发出 REST/CRUD 请求,当 REST 服务器执行 CRUD 操作时,它不仅会响应请求者,还会通过 WebSocket 向其他人发送通知.

The browser does REST/CRUD requests to your Web/REST server, and when the REST server performs a CRUD operation, it not only answers to the requestor, but also sends out notification to others via WebSocket.

有两个重要方面:

首先,WebSocket 是不够的,因为它只是原始的点对点消息传递).您可能需要一些发布和订阅"模式在 WebSocket 之上,以便对给定资源的 CRUD 操作可以仅向那些感兴趣并允许的浏览器/WebSocket 客户端发送通知接收相应资源的通知.

First, WebSocket isn't enough, since it is only raw point-to-point messaging). You likely want some "Publish & Subscribe" schema on top of WebSocket, so that the CRUD operation on a given resource can send out a notification only to those browser/WebSocket clients that are interested and allowed to receive notification for the respective resource.

第二,根据您的 Web 服务器的技术,您希望使 () 尽可能简单(例如通过一个简单的 HTTP 请求来自来执行()您的 Web 服务器 WebSocket 服务器).

Second, depending on the technology of your Web server, you want to make the () as easy as possible (e.g. doing () by a simple HTTP request from your Web server to the WebSocket server).

以上两个都提供,例如来自 Crossbar.io:

Both of above is provided e.g. by Crossbar.io:

  • Crossbar.io 是一个 WAMP 路由器,WAMP(Web 应用程序消息传递协议")提供发布 & 通过 WebSocket 订阅".
  • Crossbar.io 有一个内置的 HTTP-to-WAMP 桥接器,允许您通过执行普通的旧 HTTP 请求进行发布.
  • Crossbar.io is a WAMP router, and WAMP ("The Web Application Messaging Protocol") provides "publish & subscribe" over WebSocket.
  • Crossbar.io has a builtin HTTP-to-WAMP bridge that allows you to publish by doing a plain old HTTP request.

请参阅此处示例.

披露:我隶属于 WAMP 和 Crossbar.io.

Disclosure: I am affiliated with WAMP and Crossbar.io.

这篇关于Socket 编程与 HTML5 WebSocket 和 REST 结合的未来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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