1. How to find a suitable websocket client? See http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations
2. Because websocket needs to be used on Android, WSS is required, then I chose java_websocket (http://java-websocket.org /)
This ws client does not support WSS well. The WSS client in the source code also needs to use certificates. My idea is not to verify the certificate. Slightly modify his code:
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.net.URI;import java.security.KeyStore;import javax.net.ssl.KeyManagerFactory;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManagerFactory;import org.java_websocket.WebSocket;import org.java_websocket.client.DefaultSSLWebSocketClientFactory;import org.java_websocket.client.WebSocketClient;import org.java_websocket.handshake.ServerHandshake;class WebSocketChatClient extends WebSocketClient {public WebSocketChatClient( URI serverUri ) {super( serverUri );}@Overridepublic void onOpen( ServerHandshake handshakedata ) {System.out.println( "Connected" );}@Overridepublic void onMessage( String message ) {System.out.println( "got: " + message );}@Overridepublic void onClose( int code, String reason, boolean remote ) {System.out.println( "Disconnected" );System.exit( 0 );}@Overridepublic void onError( Exception ex ) {ex.printStackTrace();}}public class SSLClientExample {
// always verify the host - dont check for certificate final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; /** * Trust every server - dont check for any certificate */ private static void trustAllHosts(AppWebSocketClient appClient) { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[]{}; } public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } }}; // Install the all-trusting trust manager try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); appClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sc)); } catch (Exception e) { e.printStackTrace(); } }public static void main( String[] args ) throws Exception {WebSocket.DEBUG = true;WebSocketChatClient chatclient = new WebSocketChatClient( new URI( "wss://localhost:8887" ) );trustAllHosts(appClient);chatclient.connectBlocking();BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );while ( true ) {String line = reader.readLine();if( line.equals( "close" ) ) {chatclient.close();} else {chatclient.send( line );}}}}