我正在努力尝试使用在我的eclipse环境中本地运行的大气2.0.3 tomcat 7.0.42来获得简单的基础实现(也可以从外部机器连接以查看与wireshark的流量)。我遇到的问题是无论我使用什么传输,websocket,sse,轮询,长轮询,广播响应似乎永远不会到达客户端,并且永远不会调用response.OnMessage处理程序。我在运行时没有收到异常,我尝试过使用firefox / chrome /和IE。我也使用了wireshark,我在包含我的消息响应的聊天消息的帖子后看到一个数据包:“HTTP - Continuation或非HTTP流量”,在数据包数据中我可以看到发送给客户端的消息,所以它出现服务器端正常工作。建立与服务器的初始连接,并按预期调用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('');
});
答案 0 :(得分:3)
如果在客户端设置trackMessageLength:true,则必须安装TrackMessageLengthInterceptor。所以将它添加到AtmosphereHandlerService的拦截器列表中。
- Jeanfrancois