接口定义
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; }