Repository navigation
Unable to connect using https #26
Description
/I am using node.js server side and android and another node.js client side web application.
The wep application is able to connect securely while the android application is just giving an timeout error./
//The code for creating sslcontext
Log.d(TAG,"setupContext");
//Using a client certificate
String password = "password"
KeyStore keyStore = KeyStore.getInstance("PKCS12");
InputStream is = context.getResources().openRawResource(R.raw.client_crt);
keyStore.load(is, password.toCharArray());
is.close();
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
kmf.init(keyStore, password.toCharArray());
KeyManager[] keyManagers = kmf.getKeyManagers();
// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// Using self signed certificate
is = context.getResources().openRawResource(R.raw.cacert);
InputStream caInput = new BufferedInputStream(is);
Certificate ca;
ca = cf.generateCertificate(caInput);
Log.i(TAG,"ca=" + ((X509Certificate) ca).getSubjectDN());
caInput.close();
// Create a KeyStore containing our trusted CAs
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null);
trustStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
tmf.init(trustStore);
TrustManager[] trustManagers = tmf.getTrustManagers();
// Create an SSLContext that uses our TrustManager
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(keyManagers, trustManagers, null);
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = false;
opts.sslContext = mSslContext;
opts.secure=true;
mSocket = IO.socket("https://" + mIpAddress + ":" + mPort+"/", opts);
mSocket.connect();
mSocket.on(Socket.EVENT_CONNECT, mConnectionListener);
mSocket.on(Socket.EVENT_DISCONNECT,mDisconnectionListener);
mSocket.on(Socket.EVENT_ERROR,mErrorListener);
mSocket.on(Socket.EVENT_MESSAGE,mMessageListener);
mSocket.on(Socket.EVENT_CONNECT_ERROR,mConnectionErrorListener);
// I never get call for EVENT_CONNECT
///////////////////////////SERVER CODE///////////////////////////////////////////////
var privateKey = fs.readFileSync('certificates/server.key').toString();
var certificate = fs.readFileSync('certificates/server.crt').toString();
var ca = fs.readFileSync('certificates/cacert.pem').toString();
// Sending and receiving events with Socket.IO
var app = express();
https.globalAgent.options.rejectUnauthorized = false;
var server = https.createServer({key:privateKey,cert:certificate,ca:ca },app);
// var server = require('http').Server(app);
exports.io = require('./sockets').listen(server);
//Server is listening on port 3000
Please tell a way to enable logs in android app for socket.io and possible cause of this problem.
There is no activity in node.js terminal as if the socket never got a call after using https it work fine if I use http
Thanks and regards,
Anshul
Activity
nkzawa commentedon Aug 4, 2014
Umm, I'm not sure what the problem is, but it may be helpful to compare with my settings for unit tests.
https://github.com/nkzawa/socket.io-client.java/blob/master/src/test/java/com/github/nkzawa/socketio/client/SSLConnectionTest.java#L51
Set the log level
FINEor lower to see all logs.mifoss commentedon Oct 21, 2014
i have the same Problem. I have created a self-sigend certifcate but socketio won't connect to my server...
When i listen to the "EVENT_CONNECT_ERROR" Event i get the following ErrorMessage:
"com.github.nkzawa.engineio.client.EngineIOException: xhr poll error"
------------- ServerCode ( Snippet ) -------------
Java-Client:
If i use this code for http-Requets everything works fine
kosch commentedon Nov 11, 2014
I think the problem is maybe related to host name verification. Try to set the
Unfortunality such exceptions are not logged. I suggest to add logging at Request.onError including the exception stack.
Maybe you can add the HostNameVerified as additional Option too.
feromakovi commentedon Nov 26, 2014
Hello, I'm using this library too for connecting to server. Connection to server is secured too.
But in my case I creates SSLContext which should accept all certificates like this:
and in code I set it up:
and I create custom IO.Options:
and for establishing connection I use this code, maybe very similar as the code from samples:
And this instantly gives me an Exception, the same as in previous cases:
And if I create my own server app, unsecured with scheme - http, on my private network, everything works. Is it problem on client side (me) or server side?
Thank you for your advices!
crossle commentedon Dec 10, 2014
How about https connect?
jonathanve commentedon Dec 10, 2014
Hi @feromakovi and @crossle I was able to communicate from my android phone via ssl. Here it is the project I setup for you guys to try it out.
https://github.com/jonathanve/socket-io-android
Hope it helps.
jonathanve commentedon Dec 10, 2014
I wrote the code inside my application class. Anywhere else where should I locate it instead?
jonathanve commentedon Dec 15, 2014
@nkzawa ?
nkzawa commentedon Dec 16, 2014
@jonathanve what's your problem?
nkzawa commentedon Dec 16, 2014
@jonathanve btw, thank you for your great example app 👍
jonathanve commentedon Dec 16, 2014
hi @nkzawa , thank you. Hope the example helps somebody so this issue can be closed
madhumita1 commentedon Dec 30, 2014
@nkzawa ,@jonathanve
how to handle for "https" connections?
j54n1n commentedon Jan 14, 2015
Hello,
I'm also trying to use https connection on my Android client but I get either the dreaded xhr poll error on JellyBean+ or a strange semi working behaviour on Gingerbread or IceCreamSandwich.
I've running on a Raspberry Pi a simple node.js app that runs Express to serve a webpage embedded with the client side socket.io script.
and with the https server app
I'm generating the keys and ca with a script via openssl
then I copied the .crt file to my Android Studio project and tried to load the self signed certificate as mentioned in the docs of Android.
https://developer.android.com/training/articles/security-ssl.html#UnknownCa
First thing was to add
in the manifest file.
To be sure that the certificate part was working I added the code to open a https connection and I printed out in the log the content of the html file. That worked fine without problems. But in the part of your socket.io library I got only "com.github.nkzawa.engineio.client.EngineIOException: xhr poll error" at least from JellyBean till Lollipop.
In another way Gingebread and ICS had a different behaviour: the socket.io library connected to the server without problems but only if I left the code inplace that was reading from the https input stream.
My wild guess would be that the javascript inside from the html file gets executed in some way and then kicks in the java socket.io library!?I'm attaching the logs that I made:
From JellyBean till Lollipop
For Gingerbread and ICS
No clue if I missed something else!?
j54n1n commentedon Jan 14, 2015
Ok that was fast xD.
In the end it was something that I missed in the code.
The line after
IO.setDefaultSSLContext()you have to putor provide your own implementation of the HostnameVerifier interface. Pay attention that this is the global setting via the static method
HttpsURLConnection#setDefaultHostnameVerifier. Maybe it should be mentioned in the readme?hgshoggins commentedon Jan 25, 2015
Hello,
On my side, everything is good until Android Lollipop : stumbled accross a "timeout" SocketIOException, and I can't figure out why.
Any ideas ?
wingoku commentedon Apr 28, 2015
@hgshoggins Did you find any solutions for TimeOut issue on Lollipop devices? My code is working fine on pre lollipop devices but on lollipop it is giving me timeout issue.
hgshoggins commentedon Apr 29, 2015
Hello @wingoku. No luck on my side, I had to use plain text on my ws, and make sure I was not transporting any sensitive data.
I first wanted to use it because in plain HTTP, some GSM BTS mangle the content sent by a Device : I got myself with some strange X-NOKIA-xxxx headers that I did not want to be added, that is why I preferred to use HTTPS.
nkzawa commentedon May 2, 2015
Can you try just released v0.5.0 which the websocket transport is replaced with OkHttp ?
najeebullahshah commentedon Jun 5, 2015
@j54n1n great man, your's is the best solution on the issue of https connection.
shuoli84 commentedon Jun 24, 2015
I am using this with HTTPS and it requires no hacking or workaround. Using v0.5.1.
@anshulvij @nkzawa is it ok to close this?
therockorchid commentedon Aug 13, 2015
@shuoli84 ,
can you share your connection code? Perhaps that would help me figure out what I am doing wrong? Coz I am having the same issue as mentioned above by @j54n1n
com.github.nkzawa.engineio.client.EngineIOException: xhr poll error
I am using v0.5.2
marekyggdrasil commentedon Oct 19, 2015
I was also having xhr poll error, what resolved the issue was:
@jonathanve code and also updating socket.io library from v0.3.0 to v0.6.2. I did not establish SSL security within node.js like @j54n1n suggested, instead I defined them in my nginx server configuration.
wpq2014 commentedon Jun 11, 2016
@jonathanve Did you deleted https://github.com/jonathanve/socket-io-android?
jonathanve commentedon Jun 11, 2016
@wpq2014 yes, let me restore it then. Since no comments were posted since October 2015, I thought It was not longer needed.
dominguesr commentedon Sep 23, 2016
@marekyggdrasil could you please explain how you did it on Nginx? We have a similar situation and we haven't been able to figure out how to fix it. thanks
HeartIsBeat commentedon Jul 27, 2017
I cause the problem, just use code

When "io.socket.engineio.client.EngineIOException: xhr poll error" is appeared,for I use emit method's second param is String not JSONObject.
Connect to socket.io server use java api caused me one day! O my god.
darrachequesne commentedon Apr 25, 2021
Closed due to inactivity, please reopen if needed.
Oytunkdnz commentedon Aug 31, 2022
hello!
I get this strange error:
E/Socket: Socket connection can not be established
my SocketHelper.java is:
private SocketHelper() {
try {
and error comes from VehicleBidFragmentPresenterImp.java is:
public void initSocketHelper(String userId, int auctionId) {
SocketHelper.getInstance().connect(new SocketHelper.SocketCallBack() {
@OverRide
public void onConnect(Object... args) {
// send first bid for wake up socket
sendBid(userId, 4, "1", String.valueOf(auctionId), Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis());
}
please help me to solve this problem.