我正在尝试使用Dropwizard和Atmosphere设置一些支持websocket的资源。我的设置基于以下代码:https://cvwjensen.wordpress.com/2014/08/02/websockets-in-dropwizard/
我使用以下代码在Dropwizard中设置Atmosphere:
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
答案 0 :(得分:1)
Petter,试试这个
https://github.com/Atmosphere/atmosphere/wiki/WebSocket-default-protocol-configuration
- Jeanfrancois