2012-02-09 104 views
1

如何在java中訪問wss://協議?如何使用websocket和socket.io在JAVA中連接到SSL?

我使用benkay/java-socket.io.client 但它不支持wss協議。

我試過使用SSLEngine。但這是非常艱苦的工作。

如何在java中連接到ssl?

我試圖通過SSLEngine更改SocketChannel。但它不起作用。

ssl通道沒問題。但我無法連接這個原始的websocket部分。

這是源代碼。

client = SocketChannel.open(remote); 
    client.configureBlocking(false); 
    //client.connect(remote); 

    selector = Selector.open(); 
    this.conn = new WebSocket(client, new LinkedBlockingQueue<ByteBuffer>(), this); 
    client.register(selector, SelectionKey.OP_READ); 

    try { 
    sslClient = new SSLClient(keyStore, storepass.toCharArray(), client); 
    sslClient.beginHandShake(); 
     startClient() 


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

這一點uncorret?我不知道..不一樣的原始websocket代碼..可能問題是這一點。如何解決它?

public void startClient() 
{ 
    try 
    { 
     while(true) 
     { 
      if(selector.select() <= 0) 
      { 
       continue; 
      } 

      Iterator<SelectionKey> it = selector.selectedKeys().iterator(); 

      while(it.hasNext()) 
      { 
       SelectionKey key = (SelectionKey)it.next(); 
       Log.e("key","key"); 
       if(key.isReadable()) 
       { 
        read(key); 
       } 
       it.remove(); 
      }    
     } 
    } 
    catch(Exception e) 
    { 

    } 
} 

和SSLClient是http://rapidant.tistory.com/attachment/[email protected]

密鑰存儲:JKS變化對BKS,沒有問題。

如何包裝SocketChannel?

(Web瀏覽器,它的工作。)

+0

我想將SocketChannel更改爲SSLSocketChannel。 但它不起作用。直到握手 – 2012-02-09 08:34:44

+0

或許selector.select()= 0的問題。 – 2012-02-10 01:38:35

回答