Skip to content

mountainowl/nio-ssl-autobahn-android

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wss://AutobahnAndroid

wss://AutobahnAndroid | A fork of [AutoBahnAndroid project] (https://github.com/crossbario/autobahn-android) to support SSL over NIO.

WebSocket allows bidirectional real-time messaging on the Web.

Show me some code

Here is a simple Secure WebSocket echo client:

private final WebSocketConnection mConnection = new WebSocketConnection();

private void start() {

   final String wsuri = "wss://echo.websocket.org";

   try {
      mConnection.connect(wsuri, new WebSocketHandler() {

         @Override
         public void onOpen() {
            Log.d(TAG, "Status: Connected to " + wsuri);
            mConnection.sendTextMessage("Hello, world!");
         }

         @Override
         public void onTextMessage(String payload) {
            Log.d(TAG, "Got echo: " + payload);
         }

         @Override
         public void onClose(int code, String reason) {
            Log.d(TAG, "Connection lost.");
         }
      });
   } catch (WebSocketException e) {

      Log.d(TAG, e.toString());
   }
}

... and with a custom TrustManager :

private final WebSocketConnection mConnection = new WebSocketConnection();

/**
 
 * Trust all certificates by default; useful for self-signed certificates and testing; 
 * DO NOT USE FOR PRODUCTION CODE
 
 *
 
 * @return
 
 */public final TrustManager[] createTrustAllManager() {
	TrustManager[] trustAllCerts = null;
    
	if (BuildConfig.DEBUG) {

        
		System.err.println("*** TRUSTING ALL CERTIFICATES ***");

        
		trustAllCerts = new TrustManager[] {
                
			new X509TrustManager() {
				public X509Certificate[] getAcceptedIssuers() {
					X509Certificate[] myTrustedAnchors = new X509Certificate[0];
					return myTrustedAnchors;
				}

				@Override
				public void checkClientTrusted(X509Certificate[] certs, String authType) {
				}
				
				@Override
				public void checkServerTrusted(X509Certificate[] certs, String authType) {
				}
			}
		};
	} else {
		throw new RuntimeException(new CertificateException("\n\n***TRUST ALL CERTIFICATES DISABLED IN NON-DEBUG MODE ***\n\n"));
    
	}

    
	return trustAllCerts;

}

private void start() {

   final String wsuri = "wss://echo.websocket.org";
   final WebSocketSSLContext webSocketSSLContext = new WebSocketSSLContext();

   try {
      webSocketSSLContext.setSSLTrustManagers(createTrustAllManager());
      mConnection.setWebSocketSSLContext(webSocketSSLContext);    
      mConnection.connect(wsuri, new WebSocketHandler() {

         @Override
         public void onOpen() {
            Log.d(TAG, "Status: Connected to " + wsuri);
            mConnection.sendTextMessage("Hello, world!");
         }

         @Override
         public void onTextMessage(String payload) {
            Log.d(TAG, "Got echo: " + payload);
         }

         @Override
         public void onClose(int code, String reason) {
            Log.d(TAG, "Connection lost.");
         }
      });
   } catch (WebSocketException e) {

      Log.d(TAG, e.toString());
   }
}

Features

  • Same as AndroidAutobahn
  • Implements secure web sockets using SSL over pure-NIO

You can use wss://AutobahnAndroid to create native Android apps talking to WebSocket servers.

More Information

For more information, take a look at the project documentation. This provides:

About

WebSocket/Secure-Websocket for Android

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.0%
  • Python 1.0%