使用Jetty上的Jersey上的Atmosphere连接到webServer上的websocket时,升级协议异常无效

时间:2012-07-14 14:53:08

标签: websocket jetty jersey atmosphere

我正在使用Sonatype async-http-client java库在Jetty上的Jersey上使用Atmosphere对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的泽西资源方法:

@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();

例外的Stacktrace:

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。

post上有一个asyn-http-client google group,但是他们所指的代码示例是github上不再提供的可能的解决方案。 :(

1 个答案:

答案 0 :(得分:0)

通过在URL中设置http:// protocol而不是ws://来给出错误。

WRONG:

http://127.0.0.1:8080/JERSEY_RESOURCES

正确:

ws://127.0.0.1:8080/JERSEY_RESOURCES