2013-04-24 159 views
3

我正在使用gottox socket.io java client進行Android聊天應用程序。
我可以連接到HTTP模式下的web-socket和Xhr傳輸。但是,當我切換到HTTPS只Xhr模式正在工作。我使用如下的默認SSL上下文Android Socket.io Websocket傳輸不適用於SSL

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

這在Xhr模式下正常工作。但在websocket傳輸中沒有任何反應或錯誤。

+0

你可以分享你的代碼,你如何改變傳輸到只有websocket – 2017-02-28 11:19:00

回答

0

帶SSL的Websocket在AndroidAsync中工作。現在使用它。

1

它的工作原理,但你必須做一些修改io.socket庫。 而不是使用socketio.jar,導入到src文件夾中的io.socket庫(您會在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/"); 

希望它可以幫助別人;)

0

我有與io.socket:socket.io-client:0.7.0版本相同的問題o f socket.io圖書館在Android上。它曾經爲http協議工作正常,但是對於https協議,它在建立連接時遇到問題xhr 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); 

希望這有助於。

+0

'IO.setDefaultSSLContext'和'IO。 setDefaultHostnameVerifier'在版本1.0中不再可用 – ffleandro 2017-09-13 19:14:47