4

I'm using gottox socket.io java client for an Android chat application.
I could connect to both web-socket and Xhr transport in HTTP mode. But when i switch to HTTPS only Xhr mode is working. i used the default SSL Context as below

SocketIO.setDefaultSSLSocketFactory(SSLContext.getInstance("Default"));

This works fine in Xhr mode. But in websocket transport there are no responses or errors.

4

3 回答 3

2

更新

可能是新版本IO.setDefaultSSLContextIO. setDefaultHostnameVerifier方法不可用。相反,现在我们可以创建自己的OkHttpClient并在其上设置主机名验证器和 ssl 套接字工厂等,如socket.io-client-java 使用中所述。这是那里的片段:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
                      .hostnameVerifier(myHostnameVerifier)
                      .sslSocketFactory(mySSLContext.getSocketFactory(), myX509TrustManager)
                      .build(); // default settings for all sockets 

IO.setDefaultOkHttpWebSocketFactory(okHttpClient); 
IO.setDefaultOkHttpCallFactory(okHttpClient);

初步答案:

长期以来,我对 Android 上的socket.ioio.socket:socket.io-client:0.7.0库版本有同样的问题。它曾经对协议工作正常,但是对于协议,它在建立连接给予时遇到了麻烦。httphttpsxhr poll errors

以下解决方案适用于我,无需修改库本身:

// Socket connection
private Socket mSocket;

// Configure options
IO.Options options = new IO.Options();
// ... add more options

// End point https 
String yourEndpoint = "https://whatever.yoururl.com"
String yourHostName = "yoururl.com"

// If https, explicitly tell set the sslContext.
if (yourEndpoint.startsWith("https://")) {        
    try {
        // Default settings for all sockets

        // Set default ssl context
        IO.setDefaultSSLContext(SSLContext.getDefault());

        // Set default hostname
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
                return hv.verify(yourHostName, session);
            }
        };
        IO.setDefaultHostnameVerifier(hostnameVerifier);

        // set as an option
        options.sslContext = SSLContext.getDefault();
        options.hostnameVerifier = hostnameVerifier;
        options.secure = true;

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

// Instantiate the socket
mSocket = IO.socket(mEndpoint, options);

希望这可以帮助。

于 2016-05-18T23:24:36.187 回答
1

它可以工作,但您必须对 io.socket 库进行一些修改。不使用 socketio.jar,而是将 io.socket 库导入 src 文件夹(您可以在 socket.io-java-client 包中找到)。在那里,您必须编辑 WebsocketTransport 类。

在这里你有解决方案

https://github.com/Gottox/socket.io-java-client/issues/60

 public WebsocketTransport(URI uri, IOConnection connection) {
    super(uri);
    this.connection = connection;
    SSLContext context = null;
    try {
        context = SSLContext.getInstance("TLS", "HarmonyJSSE");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } 
    try {
        context.init(null, null, null);
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    if("wss".equals(uri.getScheme()) && context != null) {
        this.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(context));
    }
}

记得像这样调用 setDefaultSSLSocketFactory:

socket = new SocketIO();
socket.setDefaultSSLSocketFactory(SSLContext.getDefault());
socket.connect("https://www.myHttpsServer.com:443/");

希望它可以帮助某人;)

于 2014-05-06T13:42:23.763 回答
0

带有 SSL 的 Websocket 在AndroidAsync中工作。暂时使用它。

于 2014-01-15T08:51:36.533 回答