通知浏览器有关服务器上的事件
我有一个基于java的网络应用程序(struts 1.2)。我需要在前端(jsp)上显示状态。现在,我的服务器收到另一台服务器通知的状态可能会发生变化。但我希望将此状态更改通知给浏览器。
我不想每隔一段时间就刷新一次。相反,我必须实现类似 gmail 聊天中所做的事情,即。浏览器通过服务器上的事件变化得到通知。
关于如何解决这个问题有什么想法吗?
我正在考虑向服务器发出状态请求,并且在服务器端我会保留该请求,直到状态发生变化才会响应。有这方面的指点、例子吗?
I have a java based web application(struts 1.2). I have a requirement to display a status on the frontend (jsp). Now the status might change which my server gets notified by another server. But I want this status change to be notified to the browser.
I don't want to make a refresh at intervals. Rather I have to implement something like done in gmail chat, ie. the browser gets notified by changing events on the server.
Any ideas on how to go about this?
I was thinking on lines of opening a request to server for status, and at the server end I would hold the request and wouldn't respond back until there is a status change. Any pointers, examples on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好的解决方案是使用 XMPP 协议。它是标准化的,许多开源解决方案将让您在几分钟内开始使用。您可以使用 Smack、StropeJS 和 Openfire 来获取您的基于java的应用程序可以按需要工作。
Best possible solution will be to make use of XMPP protocol. It's standardized and a lot of open source solutions will get you started within minutes. You can use combination of Smack, StropheJS and Openfire to get your java based app work as desired.
有一种方法称为长轮询(Comet)。它基本上向服务器发送请求。服务器上创建的请求线程只是等待用户的新数据,时间限制可能为 1 分钟或更长时间。当新数据可用时,它会被返回。
主要问题是解决服务器端问题,您不希望每个用户都有一个线程来等待新数据。当然,您可以根据您的后端使用一些异步方法。
参考: http://en.wikipedia.org/wiki/Push_technology
另一种方法是使用WebSockets。问题是目前并非所有浏览器都支持它。
There's a method called Long Polling (Comet). It basically sends a request to the server. The request thread created on the server simply waits for new data for the user, with a time limit of maybe 1 minute or more. When new data is available it is returned.
The main problem is to tackle the server-side issue, you don't want to have one thread for every user just waiting for new data. Of course you could use some asynchronous methods depending on your back-end.
Ref: http://en.wikipedia.org/wiki/Push_technology
Alternative way would be to use WebSockets. The problem is that it's not supported by all browsers today.