TakahikoKawasaki / nv-websocket-client

High-quality WebSocket client implementation in Java.
Apache License 2.0
2.03k stars 292 forks source link

Implement '101 Switching Protocols' ? #24

Closed amallo closed 8 years ago

amallo commented 8 years ago

Hi your lib looks amazing ! Do you plan to implement '101 Switching Protocols' ?

TakahikoKawasaki commented 8 years ago

I'm sorry I cannot understand your question. What do you mean by "implement '101 Switching Protocols'" ?

amallo commented 8 years ago

Hi sorry my question was not clear.

when trying to connect to my server I have this error "The status code of the opening handshake response is not '101 Switching Protocols".

On your home page it is said : "HTTP response codes other than "101 Switching Protocols" from a WebSocket endpoint are not supported",

will you support other response code in the future ? My previous library could connect properly so I guess that this 'error' depends on the way you handle connection.

TakahikoKawasaki commented 8 years ago

RFC 6455 says "Any status code other than 101 indicates that the WebSocket handshake has not completed". In other words, other HTTP status codes than 101 indicate that the server rejected to talk WebSocket protocol.

Supporting other HTTP status codes than 101 is not a task for a WebSocket client library but a task for a generic HTTP client library. nv-websocket-client is not aiming at becoming a generic HTTP client library.

What behaviors do you expect when an HTTP status code other than 101 is returned from a WebSocket server?

smilecs commented 8 years ago

though curently I have just tried my app thats using the library and its not connecting to my webserver "The status code of the opening handshake response is not '101 Switching Protocols'" I dont know if the issue is from client or server

TakahikoKawasaki commented 8 years ago

In most cases, a client application is to blame for other HTTP status codes than 101. For example, 404 (Not Found) is returned when a client application specified a wrong URL. A server is to blame only when a 5xx status code is returned.

For other status codes than 101, "the semantics of HTTP still apply" (RFC 6455, 1.3. Opening Handshake). If you receive a status code other than 101, it's not a WebSocket problem but a normal HTTP problem.

amallo commented 8 years ago

Thanks for your reply !

Problem is : my previous socket library connects successfully to my SSL websocket server but yours can't connect to the server, am I missing something ?

Here is my code :

String host = "wss://myserver.com"; mWebSocketFactory = new WebSocketFactory(); websocket = mWebSocketFactory.createSocket(host, 5000); websocket.setPingInterval(30 * 1000); websocket.setAutoFlush(true); websocket.addListener(...); websocket.connectAsynchronously();

amallo commented 8 years ago

I found my error !

I missed the url port now it works. Strange because there were no need my with previous library, sounds like a redirection problem.

Thanks for your time ! I will test your library now !

TakahikoKawasaki commented 8 years ago

Sorry, redirection (3xx) is not supported although I think it would be better to support it.

TakahikoKawasaki commented 8 years ago

Released a new version, 1.19.

Since the new version, connect() method throws OpeningHandshakeException (a subclass of WebSocketException) when a violation against the WebSocket protocol is detected during an opening handshake. For example, when a status code other than 101 is returned from a server.

OpeningHandshakeException provides additional methods such as getStatusLine(), getHeaders() and getBody() to access the response from a server.

You can retrieve the redirection URL like the following.

try
{
    // Perform the WebSocket opening handshake.
    ws.connect();
}
catch (OpeningHandshakeException e)
{
    // Get the status code.
    int statusCode = e.getStatusLine().getStatusCode();

    // If the status code is in the range of 300 to 399.
    if (300 <= statusCode && statusCode <= 399)
    {
        // Location header should hold the redirection URL.
        String location = e.getHeaders().get("Location").get(0);
    }
}
catch (WebSocketException e)
{
}
AbdulDroid commented 7 years ago

What of the 200 OK code how does one fix that, cos my URL is correct and the server person is saying they implemented a websocket, what can I do?!?

mishaxz commented 6 years ago

hello, I'm really stuck. I have a strange problem. I get a (not 101) moved permanently 301 error when not using my VPN - but using the VPN I don't have these problems. I use OkHTTP but the only discussions related to this error I could find was the neovisionaries github site.

Could anyone tell me what might be going on? What I should google? would using an actual IP addresses instead of text url help?