All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.openfin.desktop.net.WebSocketConnection Maven / Gradle / Ivy

There is a newer version: 11.0.2
Show newest version
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) {
		}
		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy