Search in sources :

Example 1 with ConnectionClosedHandler

use of org.eclipse.che.ide.websocket.events.ConnectionClosedHandler 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();
            }
        }
    });
}
Also used : ConnectionOpenedHandler(org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler) Action(org.eclipse.che.ide.api.action.Action) ConnectionErrorHandler(org.eclipse.che.ide.websocket.events.ConnectionErrorHandler) MessageReceivedHandler(org.eclipse.che.ide.websocket.events.MessageReceivedHandler) Jso(org.eclipse.che.ide.collections.Jso) Operation(org.eclipse.che.api.promises.client.Operation) MessageReceivedEvent(org.eclipse.che.ide.websocket.events.MessageReceivedEvent) WebSocketClosedEvent(org.eclipse.che.ide.websocket.events.WebSocketClosedEvent) AddTerminalClickHandler(org.eclipse.che.ide.extension.machine.client.processes.AddTerminalClickHandler) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) ConnectionClosedHandler(org.eclipse.che.ide.websocket.events.ConnectionClosedHandler)

Aggregations

JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 Operation (org.eclipse.che.api.promises.client.Operation)1 Action (org.eclipse.che.ide.api.action.Action)1 Jso (org.eclipse.che.ide.collections.Jso)1 AddTerminalClickHandler (org.eclipse.che.ide.extension.machine.client.processes.AddTerminalClickHandler)1 ConnectionClosedHandler (org.eclipse.che.ide.websocket.events.ConnectionClosedHandler)1 ConnectionErrorHandler (org.eclipse.che.ide.websocket.events.ConnectionErrorHandler)1 ConnectionOpenedHandler (org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler)1 MessageReceivedEvent (org.eclipse.che.ide.websocket.events.MessageReceivedEvent)1 MessageReceivedHandler (org.eclipse.che.ide.websocket.events.MessageReceivedHandler)1 WebSocketClosedEvent (org.eclipse.che.ide.websocket.events.WebSocketClosedEvent)1