Skip to content
This repository has been archived by the owner on Mar 6, 2018. It is now read-only.

android client over https #106

Open
pratikkaneria opened this issue Aug 8, 2014 · 5 comments
Open

android client over https #106

pratikkaneria opened this issue Aug 8, 2014 · 5 comments

Comments

@pratikkaneria
Copy link

have been playing with your contribution and i found an issue while connecting android socketio client to node server (over https) .
When i am trying to connect android client no response is coming from server (no error, no exception), but same thing is working with simple java application.

Can you please find out why its not working with android.

@Laetitia28
Copy link

hi, you sure you havn't error of handshaking ? . Because I have the same issue with Https ( works with http ).
I post my code (works) , I hope it will help you.
1 - first generate .BKS file with your server
2 - Paste this code ( don't forget to change var password in getFactory() method.

public class Socket implements IOCallback{

private SocketIO socket;
private static Context context;
private String url;

public Socket(String url) throws MalformedURLException, NoSuchAlgorithmException {

    this.url=url;

    try {
        SocketIO.setDefaultSSLSocketFactory(getFactory());
    } catch (Exception e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }

    //Creation du socket en fonction de l'adresse de la machine
    this.socket = new SocketIO();

    //Creation des fonctions de callbacks
    Log.d("io.socket", url.toString());
    socket.connect(url, this);
    try {
        SocketIO.setDefaultSSLSocketFactory(getFactory());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }     
}

public static SSLContext getFactory() throws Exception {
    KeyStore trusted = KeyStore.getInstance("BKS");

    InputStream in = context.getResources().openRawResource(R.raw.infotel);

          try {
              // Initialisation de notre keystore. On entre le mot de passe (storepass)
              trusted.load(in, "mypassword".toCharArray());
          } finally {
              in.close();
          }


    TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
    tmf.init(trusted);

    SSLContext ssl_context = SSLContext.getInstance("SSL");
    ssl_context.init(null, tmf.getTrustManagers(), null);

    return ssl_context;
}

@Override
public void onMessage(JSONObject arg0, IOAcknowledge arg1) {
    Log.d("socket5.io", "message from serveur"+arg0.toString());
}                                                        

@Override
public void onMessage(String arg0, IOAcknowledge arg1) {
    //System.out.println("Server said: " + arg0);
    Log.d("socket5.io", "message from serveur"+arg0);
 }

@Override
public void onDisconnect() {
    Log.i("socket.io", "deconecter");
    // TODO Auto-generated method stub
}
@Override
public void onConnect() {
    Log.d("socket.io", "connecter");
}

@Override
public void on(String event, IOAcknowledge ack, Object... args) {
}

public void deconnexion(){
    socket.disconnect();
}

public void send(String subscribe,JSONObject json){
    socket.emit(subscribe, json);
    Log.d("socket2.io", "emit");
}

@Override
public void onError(SocketIOException arg0) {
    Log.d("io.socket", "error : "+arg0.toString());

}

}

hope it's help

@StephenMilone
Copy link

I have a similar issue, for the socket factory I use the default. SocketIO.setDefaultSSLSocketFactory(SSLContext.getDefault());
And I'm not getting any callbacks from IOCallback but when I proxy the device, I see the call go out and get a HTTP 200 on Charles..very confusing.

@gnorbsl
Copy link

gnorbsl commented Nov 4, 2014

Same Issue here, used SocketIO.setDefaultSSLSocketFactory(SSLContext.getDefault()); after a NPE while handshake, it solved the issue but i get no output at all, no errors no logs for any Callbacks...

@temahussain
Copy link

This source uses an older version of https://github.com/TooTallNate/Java-WebSocket/commits/master underneath. You simply need to build its last version dated August 10, 2014 to get it work.

The following needs to be changed in WebsocketTransport.java:

From
this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(context));

To
try {
this.setSocket(context.getSocketFactory().createSocket());
} catch (IOException e) {
e.printStackTrace();
}

These changes are already pushed as the following pull request:
#103

@Elshad
Copy link

Elshad commented Jan 22, 2015

@temahussain good solution, thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants