我正在使用Sonatype async-http-client java庫在Jetty上使用澤西島上的Atmosphere進行websocket調用。連接到WebServer上的websocket時出現無效的升級協議異常,並在Jetty上的Jersey上使用Atmosphere
我收到異常java.lang.IllegalStateException: Invalid upgrade protocol, status should be 101 but was 200
。該錯誤由WebSocketTextListener.onError(Throwable t)
方法啓動。
任何想法如何解決這個問題?
這是使用Sonatype的異步HTTP客戶端,客戶端代碼:
public void getMealWebSocketSubscriber(String restaurantId , String mealId, OnGetMealListener listener) throws ApiException {
try {
WebSocket websocket = client.prepareGet(getAbsolutePath("restaurants/" + restaurantId + "/meals/" + mealId + "/websocket/"))
.execute(new WebSocketUpgradeHandler.Builder().build())
.get();
websocket.addWebSocketListener(
new WebSocketTextListener() {
public void onOpen(WebSocket websocket) {
System.out.println("WebSocket onOpen");
}
public void onClose(WebSocket websocket) {
System.out.println("WebSocket onClose");
}
public void onError(Throwable t) {
System.out.println("WebSocket onError");
t.printStackTrace();
}
public void onMessage(String message) {
System.out.println("WebSocket onMessage: " + message);
}
public void onFragment(String fragment, boolean last) {
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
throw new ApiException(e.getMessage());
}
}
這些都是使用澤西資源的方法氛圍:
@Path("/websocket")
@Suspend
@GET
@Produces({MediaType.APPLICATION_JSON})
public String suspend() {
return "";
}
@Path("/websocket")
@Broadcast(writeEntity = false)
@POST
@Produces({MediaType.APPLICATION_JSON})
public Meal broadcast() {
return getMealById(mealId);
}
這是用來啓動我大氣的代碼servlet:
this.webServer = new Server(8080);
AtmosphereServlet atmosphereServlet = new AtmosphereServlet();
ServletHolder servletHolder = new ServletHolder(atmosphereServlet);
//Atmosphere
servletHolder.setInitParameter("org.atmosphere.websocket.messageContentType" , "application/json");
servletHolder.setInitParameter("org.atmosphere.useWebSocket","true");
servletHolder.setAsyncSupported(true);
//Jersey resources
// servletHolder.setInitParameter("com.sun.jersey.config.property.packages", "com.famenu.server.resources");
servletHolder.setInitParameter("com.sun.jersey.config.property.resourceConfigClass" , "com.sun.jersey.api.core.ClassNamesResourceConfig");
servletHolder.setInitParameter("com.sun.jersey.config.property.classnames" ,
"com.famenu.server.resources.ItemResource, " +
"com.famenu.server.resources.MealAtmosphereResource, " +
"com.famenu.server.resources.MealResource, " +
"com.famenu.server.resources.MealsResource, " + MY_OTHER_JERSEY_RESOURCES);
servletHolder.setInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters" , "com.sun.jersey.api.container.filter.LoggingFilter");
servletHolder.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters" , "com.sun.jersey.api.container.filter.LoggingFilter");
servletHolder.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature" , "true");
ServletContextHandler servletContextHandler = new ServletContextHandler(webServer, "/rest", true, false);
servletContextHandler.addServlet(servletHolder, "/");
webServer.setHandler(servletContextHandler);
webServer.start();
堆棧跟蹤異常:
java.lang.IllegalStateException: Invalid upgrade protocol, status should be 101 but was 200
at com.famenu.shared.api.AsyncApi.getMealWebSocketSubscriber(AsyncApi.java:172)
at com.famenu.shared.api.AsyncApiTest.testGetMealWebSocketSubscriber(AsyncApiTest.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
我使用大氣 - 球衣0.9.7,碼頭&碼頭-的WebSocket 7.6RC4,Sonatype的/異步HTTP客戶端1.7.4,新澤西州1.12。
asyn-http-client google group上有post,但是他們提到的代碼示例是對git的一個可能的解決方案。 :(