Websockets in Jelastic: DeploymentException: The HTTP response from the server did not permit the HTTP upgrade to WebSocket

最后都变了- 提交于 2021-01-28 12:09:58

问题


I'm trying to play with websockets, so I created WS server like that:

@ServerEndpoint(value = "/chat")
public class WsChatServlet{
private static final Logger LOGGER =
        Logger.getLogger(WsChatServlet.class.getName());

@OnOpen
public void onOpen(Session session) {
    LOGGER.log(Level.INFO, "New connection with client: {0}",
            session.getId());
}

@OnMessage
public String onMessage(String message, Session session) {
    LOGGER.log(Level.INFO, "New message from Client [{0}]: {1}",
            new Object[] {session.getId(), message});
    return "Server received [" + message + "]";
}

@OnClose
public void onClose(Session session) {
    LOGGER.log(Level.INFO, "Close connection for client: {0}",
            session.getId());
}

@OnError
public void onError(Throwable exception, Session session) {
    LOGGER.log(Level.INFO, "Error for client: {0}", session.getId());
}
}

and deployed it in Tomcat 7 running on Java 7. Then I created that client:

@ClientEndpoint
public class Main {
private static final String uri = "ws://myserver.net/chat";

public static void main(String[] args) throws DeploymentException, IOException, URISyntaxException, InterruptedException {
    WebSocketContainer wsContainer =  ContainerProvider.getWebSocketContainer();
    Session session = wsContainer.connectToServer(Main.class, new URI(uri));
    session.getBasicRemote().sendText("Here is a message!");
    Thread.sleep(1000);
    session.close();
}

@OnMessage public void processMessage(String message){
    System.out.println(message);
}
}

This client runs as a comand-line app. When I run it, I get exception:

Exception in thread "main" javax.websocket.DeploymentException: The HTTP response from the server [HTTP/1.1 302 Found] did not permit the HTTP upgrade to WebSocket

So it looks like server doesn't responds propertly to my client. I noticed that if I access ws://myserver.net/chat, than it responds with 302, but for ws://myserver.net/chat/ (notice /) it responds with 200 OK.

More interesting is this Tomcat logs:

    • [01/Oct/2014:15:52:35 +0000] "GET /chat/ HTTP/1.0" 200 57
    • [01/Oct/2014:15:52:42 +0000] "GET /chat HTTP/1.0" 302 -

As you may see my client connects with HTTP 1.0, not 1.1, which may be wrong. I am completely stucked with it. Don't understand why it doesn't work.

EDIT: So, my main question is why websockets doesn't work (I guess HTTP Upgrade response header missed) and how to fix that.

EDIT2: I forgot most important thing: I deploy it in free Jelastic environment.


回答1:


So finally I found what went wrong and the answer is: Jelastic. I deployed my app to the free Jelastic instance, that means all requests were proxyed by nginx. As mentioned here, you must obtain public IP to let your app work with WebSockets in Jelastic.




回答2:


as stated by wikipedia:

The HTTP response status code 302 Found is a common way of performing URL redirection

and that 200 OK is the

Standard response for successful HTTP requests

So based on you situation, I guess you explorer is saving the first request with a trailing slash, in either case, the http status is Found or OK, the resource is found.

finally I guess changing the value of sessionCookiePathUsesTrailingSlash to false would solve the problem and the response will be always 200 OK. (for changing it check this)

if this didn't work, many reasons can push tomcat to add trailing slash, check this discussion for other reasons



来源:https://stackoverflow.com/questions/26145662/websockets-in-jelastic-deploymentexception-the-http-response-from-the-server-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!