eneter.messaging.messagingsystems.websocketmessagingsystem
Class WebSocketListener

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

public class WebSocketListener
extends java.lang.Object

WebSocket server.
The following example implements a simple service echoing the incoming message back to the client.

 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.net.URI;
 
 import eneter.messaging.messagingsystems.websocketmessagingsystem.*;
 import eneter.net.system.IMethod1;
 
 public class Program
 {
  public static void main(String[] args) throws Exception
    {
        WebSocketListener aService = new WebSocketListener(new URI("ws://127.0.0.1:8045/Echo/"));
        aService.startListening(new IMethod1<IWebSocketClientContext>()
            {
                // Method called if a client is connected.
                // The method is called is called in parallel for each connected client!
                public void invoke(IWebSocketClientContext client) throws Exception
                {
                    WebSocketMessage aMessage;
                    while ((aMessage = client.receiveMessage()) != null)
                    {
                        if (aMessage.isText())
                        {
                            String aTextMessage = aMessage.getWholeTextMessage();
                            
                            // Display the message.
                            System.out.println(aTextMessage);
                            
                            // Send back the echo.
                            client.sendMessage(aTextMessage);
                        }
                    }
                }
            });
        
        System.out.println("Websocket echo service is running. Press ENTER to stop.");
        new BufferedReader(new InputStreamReader(System.in)).readLine();
        
        aService.stopListening();
    }
 }
 
 


Constructor Summary
WebSocketListener(java.net.URI webSocketUri)
          Construct websocket service.
WebSocketListener(java.net.URI webSocketUri, IServerSecurityFactory securityFactory)
          Construct websocket service.
 
Method Summary
 java.net.URI getAddress()
          Returns address of the service.
 boolean isListening()
          Returns true if the service is listening.
 void startListening(IMethod1<IWebSocketClientContext> connectionHandler)
          Starts listening.
 void stopListening()
          Stops listening and closes all open connections with clients.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WebSocketListener

public WebSocketListener(java.net.URI webSocketUri)
Construct websocket service.

Parameters:
webSocketUri - service address. Provide port number too.

WebSocketListener

public WebSocketListener(java.net.URI webSocketUri,
                         IServerSecurityFactory securityFactory)
Construct websocket service.

Parameters:
webSocketUri - service address. Provide port number too.
securityFactory - Factory allowing SSL communication.
Method Detail

startListening

public void startListening(IMethod1<IWebSocketClientContext> connectionHandler)
                    throws java.lang.Exception
Starts listening. To handle connected clients the connectionHandler is called. The connectionHandler handler is called in parallel from multiple threads as clients are connected.

Parameters:
connectionHandler - callback handler handling incoming connections. It is called from multiple threads.
Throws:
java.lang.Exception

stopListening

public void stopListening()
Stops listening and closes all open connections with clients.


isListening

public boolean isListening()
                    throws java.lang.Exception
Returns true if the service is listening.

Returns:
true if listening.
Throws:
java.lang.Exception

getAddress

public java.net.URI getAddress()
Returns address of the service.