这是我第一次与大气合作,对我来说这很艰难。我想要的是,一个用户从我的API(Java Servlet / Tomcat)接收推送消息。
我正在使用jquery.atmosphere.js和以下应用程序代码:
checkForMenuUpdates: function () {
var self = this;
checkUpdates();
function checkUpdates() {
var request = {url: path.apiPath + 'user.checkUpdates?token=' + token,
logLevel: 'debug',
'enableXDR': true,
transport: 'sse',
trackMessageLength: "false",
fallbackTransport: 'long-polling'};
request.onOpen = function (response) {
};
request.onMessage = function (response) {
}
var socket = jqueryAtmosphere;
var subSocket = socket.subscribe(request);
}
},
服务器端:控制器:
else if ("user.checkUpdates".equals(pathInfo)) {
try {
AtmosphereBroadcaster atmo=new AtmosphereBroadcaster();
atmo.subscribe(response);
//ServerSentEvents updates = new ServerSentEvents();
//updates.establishConnection(token.getUserId(), response, callback);
} catch (Exception ex) {
ErrorHandler error = new ErrorHandler();
error.Error400(response, callback);
}
}
“广播员”:
private @PathParam("token")
Broadcaster topic;
public SuspendResponse<String> subscribe(HttpServletResponse httpResponse) {
StandardResponse res = new StandardResponse();
httpResponse = res.standardResponseSSE(httpResponse);
return new SuspendResponse.SuspendResponseBuilder<String>()
.broadcaster(topic)
.outputComments(true)
.addListener(new EventsLogger())
.build();
}
public static void push(String message, String topic) {
Collection<Broadcaster> broadcasters = BroadcasterFactory.getDefault().lookupAll();
for (Broadcaster b : broadcasters) {
System.out.println(b.toString());
}
System.out.println("Request to push- Message: " + message + ", Topic: " + topic);
Broadcaster b = null;
if (null != (b = BroadcasterFactory.getDefault().lookup(JerseyBroadcaster.class, topic))) {
System.out.println("Request to push- Message: " + message + ", Topic: " + topic);
b.broadcast(message + "\n");
}
}
private class EventsLogger implements AtmosphereResourceEventListener {
@Override
public void onPreSuspend(AtmosphereResourceEvent are) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onSuspend(AtmosphereResourceEvent are) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onResume(AtmosphereResourceEvent are) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onDisconnect(AtmosphereResourceEvent are) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onBroadcast(AtmosphereResourceEvent are) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onThrowable(AtmosphereResourceEvent are) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onClose(AtmosphereResourceEvent are) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void onHeartbeat(AtmosphereResourceEvent are) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
和StandardResponse:
public HttpServletResponse standardResponseSSE(HttpServletResponse response) {
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setDateHeader("Expires", -1);
return response;
}
而且我从chrome控制台获得以下输出:
SSE成功打开jquery.atmosphere.js?bust = 6539327597711.235:3171 SSE重新连接最大尝试次数达到6
对我来说,似乎没有退货消息。我有一个非常基本的示例(https://weblogs.java.net/blog/swchan2/archive/2014/05/21/server-sent-events-async-servlet-example),它运行良好。但是对我来说,SSE不适用于大气框架。
那么,这有什么意义?我在做什么错?
编辑:
我知道JavaScript代码很奇怪-它的“正在建设中”:)
当然-当我将此代码添加到EventsLogger时,它正在工作:
public EventsLogger(HttpServletResponse response) {
PrintWriter write = null;
try {
write = response.getWriter();
while (true) {
write.write("data:Test \n\n");
write.flush();
Thread.sleep(1000);
}
} catch (IOException ex) {
Logger.getLogger(AtmosphereBroadcaster.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(AtmosphereBroadcaster.class.getName()).log(Level.SEVERE, null, ex);
} finally {
write.close();
}
}
心跳出现问题,无法正确发送-如何解决此问题?
停止执行:
throw new UnsupportedOperationException("Not supported yet.");
在侦听器中,它将起作用:-)