In my case, android application is behind the proxy server and it needs to connect to the socket.io server. How can I handle this case? Does this library automatically handles this case? I was not able to know this from the example code given. Kindly, let me know if it is possible for android behind proxy to connect to socket.io server using this library. If not, just let me know what should be changed in the code to do so. Thanks
Hi, I'm not familiar with that situation, but I think you don't need anything special.
Socket.IO and this library treat only HTTP and WebSocket, so it's ok if the proxy server could handle them.
Proxy server just blocks the socket connection when I used another java websocket library. This is why I came here looking for any other library. I am always behind a proxy server. Let me use this library and I will let you know if this library works behind the proxy server or not.
I use Charles Proxy (using the proxy settings in the device's System > Wi-Fi settings with this library. Though I can't actually see the data being transferred in Charles, the library still functions as it should.
It is because library doesn't know that there is a proxy and it should be sending or receiving the data through that proxy. Just like in Windows Firefox, we need to tell the software that it should "use system proxy", similarly, any application which connects to Internet should be able to know whether there is a proxy set on device or not. If there is a proxy set on the device application should also use that proxy. Other socket libraries I have used doesn't have this feature. They don't care whether android device is behind proxy or not. I haven't tested this library yet. In couple of days, I will switch to this library in my project.
Why was this issue closed? I cant connect to my socket.io server if i am behind a proxy that requires a username and password. If i use the browser, the browsers shows a popup where i put in the username and password and i can connect to the chat web front-end. How i setup the client so that i can pass the username and password or so that it uses the system's proxy settings. The system proxy settings says they are for the bowser and some apps might not use them.
@josephkandi Did you get it working ?
@akprats33 Nope i did not
Just in case anyone is still stuck with proxy, I got it working with the following configuration -
public connect() {
String httpsProxyHost = getSystemProperty("https.proxyHost");
this.protocol = StringUtils.isNotEmpty(httpsProxyHost) ? Protocol.HTTPS : Protocol.HTTP;
OkHttpClient okHttpClient = this.getHttpClientBuilder()
.build();
IO.setDefaultOkHttpCallFactory(okHttpClient);
IO.setDefaultOkHttpWebSocketFactory(okHttpClient);
try {
socket = IO.socket(<socket url>);
socket.connect();
} catch (URISyntaxException e) {
throw new RuntimeException("Error in initializing Socket.", e);
}
}
/**
* Generate client builder with proxy settings applied
*
* @return
*/
private OkHttpClient.Builder getHttpClientBuilder() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.pingInterval(10000, TimeUnit.MILLISECONDS);
String proxyHostProperty = getProxyHostProperty();
String proxyPortProperty = getProxyPortProperty();
if (StringUtils.isEmpty(proxyHostProperty) ||
StringUtils.isEmpty(proxyPortProperty)) return clientBuilder;
int proxyPort = Integer.parseInt(proxyPortProperty);
Proxy networkProxy =
new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHostProperty, proxyPort));
// set proxy
clientBuilder.proxy(networkProxy);
String proxyUser = getProxyUsernameProperty();
String proxyPassword = getProxyPasswordProperty();
// no authentication at all
if (StringUtils.isEmpty(proxyUser)) return clientBuilder;
Authenticator proxyAuthenticator = new Authenticator() {
@Nullable
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic(proxyUser, proxyPassword);
Request authRequest = response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
ResponseBody responseBody = response.body();
if (responseBody != null) responseBody.close();
return authRequest;
}
};
clientBuilder.proxyAuthenticator(proxyAuthenticator);
return clientBuilder;
}
@ akprats33 No, no lo hice
Lograste hacerlo funcionar?
This does not work for me.
Most helpful comment
Why was this issue closed? I cant connect to my socket.io server if i am behind a proxy that requires a username and password. If i use the browser, the browsers shows a popup where i put in the username and password and i can connect to the chat web front-end. How i setup the client so that i can pass the username and password or so that it uses the system's proxy settings. The system proxy settings says they are for the bowser and some apps might not use them.