后端更新时将数据发送到前端 [英] Send data to front end when back end updates

查看:921
本文介绍了后端更新时将数据发送到前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序的后端接收来自多个客户端的更新.发生此类更新时,应将其传达给所有其他客户端.

The backend of my web application receives updates from several clients. When such an update happens it should be communicated to all other clients.

后端更新后,如何启动从服务器到所有Web浏览器客户端的更新?

How can I initiate an update from the server to all web browser clients when my backend is updated?

我正在使用JBoss,JSF和Spring框架.

I'm using JBoss, JSF and the Spring framework.

推荐答案

查看类似的堆栈溢出问题: WebSockets与服务器发送的事件/EventSource

See similar Stack overflow quetion : WebSockets vs. Server-Sent events/EventSource

我假设,正如DarthVader所做的那样,您的前端是某种(通常)无状态的HTML页面.浏览器中的内容.如果要自动推送所有客户端更改,则有三个选项:

I'm assuming, as DarthVader did, that your frontend is a (generally) stateless HTML page of some sort. Something in a browser. If you want all clients to be pushed changes automatically, you have three options:

Comet :(已弃用)
Comet本质上提出了没有请求超时限制的AJAX请求.您发出请求,它就坐在那儿,并根据需要通过它流传输数据.这可以通过隐藏的iFrame或标准XMLHTTPRequests(jQuery可以为您包装)来完成.您可以在此处了解更多信息.>

Comet: (deprecated)
Comet is essentially making AJAX requests that have no request timeout limit. You make the request, and it sits there and streams data through it as is neccessary. This can be done with hidden iFrames or standard XMLHTTPRequests (which jQuery can wrap for you). You can read more about this method here.

长时间轮询:
本质上,您使用javascript setInterval 方法来连续轮询服务器以查找更改.只需设置一个向服务器发出标准AJAX GET请求的时间间隔,每次成功后,就相应地更新页面.

Long Polling:
Essentially, you use the javascript setInterval method to continuously poll your server for changes. Simply set an interval that does a standard AJAX GET request to the server, and upon every success, update your page accordingly.

浏览器API

  • HTML5 WebSockets
    Using any type of Event-Based backend (Twisted, EventMachine, node.js, etc) makes WebSockets the ideal solution. Simply have all clients register with the backend, and upon a submit from any given client, push the changes to all other clients. You can read more (and see a nice example) of WebSockets on this page. Browser support => canIuse

服务器发送事件(SSE)
使用服务器发送的事件,可能服务器通过将消息推送到网页来随时将新数据发送到网页.这些传入消息可被视为网页内的事件+数据. 浏览器支持=> canIuse

Server-sent event (SSE)
With server-sent events, it's possible for a server to send new data to a web page at any time, by pushing messages to the web page. These incoming messages can be treated as Events + data inside the web page. Browser suppport => canIuse

这篇关于后端更新时将数据发送到前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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