22
33import java .util .HashMap ;
44import java .util .HashSet ;
5- import java .util .LinkedHashMap ;
65import java .util .Map ;
76import java .util .Set ;
87
1110import com .google .gson .GsonBuilder ;
1211import com .pusher .client .channel .*;
1312import com .pusher .client .channel .impl .message .SubscribeMessage ;
13+ import com .pusher .client .channel .impl .message .SubscriptionCountData ;
1414import com .pusher .client .channel .impl .message .UnsubscribeMessage ;
1515import com .pusher .client .util .Factory ;
1616
1717public abstract class BaseChannel implements InternalChannel {
1818 protected final Gson GSON ;
1919 private static final String INTERNAL_EVENT_PREFIX = "pusher_internal:" ;
2020 protected static final String SUBSCRIPTION_SUCCESS_EVENT = "pusher_internal:subscription_succeeded" ;
21+ protected static final String SUBSCRIPTION_COUNT_EVENT = "pusher_internal:subscription_count" ;
22+ protected static final String PUBLIC_SUBSCRIPTION_COUNT_EVENT = "pusher:subscription_count" ;
2123 private Set <SubscriptionEventListener > globalListeners = new HashSet <SubscriptionEventListener >();
2224 private final Map <String , Set <SubscriptionEventListener >> eventNameToListenerMap = new HashMap <String , Set <SubscriptionEventListener >>();
2325 protected volatile ChannelState state = ChannelState .INITIAL ;
2426 private ChannelEventListener eventListener ;
2527 private final Factory factory ;
2628 private final Object lock = new Object ();
29+ private Integer subscriptionCount ;
2730
2831 public BaseChannel (final Factory factory ) {
2932 GsonBuilder gsonBuilder = new GsonBuilder ();
@@ -37,6 +40,11 @@ public BaseChannel(final Factory factory) {
3740 @ Override
3841 abstract public String getName ();
3942
43+ @ Override
44+ public Integer getCount () {
45+ return subscriptionCount ;
46+ }
47+
4048 @ Override
4149 public void bind (final String eventName , final SubscriptionEventListener listener ) {
4250 validateArguments (eventName , listener );
@@ -112,6 +120,8 @@ public PusherEvent prepareEvent(String event, String message) {
112120 public void onMessage (String event , String message ) {
113121 if (event .equals (SUBSCRIPTION_SUCCESS_EVENT )) {
114122 updateState (ChannelState .SUBSCRIBED );
123+ }else if (event .equals (SUBSCRIPTION_COUNT_EVENT )) {
124+ handleSubscriptionCountEvent (message );
115125 } else {
116126 final Set <SubscriptionEventListener > listeners = getInterestedListeners (event );
117127 if (listeners != null ) {
@@ -184,6 +194,12 @@ private void validateArguments(final String eventName, final SubscriptionEventLi
184194 }
185195 }
186196
197+ private void handleSubscriptionCountEvent (final String message ) {
198+ final SubscriptionCountData subscriptionCountMessage = GSON .fromJson (message , SubscriptionCountData .class );
199+ subscriptionCount = subscriptionCountMessage .getCount ();
200+ onMessage (PUBLIC_SUBSCRIPTION_COUNT_EVENT , message );
201+ }
202+
187203 protected Set <SubscriptionEventListener > getInterestedListeners (String event ) {
188204 synchronized (lock ) {
189205 Set <SubscriptionEventListener > listeners = new HashSet <SubscriptionEventListener >();
0 commit comments