cometd源码阅读-Extension扩展(十)

接口定义

复制代码
 public interface Extension {
        /**
         * <p>Callback method invoked every time a message is incoming.</p>
         *   <调用源码处>每次消息传入时调用的回调方法。我们可以在消息里面加一些定制内容 或者拦截加密
         * @param from    the session that sent the message
         * @param message the incoming message
         * @param promise the promise to notify whether message processing should continue
         */
        default void incoming(ServerSession from, ServerMessage.Mutable message, Promise<Boolean> promise) {
            promise.succeed(message.isMeta() ? rcvMeta(from, message) : rcv(from, message));
        }

        /**
         * <p>Blocking version of {@link #incoming(ServerSession, ServerMessage.Mutable, Promise)}
         * for non-meta messages.</p>
         * incoming分发默认实现  每次消息传入时调用的回调方法。针对普通消息调用
         * @param from    the session that sent the message
         * @param message the incoming message
         * @return whether message processing should continue
         */
        default boolean rcv(ServerSession from, ServerMessage.Mutable message) {
            return true;
        }

        /**
         * <p>Blocking version of {@link #incoming(ServerSession, ServerMessage.Mutable, Promise)}
         * for meta messages.</p>
         *rcvMeta分发默认实现 每次消息传入时调用的回调方法。针对内置协议相关消息处理/meta
         * @param from    the session that sent the message
         * @param message the incoming message
         * @return whether message processing should continue
         */
        default boolean rcvMeta(ServerSession from, ServerMessage.Mutable message) {
            return true;
        }

        /**
         * <p>Callback method invoked every time a message is outgoing.</p>
         *Callback method invoked every time a message is outgoing
         * <源码调用处> 每次消息传出时调用回调方法
         * @param from    the session that sent the message or null
         * @param to      the session the message is sent to, or null for a publish.
         * @param message the outgoing message
         * @param promise the promise to notify whether message processing should continue
         */
        default void outgoing(ServerSession from, ServerSession to, ServerMessage.Mutable message, Promise<Boolean> promise) {
            promise.succeed(message.isMeta() ? sendMeta(to, message) : send(from, to, message));
        }

        /**
         * <p>Blocking version of {@link #outgoing(ServerSession, ServerSession, ServerMessage.Mutable, Promise)}
         * for non-meta messages.</p>
         * outgoing 分发默认是实现 普通消息发送的回调
         * @param from    the session that sent the message or null
         * @param to      the session the message is sent to, or null for a publish.
         * @param message the outgoing message
         * @return whether message processing should continue
         */
        default boolean send(ServerSession from, ServerSession to, ServerMessage.Mutable message) {
            return true;
        }

        /**
         * <p>Blocking version of {@link #outgoing(ServerSession, ServerSession, ServerMessage.Mutable, Promise)}
         * for meta messages.</p>
         * outgoing分发默认实现 内置协议消息发送的回调
         * @param to      the session the message is sent to, or null for a publish.
         * @param message the outgoing message
         * @return whether message processing should continue
         */
        default boolean sendMeta(ServerSession to, ServerMessage.Mutable message) {
            return true;
        }
    }
复制代码

Extendsion2种生命周期

一种是全局拦截 一种是针对session级别的 我们可以看到触发Extend都分别会调用session或者全局的

复制代码
 //调用全局outgoing生命周期
        extendOutgoing(sender, session, reply, Promise.from(b -> {
            if (b) {
                if (session != null) {
                    //调用session级的outgoing生命周期
                    session.extendOutgoing(sender, reply, promise);
                } else {
                    promise.succeed(reply);
                }
            } else {
                promise.succeed(null);
            }
        }, promise::fail));
复制代码

全局拦截初始化

BayeuxServer bayeuxServer = ...;
bayeuxServer.addExtension(new ExtensionA());
bayeuxServer.addExtension(new ExtensionB());

Session级初始化

我们可以通过全局Extension 在握手成功进行初始化session 如以下

 @Override
    public boolean rcvMeta(ServerSession session, ServerMessage.Mutable message) {
        if (Channel.META_HANDSHAKE.equals(message.getChannel())) {
            session.addExtension(newSessionExtension(session, message));
        }
        return true;
    }

 

posted @   意犹未尽  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
历史上的今天:
2016-09-28 正则表达式环视
点击右上角即可分享
微信分享提示