TakahikoKawasaki / nv-websocket-client

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

The status code of the opening handshake response is not '101 Switching Protocols'. The status line is: HTTP/1.1 400 Bad Request #211

Closed wishy-viewlift closed 3 years ago

wishy-viewlift commented 3 years ago

I am facing issue with socket connection on Android device while the it seems to be working fine on web. Can you let me know what should I check over here?

mahditakrim commented 3 years ago

Hi wishy,

Make sure that the origin field of the websocket is defined like below your_websocket_object.addHeader("Origin", "your_origin_address");

james04gr commented 3 years ago

Hi wishy,

Make sure that the origin field of the websocket is defined like below your_websocket_object.addHeader("Origin", "your_origin_address");

What is the your_origin_address?? You mean the URL that the request is targetting to?

For example if the connectionUrl is: "wss://my.app.domain/myapp?c=823h" i must do mWebSocketObj.addHeader("my.app.domain") ??

james04gr commented 3 years ago

I am facing issue with socket connection on Android device while the it seems to be working fine on web. Can you let me know what should I check over here?

Why did you close this?

wishy-viewlift commented 3 years ago

@james04gr sorry for not commenting earlier The issue was from backend due to which I was facing it. Hence this can be closed.

james04gr commented 3 years ago

Can you please share some info since i am facing the same and i dont really know what to do? Also, i made a question above. Do you know what origin_address means according to my example?

wishy-viewlift commented 3 years ago

You can simply use the following method WebSocket ws = new WebSocketFactory().createSocket("ws://localhost/endpoint"); Replace with your url Headers are same as you have in GET / POST request. If your socket demands it then send otherwise not.

You can also try with OKHttp Try these links https://trinitytuts.com/connect-to-websocket-on-android-using-okhttp/ http://www.websocket.org/echo.html

Let me know if this works out

james04gr commented 3 years ago

` private fun getWebSocketFactoryInstance(): WebSocketFactory { if (webSocketFactory != null) return webSocketFactory!! return WebSocketFactory().setConnectionTimeout(10000) }

private fun connectWebSocket(): WebSocket {
    disconnect()
    webSocket = getWebSocketFactoryInstance().createSocket(login.connectUrl()).also {
         // Should i add something like this??
         // it?.addHeader("Origin", BuildConfig.WEB_SOCKET_URL)
    }
    return webSocket!!
}`

login.connectUrl() is the final connectionUrl

The problem is that sometimes it connects, sometimes i get the OpeningHandshakeException mentioned above. Is it sure a backend problem or a client problem, because in the same backend URL (which is an Amazon Web socket url) other devices like iOS and Windows, connect always successfully in that same URL

wishy-viewlift commented 3 years ago

@james04gr It surely seems to be a problem from backend coz I even I faced the same. Try to open socket with this url and send some string - you will receive the same string in response ws://echo.websocket.org If this works I am pretty sure your backend needs to look.

Try with OKHttp

` OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(60, TimeUnit.SECONDS) .readTimeout(60, TimeUnit.SECONDS) .writeTimeout(60, TimeUnit.SECONDS) .build(); Request request = new Request.Builder().url("ws://echo.websocket.org").build(); client.newWebSocket(request, new WebSocketListener() { @Override public void onMessage(WebSocket webSocket, String text) { super.onMessage(webSocket, text); Log.i("Socket response",text); }

        @Override
        public void onMessage(WebSocket webSocket, ByteString bytes) {
            super.onMessage(webSocket, bytes);
        }

        @Override
        public void onOpen(WebSocket webSocket, okhttp3.Response response) {
            super.onOpen(webSocket, response);
            webSocket.send("Socket open and working");
        }
    });
    client.dispatcher().executorService().shutdown();`
james04gr commented 3 years ago

Well, i just found that it was indeed a backend problem. Thank you for your time and help! Appreciate it!

wishy-viewlift commented 3 years ago

@james04gr glad to help. Closing this now

ndvbd commented 3 years ago

@wishy-viewlift @james04gr I see this issue and I think this is a backend problem - can you tell us what was your backend problem so we can have a general direction? Are you using CloudFlare?

ndvbd commented 1 year ago

I have the same issue, and nothing gets to the server.