|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objecteneter.messaging.messagingsystems.websocketmessagingsystem.WebSocketListener
public class WebSocketListener
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 |
---|
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.Method Detail |
---|
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()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |