Previous Topic

Next Topic

Book Contents

Book Index

Creating WebSockets Application (deprecated)

Create OSGi Service

DemoWebSocketApplication.java:

// Demo class which extends the abstract class WebSocketApplication
public class DemoWebSocketApplication  extends WebSocketApplication {

   public static final String ALIAS =  "/demo/example";

   private static final String DEFAULT_SUB_PROTOCOL =  "demo_defaultSubProtocol";
   private static final String NAME =  "demo_name";

   public DemoApplication() {
     super(DEFAULT_SUB_PROTOCOL, ALIAS, NAME);
    
     //you can add multiple subProtocols
     //supportedSubProtocols.add("someSubProtocol" );
  }

   //Methods below are optional if  you are working with SocketSender
   //Attach WebSocketListener(s) to your WebSocketApplication
  @Override
   public void manageWebSocketListeners(WebSocketListenerManager webSocket) {
     //Option a. - we use our own DemoWebSocketListener class which extends  the WebSocketListenerAdapter
     //and overrides the methods we require
    webSocket.addListener( new DemoWebSocketListener());

     //Option b. - the WebSocketApplication itself is a WebSocketListener
     //so we can attach it and use it as a global point of listening
     //for  some minor action
    webSocket.addListener( this);
  }

   //In connected() you obtain the webSocket so that you can work with it
  @Override
   public void connected(WebSocket webSocket) {
     //doSomething
  }
}

Registering the OSGi Service

DemoActivator.java:

// Demo Activator
public class DemoActivator  implements BundleActivator {

   private ServiceRegistration demoServiceRegistration;

   public void start(BundleContext context)  throws Exception {
    DemoWebSocketApplication demoWebSocketApplication =  new DemoWebSocketApplication();

    Dictionary< String,  String> demoProperties =  new Hashtable< String,  String>();

    demoProperties.put(WebSocketApplication.URI, DemoWebSocketApplication.ALIAS);

    demoServiceRegistration = context.registerService(WebSocketApplication.class, demoWebSocketApplication, demoProperties);
  }

   public void stop(BundleContext context)  throws Exception {
     if (demoServiceRegistration !=  null) {
        demoServiceRegistration.unregister();
        demoServiceRegistration =  null;
    }
  }
}

Using the Bosch IoT Gateway Software WebSockets

Use the default Bosch IoT Gateway Software WebSocket implementation

DemoWebSocketApplication.java:

// Demo class which extends the  abstract class WebSocketApplication and uses the SimpleWebSocket
public class DemoWebSocketApplication  extends WebSocketApplication {

   public final static String ALIAS =  "/demo/example";

   private final String defaultSubProtocol =  "demo_defaultSubProtocol";
   private final String name =  "demo_name";

   public DemoApplication() {
     super(defaultSubProtocol, ALIAS, name);
  }

  @Override
   public void manageWebSocketListeners(WebSocketListenerManager webSocket) {
     //Option 1: the WebSocketApplication itself is a WebSocketListener
     //so we can attach it and use it as a global point of listening
    webSocket.addListener( this);

     //Option 2: we use our own DemoWebSocketListener class which extends  the WebSocketListenerAdapter
     //and overrides the methods we require
    webSocket.addListener( new DemoWebSocketListener());
  }

   //override connected() if  you want to obtain the webSocket as soon as it's available
  @Override
   public void connected(WebSocket webSocket) {
     //can save this  webSocket in some collection or just send message or whatever you want
  }

   //override any method which your application requires if  it has been added as a WebSocketListener
  @Override
   public void onMessage(WebSocket webSocket,  String text) {
     //doSomething, or obtain the webSocket if  you need it and haven't obtained it inconnected
    
  }
  
  .
  .
  .
}

Use your own WebSocket implementation to extend SimpleWebSocket

Use your own WebSocket implementation which extends the SimpleWebSocket, same approach as option b)

DemoWebSocket.java:

//Demo of a custom WebSocket implementation
//You can add fields, constructors, methods and any custom logic, that your application requires
public class DemoWebSocket  extends SimpleWebSocket {

   public DemoWebSocket(WebSocketHandler webSocketHandler,  String individualUri,
       String sessionId, WebSocketId id,  String applicationUri,  String subProtocol) {

     super(webSocketHandler, individualUri, sessionId, id, applicationUri, subProtocol);
  }
  
  @Override
   public void connected() {
    
     //send messages immediately after establishing the connection if  it is required
  }

  .
  .
  .
}

DemoWebSocketApplication.java:

// Demo class which extends the  abstract class WebSocketApplication and uses a different WebSocket implementation
public class DemoWebSocketApplication  extends WebSocketApplication {


   public final static String ALIAS =  "/demo/example";

   private final String defaultSubProtocol =  "demo_defaultSubProtocol";
   private final String name =  "demo_name";

   public DemoApplication() {
     super(defaultSubProtocol, ALIAS, name);
  }

  @Override
   public WebSocket doCreateWebSocket(WebSocketHandler handler,  String individualUri,  String sessionId,
      WebSocketId id,  String applicationUri,  String subProtocol) {

     return new DemoWebSocket(handler, individualUri, sessionId, id, applicationUri, subProtocol);
  }

  @Override
   public void manageWebSocketListeners(WebSocketListenerManager webSocket) {
     //Use any of the three options mentioned in b. to attach a WebSocketListener
    webSocket.addListener(...);
  }

   //override connected() if  you want to obtain the webSocket as soon as it's available
  @Override
   public void connected(WebSocket webSocket) {
     //can save this  webSocket in some collection or just send message or whatever you want
  }

   //override any method which your application requires if  it has been added as a WebSocketListener
  @Override
   public void onMessage(WebSocket webSocket,  String text) {
     //doSomething, or obtain the webSocket if  you have not done so in connected
    
  }
  
  .
  .
  .
}

Use SocketSender

Use SocketSender:

DemoSocketSender.java:

// DemoSocketSender class to demonstrate the SocketSender
public class DemoSocketSender  extends SocketSender<DemoWebSocketApplication> {

   public final static String ALIAS =  "/demo/example";

   public DemoSocketSender(DemoWebSocketApplication demoWebSocketApplication) {
     super(demoWebSocketApplication);
  }

  @Override
   public void connected() {
     //send something here if  your application requires it
    sendText( "Welcome to the DemoWebSocketApplication!");
  }

   //override any method which your application requires
  @Override
   public void onMessage( String text) {
     //doSomething
  }
  
  .
  .
  .
}

DemoWebSocketApplication.java:

// Demo class which extends the  abstract class WebSocketApplication and uses a SocketSender
public class DemoWebSocketApplication  extends WebSocketApplication {

   public final static String ALIAS =  "/demo/example";

   private final String defaultSubProtocol =  "demo_defaultSubProtocol";
   private final String name =  "demo_name";

   public DemoApplication() {
     super(defaultSubProtocol, ALIAS, name);
  }

  @Override
   public SocketSender createSocketSender() {
     return new DemoSocketSender( this);
  }
}

Additional Information

Using the WebSocketListeners

DemoWebSocketApplication.java:

//Creating a SocketSender with webSocket example
public class DemoWebSocketApplication  extends WebSocketApplication {
  .
  .
  .

  @Override
  public void onMessage(WebSocket webSocket,  String text) {
    DemoSocketSender demoSocketSender =  new DemoSocketSender( this, webSocket);
    WebSocketListener webSocketListener = demoSocketSender.getWebSocketListener();

    webSocket.add(webSocketListener);

     //doSomethingElse
  }

  .
  .
  .
}

Using the FrameTransmitter

Closing a WebSocket

Ping Pong Mechanism

Our WebSockets Server implements a default server initiated Ping-Pong mechanism. The server sends a Ping frame to all connected WebSocket instances at a configurable predefined time. If a Pong frame is not received from a given WebSocket instance within the same time interval, it is closed.

The System Ping is not useful for some WebSocketApplication services. It can be disabled a custom pinging implementation can be used. To disable it the notUsingSystemPing() must be overridden in your WebSocketApplication

public class DemoWebSocketApplication  extends WebSocketApplication {
  .
  .
  .

  /*
  return true if  you wish to disable System Ping
  by default this  method returns  false , which indicates
  that you are going to use the System  Ping
  */
  @Override
  public boolean notUsingSystemPing() {
     return true;
  }
  
  .
  .
  .
}

When the client sends the specific keep alive (Heartbeat) message (which starts with &#!^@#$) the server ignores it and will not notify the WebSockets Listeners, because this message is used only to verify if the connection is still open.

Handling WebSocket Exceptions

Monitoring the Bosch IoT Gateway Software WebSockets Implementation

You can obtain a large variety of information about the current state of the Bosch IoT Gateway Software WebSockets Server, registered WebSocketApplication services, created and recently closed WebSocket instances, running StandAlone HTTP Servers, etc. To do so, track the WebSocketsServerInfo service (for more details see the com.prosyst.mbs.services.websockets.info package).

  1. WebSocket information is found in the WebSocketInfo class, available through the corresponding getter methods of the class (WebSocketInfo)
  2. WebSocketApplication information found in the WebSocketApplicationInfo class, available through the corresponding getter methods of the class (WebSocketApplicationInfo)
  3. Standalone Server information is found in the StandaloneServerInfo class, available through the corresponding getter methods of the class (StandaloneServerInfo)