我正在使用amoat尝试使用在eclipse环境中本地运行的atmosphere 2.0.3 tomcat 7.0.42获得简单的基本实现(也可以从外部计算机连接以查看wireshark的流量).无论我使用哪种传输方式,websocket,sse,轮询,长轮询,我都遇到的问题是,广播响应似乎永远不会到达客户端和响应.永远不会调用OnMessage处理程序.我在运行时没有收到异常,并且我尝试使用firefox / chrome /和IE.我还使用了Wirehark,在包含我的消息响应的聊天消息发布后,我看到一个数据包:“ HTTP-Continuation或non-HTTP traffic”,并且在数据包数据中,我可以看到向客户端的传出消息,因此似乎服务器端工作正常.建立与服务器的初始连接,并按预期方式调用js onOpen处理程序.
我正在做的工作主要基于气氛示例聊天应用程序.如果有人有任何建议,我将不胜感激.可能还值得一提的是,我从大气示例中添加了实际的聊天处理程序,js和html页面,并且它也不起作用,并且也没有调用onMessage js处理程序,因此我认为它是一个配置问题.
web.xml
<servlet>
<description>AtmosphereServlet</description>
<servlet-name>AtmosphereServlet</servlet-name>
<servlet-class>org.atmosphere.cpr.AtmosphereServlet</servlet-class>
<init-param>
<param-name>o.a.useWebSocket</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>org.atmosphere.useNative</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<url-pattern>/chat/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AtmosphereServlet</servlet-name>
<url-pattern>/chatSample/*</url-pattern>
</servlet-mapping>
POM.xml
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-compat-tomcat</artifactId>
<version>1.0.15</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-compat-tomcat7</artifactId>
<version>1.0.15</version>
</dependency>
<dependency>
<groupId>org.atmosphere</groupId>
<artifactId>atmosphere-runtime</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
服务器端代码:
@AtmosphereHandlerService(path="/chat",
broadcasterCache = UUIDBroadcasterCache.class,
interceptors = { AtmosphereResourceLifecycleInterceptor.class,
BroadcastOnPostAtmosphereInterceptor.class,
HeartbeatInterceptor.class
})
public class ChatController extends OnMessage<String> {
private final ObjectMapper mapper = new ObjectMapper();
@Override
public void onMessage(AtmosphereResponse response, String message) throws IOException {
response.write(mapper.writeValueAsString(mapper.readValue(message, Data.class)));
}
}
客户端Javascript(已通过polling / long-polling / sse / websockets进行了尝试,并且所有初始都成功连接,并在初始连接后调用OnOpen处理程序:
var transport = 'long-polling';
var request = { url:'/Chat2/chat',
contentType : "application/json",
logLevel : 'debug',
transport : transport,
trackMessageLength : true,
reconnectInterval : 5000,
fallbackTransport: 'polling'};
request.onOpen = function(response) {
console.log('OnOpen: Atmosphere connected using ' + response.transport );
transport = response.transport;
};
request.onReopen = function(response) {
console.log('OnReopen: connection reopened');
};
request.onTransportFailure = function(errorMsg, request) {
atmosphere.util.info(errorMsg);
if (window.EventSource) {
request.fallbackTransport = "polling";
}
console.log('OnTransportFailure: Atmosphere Chat. Default transport is WebSocket, fallback is ' + request.fallbackTransport);
};
request.onMessage = function (response) {
alert('OnMessage: message received');
};
request.onClose = function(response) {
console.log('OnClose: Client closed the connection after a timeout');
subSocket.push(atmosphere.util.stringifyJSON({ author: author, message: 'disconnecting' }));
};
request.onError = function(response) {
console.log('OnError: error occurred');
console.log(response);
logged = false;
};
request.onReconnect = function(request, response) {
console.log('OnReconnect: Reconnected');
};
subSocket = socket.subscribe(request);
$('#chatSubmit').click(function() {
var msg = $('#chatText').val();
subSocket.push(atmosphere.util.stringifyJSON({ author: author, message: msg }));
$('#chatText').val('');
});
解决方法:
如果在客户端上设置trackMessageLength:true,则必须安装TrackMessageLengthInterceptor.因此,将其添加到AtmosphereHandlerService的拦截器列表中.
-让弗朗西斯(Jeanfrancois)