我正在使用 atmosphere 尝试在我的 Eclipse 环境中本地运行的 atmosphere 2.0.3 tomcat 7.0.42 获得简单的基本实现(也从外部机器连接以查看与 Wireshark 的流量)。 我遇到的问题是,无论我使用什么传输,websocket,sse,轮询,长轮询,广播响应似乎永远不会到达客户端和响应。永远不会调用 OnMessage 处理程序。 我在运行时没有收到任何异常,并且我已经尝试使用Firefox/chrome/和IE。 我还使用过wireshark,在发布包含我的消息响应的聊天消息后,我会看到一个数据包:"HTTP - 延续或非HTTP流量",在数据包数据中,我可以看到传出的消息到客户端,所以看起来服务器端工作正常。 建立与服务器的初始连接,并按预期调用 js onOpen 处理程序。
我正在做的工作主要基于氛围示例聊天应用程序。 如果有人有任何建议,我将不胜感激。 可能还值得一提的是,我从大气示例中添加了实际的聊天处理程序、js 和 html 页面,它也没有行为,也没有在其中调用 onMessage js 处理程序,所以我认为这是一个配置问题。
网络.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>
聚甲醛.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的拦截器列表中。