本文介绍了气氛响应,广播不调用javascript onMessage处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力尝试使用在我的eclipse环境中本地运行的大气2.0.3 tomcat 7.0.42获得简单的基本实现(也可以从外部机器连接到wireshark查看流量)。我遇到的问题是无论我使用什么传输,websocket,sse,轮询,长轮询,广播响应似乎永远不会到达客户端,并且永远不会调用response.OnMessage处理程序。我在运行时没有收到异常,我尝试过使用firefox / chrome /和IE。我也使用了wireshark,我看到一个包含我的消息响应的聊天消息后的数据包:HTTP - 延续或非HTTP流量,在数据包数据中,我可以看到发送给客户端的消息,所以它似乎服务器端正常工作。建立与服务器的初始连接,并按预期调用js onOpen处理程序。

I'm working with atmosphere trying to get the simple base implementation using atmosphere 2.0.3 tomcat 7.0.42 running locally in my eclipse environment (also connecting from external machine to see traffic with wireshark). The problem I am experiencing is no matter what transport I use, websocket, sse, polling, long-polling, the broadcast response never seems to get to the client and the response.OnMessage handler is never invoked. I receive no exceptions during runtime, and I have tried with firefox/chrome/and IE. I have also used wireshark and I see a packet after the post of a chat message that contains my message response: "HTTP - Continuation or non-HTTP traffic" and in the packet data I can see the outgoing message to the client, so it appears the server side is working correctly. The initial connection to server is established and the js onOpen handler is invoked as expected.

我正在做的工作主要基于大气样本聊天应用程序。如果有人有任何建议,我将不胜感激。可能还值得一提的是,我在大气样本中添加了实际的聊天处理程序,js和html页面,它也没有表现,并且onMessage js处理程序也没有被调用,所以我认为它是一个配置问题。

The work I am doing is based largely on the atmosphere sample chat application. If anyone has any suggestions, I would appreciate it. Might also be worth mentioning, that I added in the actual chat handler, js, and html page from the atmosphere sample and it also does not behave and the onMessage js handler is not invoked in that either, so I'm thinking it is a configuration issue.

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 / sse / websockets并且最初都成功连接并在初始连接后调用OnOpen处理程序:

Client side Javascript (has been tried with polling/long-polling/sse/websockets and all successfully connect initially and call the OnOpen handler after initial connection:

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('');
});


推荐答案

你必须安装。所以将它添加到AtmosphereHandlerService的拦截器列表中。

you must install the TrackMessageLengthInterceptor if you set the trackMessageLength : true on the client side. So add it to your AtmosphereHandlerService's interceptor list.

- Jeanfrancois

-- Jeanfrancois

这篇关于气氛响应,广播不调用javascript onMessage处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:46