public class WebSocketListener
extends java.lang.Object
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 and Description |
---|
WebSocketListener(java.net.URI webSocketUri)
Construct websocket service.
|
WebSocketListener(java.net.URI webSocketUri,
IServerSecurityFactory securityFactory)
Construct websocket service.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public WebSocketListener(java.net.URI webSocketUri)
webSocketUri
- service address. Provide port number too.public WebSocketListener(java.net.URI webSocketUri, IServerSecurityFactory securityFactory)
webSocketUri
- service address. Provide port number too.securityFactory
- Factory allowing SSL communication.public void startListening(IMethod1<IWebSocketClientContext> connectionHandler) throws java.lang.Exception
connectionHandler
- callback handler handling incoming connections. It is called from multiple threads.java.lang.Exception
public void stopListening()
public boolean isListening() throws java.lang.Exception
java.lang.Exception
public java.net.URI getAddress()