use of org.eclipse.che.ide.websocket.events.WebSocketClosedEvent in project che by eclipse.
the class TerminalPresenter method connectToTerminalWebSocket.
private void connectToTerminalWebSocket(@NotNull String wsUrl) {
countRetry--;
socket = WebSocket.create(wsUrl);
socket.setOnMessageHandler(new MessageReceivedHandler() {
@Override
public void onMessageReceived(MessageReceivedEvent event) {
terminal.write(event.getMessage());
}
});
socket.setOnCloseHandler(new ConnectionClosedHandler() {
@Override
public void onClose(WebSocketClosedEvent event) {
if (CLOSE_NORMAL == event.getCode()) {
connected = false;
terminalStateListener.onExit();
}
}
});
socket.setOnOpenHandler(new ConnectionOpenedHandler() {
@Override
public void onOpen() {
JavaScriptObject terminalJso = moduleHolder.getModule("Xterm");
// if terminal was created programmatically then we don't set focus on it
TerminalOptionsJso terminalOptionsJso = TerminalOptionsJso.createDefault();
if (source instanceof AddTerminalClickHandler || source instanceof Action) {
terminalOptionsJso.withFocusOnOpen(true);
}
terminal = TerminalJso.create(terminalJso, terminalOptionsJso);
connected = true;
view.openTerminal(terminal);
terminal.on(DATA_EVENT_NAME, new Operation<String>() {
@Override
public void apply(String arg) throws OperationException {
Jso jso = Jso.create();
jso.addField("type", "data");
jso.addField("data", arg);
socket.send(jso.serialize());
}
});
}
});
socket.setOnErrorHandler(new ConnectionErrorHandler() {
@Override
public void onError() {
connected = false;
if (countRetry == 0) {
view.showErrorMessage(locale.terminalErrorStart());
notificationManager.notify(locale.connectionFailedWithTerminal(), locale.terminalErrorConnection(), FAIL, FLOAT_MODE);
} else {
reconnect();
}
}
});
}
Aggregations