EIOWebSocketTransport.java 3.13 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.eclipse.jetty.websocket.api.RemoteEndpoint
 *  org.eclipse.jetty.websocket.api.Session
 *  org.eclipse.jetty.websocket.api.WebSocketListener
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.socketio.impl.engine;

import com.adobe.granite.socketio.impl.engine.EIOPacket;
import com.adobe.granite.socketio.impl.engine.EIOTransport;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.Future;
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EIOWebSocketTransport
extends EIOTransport
implements WebSocketListener {
    private static final Logger log = LoggerFactory.getLogger(EIOWebSocketTransport.class);
    public static final String NAME = "websocket";
    public static final Set<String> UPGRADES = Collections.emptySet();
    private Session outbound;
    private final String id;

    public EIOWebSocketTransport(String id) {
        this.id = id;
    }

    @Override
    public String getName() {
        return "websocket";
    }

    @Override
    public boolean supportsFraming() {
        return true;
    }

    @Override
    public Set<String> getUpgrades() {
        return UPGRADES;
    }

    @Override
    public /* varargs */ void send(EIOPacket ... packets) {
        if (this.outbound == null) {
            return;
        }
        for (EIOPacket packet : packets) {
            String encoded = packet.encode();
            log.trace("[{}] sendString({})", (Object)this.id, (Object)encoded);
            try {
                this.outbound.getRemote().sendStringByFuture(encoded).get();
                continue;
            }
            catch (Exception e) {
                log.error("[{}] error while sending data", (Throwable)e);
                this.onError(e.getMessage());
            }
        }
    }

    @Override
    protected void doClose() {
        log.debug("[{}] closing", (Object)this.id);
        if (this.outbound != null) {
            this.outbound.close();
        }
    }

    public void onWebSocketConnect(Session session) {
        this.outbound = session;
        this.onOpen();
    }

    public void onWebSocketClose(int statusCode, String reason) {
        log.debug("[{}] onWebSocketClose({}, {})", new Object[]{this.id, statusCode, reason});
        this.outbound = null;
        this.onClose();
    }

    public void onWebSocketError(Throwable cause) {
        log.debug("[{}] onWebSocketError()", (Object)this.id, (Object)cause);
        this.onError(cause.getMessage());
    }

    public void onWebSocketText(String message) {
        log.trace("[{}] onWebSocketText({})", (Object)this.id, (Object)message);
        this.onData(message);
    }

    public void onWebSocketBinary(byte[] payload, int offset, int len) {
        log.trace("[{}] onWebSocketBinary(<data>, {}, {})", new Object[]{this.id, offset, len});
        throw new UnsupportedOperationException("binary frames not supported yet.");
    }
}