2013-10-22 9 views
3

我正在嘗試使用氛圍試圖獲得簡單的基本實現2.0.3 tomcat 7.0.42在我的eclipse環境中本地運行(也從外部機器連接以查看流量與wireshark)。我遇到的問題是無論我使用什麼傳輸方式,websocket,sse,polling,long-polling,廣播響應似乎永遠都不會到達客戶端和響應.OnMessage處理程序從不被調用。在運行時我沒有收到任何異常,並且我嘗試過使用firefox/chrome /和IE。我也使用了wireshark,並在包含我的消息響應的聊天消息的帖子後看到一個數據包:「HTTP - 繼續或非HTTP流量」,並且在數據包數據中,我可以看到傳出消息給客戶端,所以它出現服務器端正在正常工作。初始連接到服務器已建立,並按預期調用js onOpen處理程序。大氣的反應,廣播不要調用javascript的onMessage處理程序

我正在做的工作很大程度上基於氣氛示例聊天應用程序。如果有人有任何建議,我將不勝感激。也許值得一提的是,我在大氣樣本中的實際聊天處理程序,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(一直試圖與輪詢/長輪詢/ 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(''); 
}); 

回答

3

如果在客戶端設置trackMessageLength:true,則必須安裝TrackMessageLengthInterceptor。所以把它添加到你的AtmosphereHandlerService的攔截器列表中。

- Jeanfrancois

+0

謝謝Jean,那的確是問題所在。添加攔截器是我需要做的。我現在能夠前進。 –

+1

從我的客戶端請求中移除trackMessageLength對我來說也有竅門! @see http://stackoverflow.com/questions/25598745/not-able-to-get-broadcast-on-websockethandleradapter –

相關問題