我想在 java web 应用程序中实现长轮询。基本上,当用户登录时,我希望他连接到通知服务。我想在新通知发生时从服务器向他推送新通知,我希望他实时看到这些通知。 (如此短的轮询或定期从客户端检查服务器是不够的)。
我该怎么做?本质上,我想要一种从服务器推送字符串消息并让客户端立即接收它的方法。
我听说过一些引用资料表明这可以使用来自服务器的“http block 传输” header 来完成。但如何在客户端上进行设置?
最佳答案
虽然我回答晚了,但我会继续并附上我的答案。如果您使用的是 HTML 5, 尝试使用 HTML 5 - Server Sent Events (SSE) 以及 HTML 5 - Web Workers . 请注意,截至目前,MS IE 不支持 SSE。
在 Struts2 端,可能有一个标准操作来处理请求。
另见 question . 也可以看到当前兼容性here . 有demo here ,我们可以在其中看到正在发出的周期性请求(在网络监视器中),并很好地了解可以做什么。
另请注意,URL(事件源)只能分配一次。
更新 :根据我另外查看的内容(参见 here 和 here ),SSE 实际上保持连接以便从服务器接收定期更新。因此,服务器也可以有一个“无限循环”,一直持续到客户端终止,然后尝试重新连接,在这种情况下,服务器可以再次重新发送事件。
确定客户端错过了哪些事件应由实现处理(如有必要)。
这是一个链接 demos SSE with a connection being maintained . 根据 spec我们还应该能够在 header 中发送“Last-Event-ID”。但是还没有找到使用它的例子!
更新 2:一个使用 HttpServletResponse 保持连接并返回响应的示例,另一个使用 s2 流结果重复轮询操作并返回响应的示例。关于没有保持连接时的轮询频率,chrome 似乎是 3 秒,而 firefox 的要大得多。
1.) SSE.java
public class SSE extends ActionSupport {
public String handleSSE() {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");
System.out.println("Inside handleSSE()+suscribe "+Thread.currentThread().getName());
int timeout = 15*1000;
long start = System.currentTimeMillis();
long end = System.currentTimeMillis();
while((end - start) < timeout) {
try {
PrintWriter printWriter = response.getWriter();
printWriter.println( "data: "+new Date().toString() );
printWriter.println(); // note the additional line being written to the stream..
printWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
end = System.currentTimeMillis();
}
System.out.println("Exiting handleSSE()-suscribe"+Thread.currentThread().getName());
return SUCCESS;
}
}
2.) SSES2.java
public class SSES2 extends ActionSupport {
private InputStream sseStream;
public InputStream getSseStream() {
return sseStream;
}
public String handleSSES2() {
System.out.println("Inside handleSSES2() ");
String result = "data: "+new Date().toString() + "\n\n";
sseStream = new ByteArrayInputStream(result.getBytes() );
System.out.println("Exiting handleSSES2() ");
return SUCCESS;
}
}
3.) struts.xml
<struts>
<include file="strutsBase.xml"/>
</struts>
4.) strutsBase.xml
<struts>
<!-- Configuration for the default package. -->
<package name="strutsBase" extends="struts-default" >
<!-- Default interceptor stack. -->
<default-interceptor-ref name="basicStack"/>
<action name="suscribe" class="com.example.struts2.sse.action.SSE" method="handleSSE">
<result name="success">/view/empty.txt</result>
<!-- we don't need the full stack here -->
</action>
<action name="suscribeStateless" class="com.example.struts2.sse.action.SSES2" method="handleSSES2">
<result name="success" type="stream">
<param name="contentType">text/event-stream</param>
<param name="inputName">sseStream</param>
</result>
</action>
</package>
</struts>
sse.html
<!doctype html>
<meta charset="utf-8">
<title>EventSource demo</title>
<h1>new EventSource() for S2</h1>
<p><output id="result">OUTPUT VALUE</output></p>
<script>
(function(global, window, document) {
'use strict';
function main() {
window.addEventListener('DOMContentLoaded', contentLoaded);
}
function contentLoaded() {
var result = document.getElementById('result');
var stream = new EventSource('suscribe.action');
stream.addEventListener('message', function(event) {
var data = event.data;
result.value = data;
});
}
main();
})(this, window, window.document);
</script>
<!--
Also See :
http://server-sent-events-demo.herokuapp.com/
-->
sseAction.html
Same as sse.html except that EventSource url is 'suscribeStateless.action' instead of suscribe.action
关于java - 如何使用 Struts 2 在 Java 中实现长轮询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18794547/