eneter.messaging.messagingsystems.websocketmessagingsystem
Interface IWebSocketClientContext


public interface IWebSocketClientContext

Represents the client on the server side. The client context is obtained when a client opened the connection with the server. It provides functionality to receive messages from the client and send back response messages. To see the example refer to WebSocketListener.


Method Summary
 void closeConnection()
          Closes connection with the client.
 Event<java.lang.Object> connectionClosed()
          The event is invoked when the connection with the client was closed.
 java.net.InetSocketAddress getClientEndPoint()
          Returns the IP address and port of the connected client.
 java.util.Map<java.lang.String,java.lang.String> getHeaderFields()
          Returns the readonly dictionary.
 int getReceiveTimeout()
          Gets the receive timeout in milliseconds.
 int getSendTimeout()
          Gets the send timeout in milliseconds.
 java.net.URI getUri()
          Returns URI of this connection including query parameters sent from by the client.
 boolean isConnected()
          Returns true if the client is connected.
 Event<java.lang.Object> pongReceived()
          The event is invoked when the pong message was received.
 WebSocketMessage receiveMessage()
          Waits until a message is received from the client.
 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 client.
 void sendPong()
          Sends unsolicited pong to the client.
 void setReceiveTimeout(int receiveTimeout)
          Sets the receive timeout in milliseconds.
 void setSendTimeout(int sendTimeout)
          Sets the send timeout in milliseconds.
 

Method Detail

connectionClosed

Event<java.lang.Object> connectionClosed()
The event is invoked when the connection with the client was closed.

Returns:
event

pongReceived

Event<java.lang.Object> pongReceived()
The event is invoked when the pong message was received.

Returns:
event

getClientEndPoint

java.net.InetSocketAddress getClientEndPoint()
Returns the IP address and port of the connected client.

Returns:
IP address

isConnected

boolean isConnected()
Returns true if the client is connected.

Returns:
true if the client is connected.

getUri

java.net.URI getUri()
Returns URI of this connection including query parameters sent from by the client.

Returns:
URI

getHeaderFields

java.util.Map<java.lang.String,java.lang.String> getHeaderFields()
Returns the readonly dictionary.

Returns:

setSendTimeout

void setSendTimeout(int sendTimeout)
Sets the send timeout in milliseconds. Default value is 0 what is infinite time.

Parameters:
sendTimeout -

getSendTimeout

int getSendTimeout()
Gets the send timeout in milliseconds. Default value is 0 what is infinite time.

Returns:
sending timeout in milliseconds

setReceiveTimeout

void setReceiveTimeout(int receiveTimeout)
                       throws java.lang.Exception
Sets the receive timeout in milliseconds. Default value is 0 what is infinite time.

Parameters:
receiveTimeout -
Throws:
java.lang.Exception

getReceiveTimeout

int getReceiveTimeout()
                      throws java.lang.Exception
Gets the receive timeout in milliseconds. Default value is 0 what is infinite time.

Returns:
receiving timeout in milliseconds
Throws:
java.lang.Exception

sendMessage

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

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.
 void ProcessConnection(IWebSocketClientContext clientContext)
 {
     ...
     
     // Send the first part of the message.
     clientContext.sendMessage("Hello ", false);
     
     // Send the second part.
     clientContext.sendMessage("wo", false);
     
     // Send the third final part.
     clientContext.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.

receiveMessage

WebSocketMessage receiveMessage()
                                throws java.lang.Exception
Waits until a message is received from the client.
Example shows how to implement a loop receiving the text messages from the client.
 void ProcessConnection(IWebSocketClientContext clientContext)
 {
     // The loop waiting for incoming messages.
     // Note: The waiting thread is released when the connection is closed.
     WebSocketMessage aWebSocketMessage;
     while ((aWebSocketMessage = clientContext.receiveMessage()) != null)
     {
         if (aWebSocketMessage.isText())
         {
             // Wait until all data frames are collected
             // and return the message.
             String aMessage = aWebSocketMessage.getWholeTextMessage();
             ...
         }
     }
 }
 
 

Returns:
websocket message
Throws:
java.lang.Exception

sendPing

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

Throws:
java.lang.Exception

sendPong

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

Throws:
java.lang.Exception

closeConnection

void closeConnection()
Closes connection with the client. It sends the close message to the client and closes the underlying tcp connection.