Closed wishy-viewlift closed 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");
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") ??
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?
@james04gr sorry for not commenting earlier The issue was from backend due to which I was facing it. Hence this can be closed.
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?
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
` 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
@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();`
Well, i just found that it was indeed a backend problem. Thank you for your time and help! Appreciate it!
@james04gr glad to help. Closing this now
@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?
I have the same issue, and nothing gets to the server.
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?