我正在使用Sonatype async-http-client java 库在 Jetty 上使用Atmosphere on Jersey 对WebServer 进行 websocket 调用。
我得到了异常java.lang.IllegalStateException: Invalid upgrade protocol, status should be 101 but was 200
。错误是由WebSocketTextListener.onError(Throwable t)
方法启动的。
知道如何解决这个问题吗?
这是使用 Sonatype async-http-client 的客户端代码:
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());
}
}
这些是使用 Atmosphere 的 Jersey 资源方法:
@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);
}
这是用于启动我的 Atmosphere 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)
我正在使用 Atmosphere-jersey 0.9.7、Jetty 和 jetty-websocket 7.6RC4、sonatype/async-http-client 1.7.4、Jersey 1.12。
在asyn-http-client google group上有一篇文章,但他们提到的可能解决此问题的代码示例在 github 上不再可用。:(