eneter.messaging.messagingsystems.websocketmessagingsystem
Class WebSocketClient

java.lang.Object
  extended by eneter.messaging.messagingsystems.websocketmessagingsystem.WebSocketClient

public class WebSocketClient
extends java.lang.Object

WebSocket client. Represents the client for the websocket communication.
The following example shows how to communicate with a websocket server.

 class MyClient
 {
 
     private EventHandler<WebSocketMessage> myOnResponseMessageReceived = EventHandler<WebSocketMessage>()
         {
             public void onEvent(Object sender, WebSocketMessage e)
             {
                 onResponseMessageReceived(sender, e);
             }
         }
 
     private WebSocketClient myClient;
 
 
     public MyClass() throws Exception
     {
         // Instantiate websocket client.
         myClient = new WebSocketClient("ws://127.0.0.1:8045/MyService/");
         
         // Subscribe to receive messages.
         myClient.messageReceived().subscribe(myOnResponseMessageReceived);
     }
 
     public void openConnection()
     {
         // Open the connection.
         myClient.openConnection();
     }
     
     public void closeConnection()
     {
         // Close the connection.
         myClient.closeConnection();
     }
 
     void sendHello()
     {
         // Send a text message.
         myClient.sendMessage("Hello.");
     }
     
     // Method called when a response message from the server is received.
     private void onResponseMessageReceived(Object sender, WebSocketMessage e)
     {
         try
         {
             if (y.isText())
             {
                 // If server responded the text message.
                 String aTextMessage = e.getWholeTextMessage();
                 
                 ...
             }
             else
             {
                 // The server responded the binary message.
                 byte[] aBinaryMessage = e.getWholeMessage();
                 
                 ...
             }
         }
         catch (Exception err)
         {
            ...
         }
     }
 }
 
 


Constructor Summary
WebSocketClient(java.net.URI address)
          Constructs the websocket client.
WebSocketClient(java.net.URI address, IClientSecurityFactory clientSecurityFactory)
          Constructs the websocket client.
 
Method Summary
 void closeConnection()
          Closes connection with the webscocket server.
 Event<java.lang.Object> connectionClosed()
          Event is invoked when the connection is closed.
 Event<java.lang.Object> connectionOpened()
          Event is invoked when the connection is open.
 int getConnectionTimeout()
           
 java.util.HashMap<java.lang.String,java.lang.String> getHeaderFields()
          Allows to get and set header-fields which shall be sent in open connection request.
 java.net.InetSocketAddress getLocalEndPoint()
           
 int getReceiveTimeout()
           
 int getSendTimeout()
           
 java.net.URI getUri()
          Returns address of websocket server.
 boolean isConnected()
          Returns true if the connection to the server is open.
 Event<WebSocketMessage> messageReceived()
          The event is invoked when a data message from server is received.
 void openConnection()
          Opens connection to the websocket server.
 Event<java.lang.Object> pongReceived()
          Event is invoked when the pong is received.
 void sendMessage(java.lang.Object data)
          Sends message to the client.
 void sendMessage(java.lang.Object data, boolean isFinal)
          Sends message to the client.
 void sendPing()
          Pings the server.
 void sendPong()
          Sends unsolicited pong to the server.
 void setConnectionTimeout(int connectionTimeout)
           
 void setReceiveTimeout(int receiveTimeout)
           
 void setSendTimeout(int sendTimeout)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WebSocketClient

public WebSocketClient(java.net.URI address)
Constructs the websocket client.

Parameters:
address - websocket uri address. Provide port number too. e.g. ws://127.0.0.1:8055/myservice/
You can also specify the query that can be used to pass some open connection related parameters. e.g. ws://127.0.0.1:8055/myservice/?param1=10¶m2=20

WebSocketClient

public WebSocketClient(java.net.URI address,
                       IClientSecurityFactory clientSecurityFactory)
Constructs the websocket client.

Parameters:
address - websocket uri address. Provide port number too. e.g. ws://127.0.0.1:8055/myservice/
You can also specify the query that can be used to pass some open connection related parameters. e.g. ws://127.0.0.1:8055/myservice/?param1=10¶m2=20
clientSecurityFactory - Factory allowing SSL communication.
Method Detail

connectionOpened

public Event<java.lang.Object> connectionOpened()
Event is invoked when the connection is open.

Returns:

connectionClosed

public Event<java.lang.Object> connectionClosed()
Event is invoked when the connection is closed.

Returns:

pongReceived

public Event<java.lang.Object> pongReceived()
Event is invoked when the pong is received. E.g. when the server responded ping.

Returns:

messageReceived

public Event<WebSocketMessage> messageReceived()
The event is invoked when a data message from server is received.

Returns:

setConnectionTimeout

public void setConnectionTimeout(int connectionTimeout)

getConnectionTimeout

public int getConnectionTimeout()

setSendTimeout

public void setSendTimeout(int sendTimeout)

getSendTimeout

public int getSendTimeout()

setReceiveTimeout

public void setReceiveTimeout(int receiveTimeout)

getReceiveTimeout

public int getReceiveTimeout()

getUri

public java.net.URI getUri()
Returns address of websocket server.

Returns:

getHeaderFields

public java.util.HashMap<java.lang.String,java.lang.String> getHeaderFields()
Allows to get and set header-fields which shall be sent in open connection request.

Returns:

getLocalEndPoint

public java.net.InetSocketAddress getLocalEndPoint()

isConnected

public boolean isConnected()
Returns true if the connection to the server is open.

Returns:

openConnection

public void openConnection()
                    throws java.lang.Exception
Opens connection to the websocket server.

Throws:
java.lang.Exception

closeConnection

public void closeConnection()
Closes connection with the webscocket server. It sends the close message to the service and closes the underlying tcp connection.


sendMessage

public void sendMessage(java.lang.Object data)
                 throws java.lang.Exception
Sends message to the client. The message must be type of string or byte[]. If the type is string then the message is sent as the text message via text frame. If the type is byte[] the message is sent as the binary message via binary frame.

Parameters:
data - message to be sent to the client. Must be byte[] or string.
Throws:
java.lang.Exception - If sending of the message failed.

sendMessage

public void sendMessage(java.lang.Object data,
                        boolean isFinal)
                 throws java.lang.Exception
Sends message to the client. Allows to send the message via multiple frames. The message must be type of string or byte[]. If the type is string then the message is sent as the text message via text frame. If the type is byte[] the message is sent as the binary message via binary frame.
It allows to send the message in multiple frames. The client then can receive all parts separately using WebSocketMessage.InputStream or as a whole message using WebSocketMessage.GetWholeMessage().
The following example shows how to send 'Hello world.' in three parts.
 // Send the first part of the message.
     aClient.sendMessage("Hello ", false);
     
     // Send the second part.
     aClient.sendMessage("wo", false);
     
     // Send the third final part.
     aClient.sendMessage("rld.", true);
 
 

Parameters:
data - message to be sent to the client. The message can be byte[] or string.
isFinal - true if this is the last part of the message.
Throws:
java.lang.Exception - If sending of the message failed.

sendPing

public void sendPing()
              throws java.lang.Exception
Pings the server. According to websocket protocol, pong should be responded.

Throws:
java.lang.Exception

sendPong

public void sendPong()
              throws java.lang.Exception
Sends unsolicited pong to the server.

Throws:
java.lang.Exception