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

org.noear.nami.channel.socketd.SocketdProxy Maven / Gradle / Ivy

There is a newer version: 3.0.5-M3
Show newest version
package org.noear.nami.channel.socketd;

import org.noear.socketd.SocketD;
import org.noear.socketd.transport.client.ClientSession;
import org.noear.socketd.transport.core.Session;
import org.noear.solon.core.handle.Context;
import org.noear.solon.net.socketd.handle.ToHandlerListener;

/**
 * Socketd 代理
 *
 * @author noear
 * @since 2.6
 */
public class SocketdProxy {
    public static final ToHandlerListener socketdToHandler = new ToHandlerListener();

    /**
     * 创建接口代理
     */
    public static  T create(String url, Class clz) throws Exception {
        ClientSession session;
        if (url.contains(",")) {
            session = SocketD.createClusterClient(url.split(","))
                    .listen(socketdToHandler)
                    .openOrThow();
        } else {
            session = SocketD.createClient(url)
                    .listen(socketdToHandler)
                    .openOrThow();
        }

        return ProxyUtils.create(() -> session, null, null, clz);
    }

    /**
     * 创建接口代理
     */
    public static  T create(ClientSession session, Class clz) {
        return ProxyUtils.create(() -> session, null, null, clz);
    }

    /**
     * 创建接口代理
     */
    public static  T create(Context ctx, Class clz) {
        if (ctx.response() instanceof Session) {
            return create((Session) ctx.response(), clz);
        } else {
            return create((Session) ctx.request(), clz);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy