public class WebSocketClient
extends java.lang.Object
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 and Description |
---|
WebSocketClient(java.net.URI address)
Constructs the websocket client.
|
WebSocketClient(java.net.URI address,
IClientSecurityFactory clientSecurityFactory)
Constructs the websocket client.
|
Modifier and Type | Method and Description |
---|---|
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) |
public WebSocketClient(java.net.URI address)
address
- websocket uri address. Provide port number too. e.g. ws://127.0.0.1:8055/myservice/public WebSocketClient(java.net.URI address, IClientSecurityFactory clientSecurityFactory)
address
- websocket uri address. Provide port number too. e.g. ws://127.0.0.1:8055/myservice/clientSecurityFactory
- Factory allowing SSL communication.public Event<java.lang.Object> connectionOpened()
public Event<java.lang.Object> connectionClosed()
public Event<java.lang.Object> pongReceived()
public Event<WebSocketMessage> messageReceived()
public void setConnectionTimeout(int connectionTimeout)
public int getConnectionTimeout()
public void setSendTimeout(int sendTimeout)
public int getSendTimeout()
public void setReceiveTimeout(int receiveTimeout)
public int getReceiveTimeout()
public java.net.URI getUri()
public java.util.HashMap<java.lang.String,java.lang.String> getHeaderFields()
public java.net.InetSocketAddress getLocalEndPoint()
public boolean isConnected()
public void openConnection() throws java.lang.Exception
java.lang.Exception
public void closeConnection()
public void sendMessage(java.lang.Object data) throws java.lang.Exception
data
- message to be sent to the client. Must be byte[] or string.java.lang.Exception
- If sending of the message failed.public void sendMessage(java.lang.Object data, boolean isFinal) throws java.lang.Exception
// 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);
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.java.lang.Exception
- If sending of the message failed.public void sendPing() throws java.lang.Exception
java.lang.Exception
public void sendPong() throws java.lang.Exception
java.lang.Exception