• 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;
        }
  • 相关阅读:
    poj 3320 Jessica's Reading Problem
    uva 120 C
    vim使用教程-转自
    2015 俄罗斯网络赛 D. Boulevard
    HTML转义字符大全
    介绍个好点的,JAVA技术群
    JAVA学习路线
    linux常用命令大全(转)好东西要分享
    Jqprint 轻量级页面打印插件
    hadoop集群搭建
  • 原文地址:https://www.cnblogs.com/LQBlog/p/16737702.html
Copyright © 2020-2023  润新知