com.openfin.desktop.net.WebSocketConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openfin-desktop-java-adapter Show documentation
Show all versions of openfin-desktop-java-adapter Show documentation
The Java API for OpenFin Runtime
package com.openfin.desktop.net;
import java.net.URI;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.handshake.ServerHandshake;
public class WebSocketConnection {
private URI uri;
private WebSocketEventHandler eventHandler;
private WebSocketClientImpl websocket;
public WebSocketConnection(URI uri) {
this.uri = uri;
}
public void send(String msg) throws WebSocketException {
this.websocket.send(msg);
}
public void close(String reason) throws WebSocketException {
this.websocket.close(CloseFrame.NORMAL, reason);
}
public void setEventHandler(WebSocketEventHandler eventHandler) {
this.eventHandler = eventHandler;
}
public void connect() throws WebSocketException {
this.websocket = new WebSocketClientImpl(this.uri);
this.websocket.connect();
}
public boolean isConnected() {
return this.websocket != null && this.websocket.isOpen();
}
public void setMaxMessageSize(int maxMessageSize) {
//not sure if there is a limit
}
class WebSocketClientImpl extends WebSocketClient {
public WebSocketClientImpl(URI serverUri) {
super(serverUri);
}
@Override
public void onOpen(ServerHandshake handshakedata) {
if (eventHandler != null) {
eventHandler.onOpen();
}
}
@Override
public void onMessage(String message) {
if (eventHandler != null) {
eventHandler.onMessage(new WebSocketMessage(message));
}
}
@Override
public void onClose(int code, String reason, boolean remote) {
if (eventHandler != null) {
eventHandler.onClose();
}
}
@Override
public void onError(Exception ex) {
}
}
}