0
我有一個春季啓動應用到我設法整合大氣聊天應用程序作爲證據的概念,要看到,豆類和CONFIGS是實際工作。Java的大氣框架 - 客戶端的onMessage從來沒有所謂的
奇怪的是,如果我用@ManagedService
設置類註釋有點不同,我不能獲得Javascript request.onMessage
函數觸發。
我試過用類似@Resume
,@Suspend
等不同的Atmosphere Services註解類的方法,但客戶端的onMessage
函數從未被解僱。 (空瀏覽器控制檯)
這是我的bean配置文件AtmosphereConfig.java
:
package my.poc.com;
import java.util.Collections;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.atmosphere.cpr.AtmosphereServlet;
import org.atmosphere.cpr.ContainerInitializer;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class AtmosphereConfig {
@Bean
public EmbeddedAtmosphereInitializer atmosphereInitializer() {
return new EmbeddedAtmosphereInitializer();
}
@Bean
public ServletRegistrationBean atmosphereServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(
new AtmosphereServlet(), "/atmurl/*");
registration.addInitParameter("org.atmosphere.interceptor.HeartbeatInterceptor"
+ ".clientHeartbeatFrequencyInSeconds", "10");
registration.setLoadOnStartup(0);
// Need to occur before the EmbeddedAtmosphereInitializer
registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
return registration;
}
@Configuration
static class MvcConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/poc.html");
}
}
private static class EmbeddedAtmosphereInitializer extends ContainerInitializer
implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
onStartup(Collections.<Class<?>> emptySet(), servletContext);
}
}
}
這是我@ManagedService
類:
package my.poc.com
import org.atmosphere.config.service.Get;
import org.atmosphere.config.service.ManagedService;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter;
@ManagedService(path = "/atmurl")
public class AtmosphereService2 {
@Get
public void myGetImpl(final AtmosphereResource resource) {
System.out.println("@Get method called by: " + resource.uuid());
AtmosphereResource currentRes = resource;
System.out.println(currentRes);
resource.addEventListener(new AtmosphereResourceEventListenerAdapter() {
@Override
public void onSuspend(final AtmosphereResourceEvent event) {
System.out.println("resource suspended: " + event.getResource());
}
@Override
public void onResume(final AtmosphereResourceEvent event) {
System.out.println("resource resumed: " + event.getResource());
}
@Override
public void onDisconnect(final AtmosphereResourceEvent event) {
if (event.isCancelled()) {
System.out.println("resource onDisconnect cancelled: " + event.getResource().uuid());
} else if (event.isClosedByClient()) {
System.out.println("resource onDisconnect closedByClient: " + event.getResource().uuid());
}
}
});
}
}
這是我poc.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Poc</title>
<script type="text/javascript" src="js/atmosphere/jquery-2.0.3.js"></script>
<script type="text/javascript" src="js/atmosphere/atmosphere.js"></script>
<script type="text/javascript" src="js/atmosphere/myAtmosphereApp.js"></script>
</head>
<body>
</body>
</html>
而且最後我的myAtmosphereApp.js
:
$(function() {
"use strict";
console.log("application_2.js start");
var socket = atmosphere;
var subSocket;
// Websocket does not work with AJP.
if (window.EventSource) {
var transport = 'sse';
} else {
var transport = 'long-polling';
}
var request = {
url : document.location.protocol + "//" + document.location.host + '/atmurl',
contentType : "application/json",
logLevel : 'debug',
transport : transport,
trackMessageLength : true,
enableProtocol : false,
fallbackTransport : 'long-polling'
};
request.onOpen = function(response) {
console.log("request.onOpen called");
}
request.onMessage = function(response) {
console.log("request.onMessage called");
}
request.onClose = function(response) {
console.log("request.onClose called");
}
subSocket = socket.subscribe(request);
});