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" ;
2122 private Set <SubscriptionEventListener > globalListeners = new HashSet <SubscriptionEventListener >();
2223 private final Map <String , Set <SubscriptionEventListener >> eventNameToListenerMap = new HashMap <String , Set <SubscriptionEventListener >>();
2324 protected volatile ChannelState state = ChannelState .INITIAL ;
2425 private ChannelEventListener eventListener ;
2526 private final Factory factory ;
2627 private final Object lock = new Object ();
28+ private Integer subscriptionCount ;
2729
2830 public BaseChannel (final Factory factory ) {
2931 GsonBuilder gsonBuilder = new GsonBuilder ();
@@ -37,6 +39,11 @@ public BaseChannel(final Factory factory) {
3739 @ Override
3840 abstract public String getName ();
3941
42+ @ Override
43+ public Integer getCount () {
44+ return subscriptionCount ;
45+ }
46+
4047 @ Override
4148 public void bind (final String eventName , final SubscriptionEventListener listener ) {
4249 validateArguments (eventName , listener );
@@ -112,6 +119,8 @@ public PusherEvent prepareEvent(String event, String message) {
112119 public void onMessage (String event , String message ) {
113120 if (event .equals (SUBSCRIPTION_SUCCESS_EVENT )) {
114121 updateState (ChannelState .SUBSCRIBED );
122+ }else if (event .equals (SUBSCRIPTION_COUNT_EVENT )) {
123+ handleSubscriptionCountEvent (message );
115124 } else {
116125 final Set <SubscriptionEventListener > listeners = getInterestedListeners (event );
117126 if (listeners != null ) {
@@ -184,6 +193,20 @@ private void validateArguments(final String eventName, final SubscriptionEventLi
184193 }
185194 }
186195
196+ private void handleSubscriptionCountEvent (final String message ) {
197+ String channelName = this .getName ();
198+ final SubscriptionCountData subscriptionCountMessage = GSON .fromJson (message , SubscriptionCountData .class );
199+ subscriptionCount = subscriptionCountMessage .getCount ();
200+ if (eventListener != null ) {
201+ factory .queueOnEventThread (new Runnable () {
202+ @ Override
203+ public void run () {
204+ eventListener .onSubscriptionCountChanged (channelName , subscriptionCountMessage .getCount ());
205+ }
206+ });
207+ }
208+ }
209+
187210 protected Set <SubscriptionEventListener > getInterestedListeners (String event ) {
188211 synchronized (lock ) {
189212 Set <SubscriptionEventListener > listeners = new HashSet <SubscriptionEventListener >();
0 commit comments