Java SSE 服务器推送WEB页面接收数据


//服务器代码
package com.xy.inuyasha.web.controller;

import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
* Created by Administrator on 2017/3/15.
*/
@WebServlet(urlPatterns = { "/role_echo" }, asyncSupported = true)
public class SseTest extends HttpServlet {

private static final long serialVersionUID = 1L;
private final static int DEFAULT_TIME_OUT = 10 * 60 * 1000;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/event-stream");
resp.setCharacterEncoding("UTF-8");
req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);//注意这里

AsyncContext actx = req.startAsync(req, resp);
actx.setTimeout(DEFAULT_TIME_OUT);
actx.addListener(new AsyncListener() {
@Override
public void onComplete(AsyncEvent arg0) throws IOException {
// TODO Auto-generated method stub
System.out.println("[echo]event complete:" + arg0.getSuppliedRequest().getRemoteAddr());
}

@Override
public void onError(AsyncEvent arg0) throws IOException {
// TODO Auto-generated method stub
System.out.println("[echo]event has error");
}

@Override
public void onStartAsync(AsyncEvent arg0) throws IOException {
// TODO Auto-generated method stub
System.out.println("[echo]event start:" + arg0.getSuppliedRequest().getRemoteAddr());
}

@Override
public void onTimeout(AsyncEvent arg0) throws IOException {
// TODO Auto-generated method stub
System.out.println("[echo]event time lost");
}
});
new Thread(new AsyncWebService(actx)).start();
}
}

class AsyncWebService implements Runnable {
AsyncContext ctx;

public AsyncWebService(AsyncContext ctx) {
this.ctx = ctx;
}

public void run() {
try {
//等待十秒钟,以模拟业务方法的执行
Thread.sleep(10000);
PrintWriter out = ctx.getResponse().getWriter();
out.println("data:中文" + new Date() + "\r\n"); //js页面EventSource接收数据格式:data:数据 + "\r\n"

out.flush();
            ctx.complete();
} catch (Exception e) {
e.printStackTrace();
}

}

}
//jsp页面js脚本
if (!!window.EventSource) { //EventSource是SSE的客户端.此时说明浏览器支持EventSource对象
var source = new EventSource('/role_echo');//发送消息
    s = '';

source.addEventListener('message', function(e) {
      
s += e.data + "<br/>";
$("#msgFromPush").html(s);
},false);//添加客户端的监听

source.addEventListener('open', function(e) {
console.log("连接打开");
}, false);

source.addEventListener('error',function(e){
if(e.currentTarget.readyState==EventSource.CLOSED){
console.log("连接关闭");
}else{
console.log(e.currentTarget.readyState);
}
});
}else{
console.log("您的浏览器不支持SSE");
}

测试能跑通!!


posted @ 2017-03-15 15:29  shown  阅读(7703)  评论(0编辑  收藏  举报