use of org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler in project che by eclipse.
the class WorkspaceComponent method handleWorkspaceEvents.
/**
* Listens message bus and handles workspace events.
*
* @param workspace
* workspace to listen
* @param callback
* callback
* @param restoreFromSnapshot
* restore or not the workspace from snapshot
*/
public void handleWorkspaceEvents(final WorkspaceDto workspace, final Callback<Component, Exception> callback, final Boolean restoreFromSnapshot) {
this.callback = callback;
if (messageBus != null) {
messageBus.cancelReconnection();
}
messageBus = messageBusProvider.createMessageBus();
messageBus.addOnOpenHandler(new ConnectionOpenedHandler() {
@Override
public void onOpen() {
loader.show(STARTING_WORKSPACE_RUNTIME);
messageBus.removeOnOpenHandler(this);
setCurrentWorkspace(workspace);
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
final WorkspaceStatus workspaceStatus = workspace.getStatus();
switch(workspaceStatus) {
case SNAPSHOTTING:
loader.show(CREATING_WORKSPACE_SNAPSHOT);
break;
case STARTING:
eventBus.fireEvent(new WorkspaceStartingEvent(workspace));
break;
case RUNNING:
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
@Override
public void execute() {
loader.setSuccess(STARTING_WORKSPACE_RUNTIME);
eventBus.fireEvent(new WorkspaceStartedEvent(workspace));
}
});
break;
default:
//
workspaceServiceClient.getSettings().then(new Function<Map<String, String>, Map<String, String>>() {
@Override
public Map<String, String> apply(Map<String, String> settings) throws FunctionException {
if (Boolean.parseBoolean(settings.getOrDefault(CHE_WORKSPACE_AUTO_START, "true"))) {
startWorkspaceById(workspace.getId(), workspace.getConfig().getDefaultEnv(), restoreFromSnapshot);
} else {
loader.show(WORKSPACE_STOPPED);
}
return settings;
}
});
}
}
});
}
use of org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler 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