如何在JSP页面中实时显示通知 [英] How to show notifications in a JSP page in real time
问题描述
我需要通过其他事件向其他用户显示有关预订事件的通知.我想以类似Facebook的方式进行操作:每当有人通过我的门户网站进行预订时,我希望该预订作为弹出窗口显示给正在使用我的应用程序的另一个用户.
I need to show notifications on booking by client events to some other user. I want to do it in a facebook-like style: whenever someone makes a booking through my portal, I want that booking to be shown as a popup to another user who is using my application.
我该如何实现?我正在使用Spring 3.0,JSP和jQuery.
How can I achieve this? I'm using Spring 3.0, JSP and jQuery.
推荐答案
如评论中所述,您可以进行长时间轮询(或多或少意味着:客户端向服务器发送请求,并保持打开状态,直到服务器响应为止)进行更新,然后在收到响应的客户端发送另一个请求后立即进行轮询或短轮询(客户端每隔X秒便向服务器ping请求到服务器以请求更新,服务器会立即以是/否"进行响应).
As said in comments, you can either go with long polling (more or less means: client sends a request to server and it is kept open until the server responds with an update, and then as soon as a response received client send another request) or short polling (every X seconds your client pings a request to your server to ask for updates, server responds immediately with yes/no).
Web套接字的受欢迎程度/支持在不断增长,但不支持此版本,因此取决于您要支持的浏览器(
Web sockets are growing in popularity/support but not supported pre ie10 so depends on what browsers you want to support (http://en.m.wikipedia.org/wiki/WebSocket)
使用jquery进行短轮询非常简单,并且可以以任何刷新率对服务器进行ajax请求.
Short polling is incredibly simple with jquery, and can just ajax a request to your server at whatever refresh rate.
编辑
如果您不打算使用websockets之类的东西(例如,因为您必须支持较旧的浏览器),那么我建议您使用短轮询而不是像彗星这样的传统长轮询方法.
If you are not going for something like websockets (because you have to support older browsers for example), then I would recommend using short polling rather than the traditional long polling approach such as comet.
请参见此处,获得简短的长短轮询评论: https://stackoverflow.com/a/4714111/258813基本上,如果需要立即通知人们,长时间轮询很有用,但是它更复杂且占用大量内存-这意味着每个登录的用户都将与服务器建立开放连接,因此您在服务器中的开放连接数量将受到限制将支持.
See here for a succinct long vs short polling commentary: https://stackoverflow.com/a/4714111/258813 Basically, long polling is useful if people need to be notified immediately, but is more complex and memory intensive - it means every logged in user will have an open connection to the server so you will be limited in the number of open connections your server will support.
短轮询目前可能是解决您所描述的问题的最常用方法-例如,如果您转到Twitter并打开chrome开发人员工具/萤火虫(无论浏览器上有什么开发工具),然后等待几秒钟,将会在后台检查定期更新的ajax请求.
Short polling is currently probably the most common approach to solving the problem you describe - if you go to twitter for example and open up chrome developer tools/firebug (whatever dev tools you have on your browser) and wait a few seconds you will see the ajax requests being made regularly in the background checking for updates.
如果您可以处理几秒钟的通知延迟,那么短轮询确实很简单,这是一个基本示例:
If you can cope with a few seconds lag in notification, then short polling is really simple, here is a basic example:
在页面加载时,在您的JS中调用它:
On page load, call this in your JS:
setInterval(checkForNotifications, 10000);
以上内容将每10秒调用一次函数checkForNotifications()
(显然,您可以将10秒更改为任意时间长度-只需根据希望用户更新的频率来调整上一行中的10000).
The above will call a function checkForNotifications()
every 10 seconds (obviously, you can change the 10 seconds to any length of time - just adjust the 10000 in the above line according to how regular you want users to be updated).
然后,在您的checkForNotifications
函数中,仅对服务器进行ajax调用,询问是否有任何更新-如果服务器说是,则仅使用JS显示警报(如果没有,则显示警报).时间最有可能),则什么也不做:
Then, in your checkForNotifications
function, just make an ajax call to your server asking if there are any updates - if the server says yes, then just display the alert using JS, if not (which will be most of the time most likely) then do nothing:
function checkForNotifications(){
$.ajax({
url: "your/server/url",
type: "GET",
success: function( notification ) {
//Check if any notifications are returned - if so then display alert
},
error: function(data){
//handle any error
}
});
}
这篇关于如何在JSP页面中实时显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!