2015-10-07 24 views
1

我試圖設置一些使用Dropwizard和Atmosphere的websocket啓用資源。我已經根據我對這裏的代碼設置:https://cvwjensen.wordpress.com/2014/08/02/websockets-in-dropwizard/接受:應用程序/ JSON失敗與使用澤西Dropdrop的Atmosphere

我用下面的代碼來設置大氣中Dropwizard:

AtmosphereServlet atmosphere = new AtmosphereServlet(); 
atmosphere.framework().addInitParameter(ApplicationConfig.ANNOTATION_PACKAGE, "my.classes"); 
atmosphere.framework().addInitParameter(ApplicationConfig.WEBSOCKET_SUPPORT, "true"); 
atmosphere.framework().addInitParameter(ApplicationConfig.PROPERTY_NATIVE_COMETSUPPORT, "true"); 
atmosphere.framework().addInitParameter(ApplicationConfig.PROPERTY_COMET_SUPPORT, "org.atmosphere.container.Jetty9AsyncSupportWithWebSocket"); 
ServletRegistration.Dynamic servlet = environment.servlets().addServlet("atmosphere", atmosphere); 
servlet.addMapping("/APIWS/*"); 

,我用它來測試它看起來像這樣的資源:

@GET 
@Produces({"application/xml", "application/json"}) 
public SuspendResponse<JAXBElement<CustomerListType>> getUpdates(@Context BroadcasterFactory bf) { 
    Broadcaster bc = getBroadcaster(bf, hash); 
    registerBroadcaster(hash, query, apiContext.getUser()); 
    return new SuspendResponse.SuspendResponseBuilder<JAXBElement<CustomerListType>>() 
      .broadcaster(bc) 
      .build(); 
} 

它主要工作,但如果我在我的Websocket請求中設置標頭Accept: application/json,它會在服務器嘗試推送某些內容時失敗。設置Accept: application/xml工作正常。

我測試使用下面的curl命令它:

curl -i -N -H "Accept: application/json" -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-Websocket-Version: 13" -H "Sec-WebSocket-Key: 258E" http://localhost:8080/APIWS/customers 

只要服務器推送一些數據時,出現以下消息:

* STATE: PERFORM => DONE handle 0x80048248; line 1617 (connection #0) 
* Curl_done 
* Empty reply from server 
* Connection #0 to host localhost left intact 
curl: (52) Empty reply from server 

回答

1
+0

感謝您的回答Jeanfrancois!你的意思是將'org.atmosphere.websocket.messageContentType'初始化參數設置爲'application/json'嗎?我嘗試了一些設置,但不幸的是它沒有任何區別。 – Petter

+2

將日誌轉到TRACE以查看澤西島內發生了什麼。我懷疑澤西球頭有一些問題,因爲大氣沒有使用它。 – jfarcand

+0

謝謝!我將所有涉及的模塊的日誌記錄設置爲TRACE,結果發現其中一個提供者實際上存在錯誤。不知道爲什麼這個例外只出現在TRACE日誌中,但我很高興它現在正在工作! :) 謝謝您的幫助! – Petter