2016-11-04 198 views
1

我試圖使用websocket連接到使用react-native的TLS服務器。下面是我的代碼(上+ Android的Windows上運行):React-native websocket TLS連接

var ws = new WebSocket('wss://hub.fingi-staging.com:20020',{ 
    rejectUnauthorized: false 
}); 

ws.onopen =() => { 
    // connection opened 

    ws.send('something'); // send a message 
}; 

ws.onmessage = (e) => { 
    // a message was received 

    console.log('message : ' + e.data); 
}; 

ws.onerror = (e) => { 
    // an error occurred 
    console.log('error:'+e.message); 
}; 

ws.onclose = (e) => { 
    // connection closed 
    console.log('close:'+e.code, e.reason); 
}; 

然而,它失敗:error:java.security.cert.CertPathValidatorException: Trust anchor for certification path not found。這是因爲服務器使用自簽名證書。

有什麼辦法解決這個問題嗎?

回答

1

回覆遲了一點,但希望這可以指向其他人在正確的方向。

我相信您收到的錯誤表明您缺少證書鏈文件,該文件用於驗證用於簽署服務器證書的CA是否有效,即信任鏈是否有效。

但是,如果您正在使用自簽名證書,通常會失敗(至少很麻煩)。如果您需要一些幫助來生成一些自簽名證書和相應的證書鏈,您可以看看here。另外,請參閱是否需要通過讓客戶端在連接時使用該文件作爲參數來指定可信CA。

我一直在努力與使用自簽名證書籤名建立安全的WebSocket服務器(用於開發目的,在生產中正確的證書/ CA必須使用),但沒有太多的成功,並恢復到使用非TLS websocket服務器。