接口定义
java.util.EventListener
/** * A tagging interface that all event listener interfaces must extend. * @since JDK1.1 */ public interface EventListener { }
org.cometd.bayeux.Bayeux.BayeuxListener
/** * <p>The common base interface for Bayeux listeners.</p> * <p>Specific sub-interfaces define what kind of events listeners will be notified.</p> */ interface BayeuxListener extends EventListener { }
org.cometd.bayeux.Bayeux.BayeuxListener
interface BayeuxServerListener extends BayeuxListener { }
监听器实现
ChannelListener
使用
BayeuxServer bayeuxServer = ...; bayeuxServer.addListener(new ChannelListener(){....}); bayeuxServer.addListener(new ChannelListener(){....});
接口定义
public interface ChannelListener extends BayeuxServerListener, ConfigurableServerChannel.Initializer { /** * <p>Callback invoked when a {@link ServerChannel} has been added to a {@link BayeuxServer} object.</p> * <源码处>channel初始化成功 * @param channel the channel that has been added */ public default void channelAdded(ServerChannel channel) { } /** * <p>Callback invoked when a {@link ServerChannel} has been removed from a {@link BayeuxServer} object.</p> * <源码处>channel初始化移除成功 * @param channelId the channel identifier of the channel that has been removed. */ public default void channelRemoved(String channelId) { } }
SessionListener
使用
BayeuxServer bayeuxServer = ...; bayeuxServer.addListener(new SessionListener(){....}); bayeuxServer.addListener(new SessionListener(){....});
接口定义
public interface SessionListener extends BayeuxServerListener { /** * <p>Callback invoked when a {@link ServerSession} has been added to a {@link BayeuxServer} object.</p> * <源码调用处>握手成功 内部session成功 * @param session the session that has been added * @param message the handshake message from the client */ public default void sessionAdded(ServerSession session, ServerMessage message) { } /** * <p>Callback invoked when a {@link ServerSession} has been removed from a {@link BayeuxServer} object.</p> *<源码调用处> session移除后监听回调 比如我们手动调用remove 或者超时自动剔除 * @param session the session that has been removed * @param message the message that caused the session removal, or null * @param timeout whether the session has been removed due to a timeout 可以判断是否是超时剔除 */ public default void sessionRemoved(ServerSession session, ServerMessage message, boolean timeout) { sessionRemoved(session, timeout); } /** * <p>Callback invoked when a {@link ServerSession} has been removed from a {@link BayeuxServer} object.</p> * * @param session the session that has been removed * @param timeout whether the session has been removed for a timeout or not * @deprecated use {@link #sessionRemoved(ServerSession, ServerMessage, boolean)} instead */ @Deprecated public default void sessionRemoved(ServerSession session, boolean timeout) { } }
SubscriptionListener
全局使用
BayeuxServer bayeuxServer = ...; bayeuxServer.addListener(new SubsriptionListener(){....}); bayeuxServer.addListener(new SubscriptionListener(){....});
channel级 利用ChannelListener全局监听器实现
/** * <p>Callback invoked when a {@link ServerChannel} has been added to a {@link BayeuxServer} object.</p> * channel初始化成功 * @param channel the channel that has been added */ public void channelAdded(ServerChannel channel) { if(channel.isService()){ channel.addListener(new ServerChannel.SubscriptionListener() { @Override public void subscribed(ServerSession session, ServerChannel channel, ServerMessage message) { } @Override public void unsubscribed(ServerSession session, ServerChannel channel, ServerMessage message) { } }); } }
接口定义
public interface SubscriptionListener extends BayeuxServerListener { /** * <p>Callback invoked when a {@link ServerSession} subscribes to a {@link ServerChannel}.</p> * <源码处>订阅指定渠道 * @param session the session that subscribes * @param channel the channel to subscribe to * @param message the subscription message sent by the client, or null in case of * server-side subscription via {@link ServerChannel#subscribe(ServerSession)} */ public default void subscribed(ServerSession session, ServerChannel channel, ServerMessage message) { } /** * <p>Callback invoked when a {@link ServerSession} unsubscribes from a {@link ServerChannel}.</p> * <源码处>取消订阅指定渠道 * @param session the session that unsubscribes * @param channel the channel to unsubscribe from * @param message the unsubscription message sent by the client, or null in case of * server-side unsubscription via {@link ServerChannel#unsubscribe(ServerSession)} */ public default void unsubscribed(ServerSession session, ServerChannel channel, ServerMessage message) { } }