From 75b958b9c63bd4eaa3e969b78322affb215d395b Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Sun, 26 Jun 2022 15:08:48 +0530 Subject: [PATCH 01/14] Event listner --- .../client/channel/ChannelEventListener.java | 15 +++++++++++++++ .../impl/message/SubscriptionCountData.java | 12 ++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/main/java/com/pusher/client/channel/impl/message/SubscriptionCountData.java diff --git a/src/main/java/com/pusher/client/channel/ChannelEventListener.java b/src/main/java/com/pusher/client/channel/ChannelEventListener.java index efd5bda1..79a7cd0c 100644 --- a/src/main/java/com/pusher/client/channel/ChannelEventListener.java +++ b/src/main/java/com/pusher/client/channel/ChannelEventListener.java @@ -40,4 +40,19 @@ public interface ChannelEventListener extends SubscriptionEventListener { * The name of the channel that was successfully subscribed. */ void onSubscriptionSucceeded(String channelName); + + /** + *

+ * Callback fired when a subscription success count + * of a channel changes + *

+ * + * + * @param channelName + * The name of the channel for which the count changed. + * + * @param count + * The latest count of subscribers on the channel. + */ + void onSubscriptionCountChange(String channelName, int count); } diff --git a/src/main/java/com/pusher/client/channel/impl/message/SubscriptionCountData.java b/src/main/java/com/pusher/client/channel/impl/message/SubscriptionCountData.java new file mode 100644 index 00000000..b5865e0b --- /dev/null +++ b/src/main/java/com/pusher/client/channel/impl/message/SubscriptionCountData.java @@ -0,0 +1,12 @@ +package com.pusher.client.channel.impl.message; + +import com.google.gson.annotations.SerializedName; + +public class SubscriptionCountData { + @SerializedName("subscription_count") + public Integer count; + + public int getCount(){ + return count; + } +} From 9aaa239e86416573aa5ce3d5189ff43e6545899e Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Tue, 28 Jun 2022 11:37:37 +0530 Subject: [PATCH 02/14] Update readme --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index d6c2b0b8..133ecee0 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,20 @@ Channel channel = pusher.subscribe("my-channel", new ChannelEventListener() { // Other ChannelEventListener methods }); ``` +If you wish to be informed for subscription count events, pass an implementation of the `ChannelEventListener` interface: + +```java +Channel channel = pusher.subscribe("my-channel", new ChannelEventListener() { + @Override + public void onSubscriptionCountChange(String channelName, int count) { + System.out.println("Members in channel: " + channelName + " are: " + count); + } + + // Other ChannelEventListener methods +}); + +``` +To know more about how to enbale subscription count events visit [here](https://www.pusher.com) ### Private channels From f97b594071295161b3468f08c4761f06d951f64a Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Tue, 28 Jun 2022 12:06:18 +0530 Subject: [PATCH 03/14] Update ecample apps --- src/main/java/com/pusher/client/example/ExampleApp.java | 6 ++++++ .../pusher/client/example/PresenceChannelExampleApp.java | 6 ++++++ .../com/pusher/client/example/PrivateChannelExampleApp.java | 6 ++++++ .../client/example/PrivateEncryptedChannelExampleApp.java | 6 ++++++ 4 files changed, 24 insertions(+) diff --git a/src/main/java/com/pusher/client/example/ExampleApp.java b/src/main/java/com/pusher/client/example/ExampleApp.java index feaaf097..d465bd7b 100644 --- a/src/main/java/com/pusher/client/example/ExampleApp.java +++ b/src/main/java/com/pusher/client/example/ExampleApp.java @@ -78,6 +78,12 @@ public void onEvent(PusherEvent event) { System.out.println(String.format( "Received event [%s]", event.toString())); } + + @Override + public void onSubscriptionCountChange(String channelName, int count) { + System.out.println(String.format( + "Count for channel [%s], changed to [%d]", channelName, count)); + } }; // subscribe to the channel and with the event listener for the event name diff --git a/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java b/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java index 45da681d..1051a78a 100644 --- a/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java +++ b/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java @@ -115,6 +115,12 @@ public void userUnsubscribed(String channelName, User user) { System.out.println(String.format("A user has left channel [%s]: %s", channelName, user)); printCurrentlySubscribedUsers(); } + + @Override + public void onSubscriptionCountChange(String channelName, int count) { + System.out.println(String.format( + "Count for channel [%s], changed to [%d]", channelName, count)); + } }; // subscribe to the channel and with the event listener for the event name diff --git a/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java b/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java index 46d9da76..2f5846c8 100644 --- a/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java +++ b/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java @@ -94,6 +94,12 @@ public void onAuthenticationFailure(String message, Exception e) { System.out.println(String.format( "Authorization failure due to [%s], exception was [%s]", message, e)); } + + @Override + public void onSubscriptionCountChange(String channelName, int count) { + System.out.println(String.format( + "Count for channel [%s], changed to [%d]", channelName, count)); + } }; // subscribe to the channel and with the event listener for the event name diff --git a/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java b/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java index 5a0242b9..25ecb653 100644 --- a/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java +++ b/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java @@ -106,6 +106,12 @@ public void onDecryptionFailure(String event, String reason) { System.out.println(String.format( "An error was received decrypting message for event:[%s] - reason: [%s]", event, reason)); } + + @Override + public void onSubscriptionCountChange(String channelName, int count) { + System.out.println(String.format( + "Count for channel [%s], changed to [%d]", channelName, count)); + } }; // subscribe to the channel and with the event listener for the event name From 4b8d7f5a1695908534e4ea7cc5e5812944ec1eea Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Tue, 28 Jun 2022 12:09:25 +0530 Subject: [PATCH 04/14] Change listner name --- README.md | 2 +- .../client/channel/ChannelEventListener.java | 2 +- .../client/channel/impl/ChannelImpl.java | 151 ++++++++++++++++++ .../com/pusher/client/example/ExampleApp.java | 2 +- .../example/PresenceChannelExampleApp.java | 2 +- .../example/PrivateChannelExampleApp.java | 2 +- .../PrivateEncryptedChannelExampleApp.java | 2 +- 7 files changed, 157 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 133ecee0..cd46483d 100644 --- a/README.md +++ b/README.md @@ -262,7 +262,7 @@ If you wish to be informed for subscription count events, pass an implementation ```java Channel channel = pusher.subscribe("my-channel", new ChannelEventListener() { @Override - public void onSubscriptionCountChange(String channelName, int count) { + public void onSubscriptionCountChanged(String channelName, int count) { System.out.println("Members in channel: " + channelName + " are: " + count); } diff --git a/src/main/java/com/pusher/client/channel/ChannelEventListener.java b/src/main/java/com/pusher/client/channel/ChannelEventListener.java index 79a7cd0c..f9002a3e 100644 --- a/src/main/java/com/pusher/client/channel/ChannelEventListener.java +++ b/src/main/java/com/pusher/client/channel/ChannelEventListener.java @@ -54,5 +54,5 @@ public interface ChannelEventListener extends SubscriptionEventListener { * @param count * The latest count of subscribers on the channel. */ - void onSubscriptionCountChange(String channelName, int count); + void onSubscriptionCountChanged(String channelName, int count); } diff --git a/src/main/java/com/pusher/client/channel/impl/ChannelImpl.java b/src/main/java/com/pusher/client/channel/impl/ChannelImpl.java index 32839cea..82a88a61 100644 --- a/src/main/java/com/pusher/client/channel/impl/ChannelImpl.java +++ b/src/main/java/com/pusher/client/channel/impl/ChannelImpl.java @@ -27,6 +27,157 @@ public ChannelImpl(final String channelName, final Factory factory) { public String getName() { return name; } +<<<<<<< HEAD +======= + + @Override + public void bind(final String eventName, final SubscriptionEventListener listener) { + + validateArguments(eventName, listener); + + synchronized (lock) { + Set listeners = eventNameToListenerMap.get(eventName); + if (listeners == null) { + listeners = new HashSet(); + eventNameToListenerMap.put(eventName, listeners); + } + listeners.add(listener); + } + } + + @Override + public void bindGlobal(SubscriptionEventListener listener) { + validateArguments("", listener); + + synchronized(lock) { + globalListeners.add(listener); + } + } + + @Override + public void unbind(final String eventName, final SubscriptionEventListener listener) { + + validateArguments(eventName, listener); + + synchronized (lock) { + final Set listeners = eventNameToListenerMap.get(eventName); + if (listeners != null) { + listeners.remove(listener); + if (listeners.isEmpty()) { + eventNameToListenerMap.remove(eventName); + } + } + } + } + + @Override + public void unbindGlobal(SubscriptionEventListener listener) { + validateArguments("", listener); + + synchronized(lock) { + if (globalListeners != null) { + globalListeners.remove(listener); + } + } + } + + @Override + public boolean isSubscribed() { + return state == ChannelState.SUBSCRIBED; + } + + /* InternalChannel implementation */ + + @Override + public PusherEvent prepareEvent(String event, String message) { + return GSON.fromJson(message, PusherEvent.class); + } + + @Override + public void onMessage(final String event, final String message) { + + if (event.equals(SUBSCRIPTION_SUCCESS_EVENT)) { + updateState(ChannelState.SUBSCRIBED); + } else if (event.equals(SUBSCRIPTION_COUNT_EVENT)) { + handleSubscriptionCountEvent(message); + } else { + final Set listeners = getInterestedListeners(event); + if (listeners != null) { + final PusherEvent pusherEvent = prepareEvent(event, message); + if (pusherEvent != null) { + for (final SubscriptionEventListener listener : listeners) { + factory.queueOnEventThread(new Runnable() { + @Override + public void run() { + listener.onEvent(pusherEvent); + } + }); + } + } + } + } + } + + + @Override + public String toSubscribeMessage() { + return GSON.toJson(new SubscribeMessage(name)); + } + + @Override + public String toUnsubscribeMessage() { + return GSON.toJson( + new UnsubscribeMessage(name)); + } + + @Override + public void updateState(final ChannelState state) { + + this.state = state; + + if (state == ChannelState.SUBSCRIBED && eventListener != null) { + factory.queueOnEventThread(new Runnable() { + @Override + public void run() { + eventListener.onSubscriptionSucceeded(ChannelImpl.this.getName()); + } + }); + } + } + + private void handleSubscriptionCountEvent(final String message) { + String channelName = this.getName(); + final SubscriptionCountData subscriptionCountMessage = GSON.fromJson(message, SubscriptionCountData.class); + if (eventListener != null ) { + factory.queueOnEventThread(new Runnable() { + @Override + public void run() { + eventListener.onSubscriptionCountChanged(channelName, subscriptionCountMessage.getCount()); + } + }); + } + } + + /* Comparable implementation */ + + @Override + public void setEventListener(final ChannelEventListener listener) { + eventListener = listener; + } + + @Override + public ChannelEventListener getEventListener() { + return eventListener; + } + + @Override + public int compareTo(final InternalChannel other) { + return getName().compareTo(other.getName()); + } + + /* implementation detail */ + +>>>>>>> ae4c039 (Change listner name) @Override public String toString() { return String.format("[Public Channel: name=%s]", name); diff --git a/src/main/java/com/pusher/client/example/ExampleApp.java b/src/main/java/com/pusher/client/example/ExampleApp.java index d465bd7b..0a8c7166 100644 --- a/src/main/java/com/pusher/client/example/ExampleApp.java +++ b/src/main/java/com/pusher/client/example/ExampleApp.java @@ -80,7 +80,7 @@ public void onEvent(PusherEvent event) { } @Override - public void onSubscriptionCountChange(String channelName, int count) { + public void onSubscriptionCountChanged(String channelName, int count) { System.out.println(String.format( "Count for channel [%s], changed to [%d]", channelName, count)); } diff --git a/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java b/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java index 1051a78a..66c5848d 100644 --- a/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java +++ b/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java @@ -117,7 +117,7 @@ public void userUnsubscribed(String channelName, User user) { } @Override - public void onSubscriptionCountChange(String channelName, int count) { + public void onSubscriptionCountChanged(String channelName, int count) { System.out.println(String.format( "Count for channel [%s], changed to [%d]", channelName, count)); } diff --git a/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java b/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java index 2f5846c8..4a6add20 100644 --- a/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java +++ b/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java @@ -96,7 +96,7 @@ public void onAuthenticationFailure(String message, Exception e) { } @Override - public void onSubscriptionCountChange(String channelName, int count) { + public void onSubscriptionCountChanged(String channelName, int count) { System.out.println(String.format( "Count for channel [%s], changed to [%d]", channelName, count)); } diff --git a/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java b/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java index 25ecb653..54133baa 100644 --- a/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java +++ b/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java @@ -108,7 +108,7 @@ public void onDecryptionFailure(String event, String reason) { } @Override - public void onSubscriptionCountChange(String channelName, int count) { + public void onSubscriptionCountChanged(String channelName, int count) { System.out.println(String.format( "Count for channel [%s], changed to [%d]", channelName, count)); } From 4014c9cc4e7f974fc550769d5c3e6a94b932396d Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Wed, 29 Jun 2022 10:55:49 +0530 Subject: [PATCH 05/14] Store count in a variable and expose through a public method --- .../com/pusher/client/channel/Channel.java | 7 + .../client/channel/impl/ChannelImpl.java | 151 ------------------ 2 files changed, 7 insertions(+), 151 deletions(-) diff --git a/src/main/java/com/pusher/client/channel/Channel.java b/src/main/java/com/pusher/client/channel/Channel.java index 90289bed..6915b9c4 100644 --- a/src/main/java/com/pusher/client/channel/Channel.java +++ b/src/main/java/com/pusher/client/channel/Channel.java @@ -17,6 +17,13 @@ public interface Channel { */ String getName(); + /** + * Gets the current subscription count of the subscribed channel. + * + * @return Count of subscriptions + */ + int getCount(); + /** * Binds a {@link SubscriptionEventListener} to an event. The * {@link SubscriptionEventListener} will be notified whenever the specified diff --git a/src/main/java/com/pusher/client/channel/impl/ChannelImpl.java b/src/main/java/com/pusher/client/channel/impl/ChannelImpl.java index 82a88a61..32839cea 100644 --- a/src/main/java/com/pusher/client/channel/impl/ChannelImpl.java +++ b/src/main/java/com/pusher/client/channel/impl/ChannelImpl.java @@ -27,157 +27,6 @@ public ChannelImpl(final String channelName, final Factory factory) { public String getName() { return name; } -<<<<<<< HEAD -======= - - @Override - public void bind(final String eventName, final SubscriptionEventListener listener) { - - validateArguments(eventName, listener); - - synchronized (lock) { - Set listeners = eventNameToListenerMap.get(eventName); - if (listeners == null) { - listeners = new HashSet(); - eventNameToListenerMap.put(eventName, listeners); - } - listeners.add(listener); - } - } - - @Override - public void bindGlobal(SubscriptionEventListener listener) { - validateArguments("", listener); - - synchronized(lock) { - globalListeners.add(listener); - } - } - - @Override - public void unbind(final String eventName, final SubscriptionEventListener listener) { - - validateArguments(eventName, listener); - - synchronized (lock) { - final Set listeners = eventNameToListenerMap.get(eventName); - if (listeners != null) { - listeners.remove(listener); - if (listeners.isEmpty()) { - eventNameToListenerMap.remove(eventName); - } - } - } - } - - @Override - public void unbindGlobal(SubscriptionEventListener listener) { - validateArguments("", listener); - - synchronized(lock) { - if (globalListeners != null) { - globalListeners.remove(listener); - } - } - } - - @Override - public boolean isSubscribed() { - return state == ChannelState.SUBSCRIBED; - } - - /* InternalChannel implementation */ - - @Override - public PusherEvent prepareEvent(String event, String message) { - return GSON.fromJson(message, PusherEvent.class); - } - - @Override - public void onMessage(final String event, final String message) { - - if (event.equals(SUBSCRIPTION_SUCCESS_EVENT)) { - updateState(ChannelState.SUBSCRIBED); - } else if (event.equals(SUBSCRIPTION_COUNT_EVENT)) { - handleSubscriptionCountEvent(message); - } else { - final Set listeners = getInterestedListeners(event); - if (listeners != null) { - final PusherEvent pusherEvent = prepareEvent(event, message); - if (pusherEvent != null) { - for (final SubscriptionEventListener listener : listeners) { - factory.queueOnEventThread(new Runnable() { - @Override - public void run() { - listener.onEvent(pusherEvent); - } - }); - } - } - } - } - } - - - @Override - public String toSubscribeMessage() { - return GSON.toJson(new SubscribeMessage(name)); - } - - @Override - public String toUnsubscribeMessage() { - return GSON.toJson( - new UnsubscribeMessage(name)); - } - - @Override - public void updateState(final ChannelState state) { - - this.state = state; - - if (state == ChannelState.SUBSCRIBED && eventListener != null) { - factory.queueOnEventThread(new Runnable() { - @Override - public void run() { - eventListener.onSubscriptionSucceeded(ChannelImpl.this.getName()); - } - }); - } - } - - private void handleSubscriptionCountEvent(final String message) { - String channelName = this.getName(); - final SubscriptionCountData subscriptionCountMessage = GSON.fromJson(message, SubscriptionCountData.class); - if (eventListener != null ) { - factory.queueOnEventThread(new Runnable() { - @Override - public void run() { - eventListener.onSubscriptionCountChanged(channelName, subscriptionCountMessage.getCount()); - } - }); - } - } - - /* Comparable implementation */ - - @Override - public void setEventListener(final ChannelEventListener listener) { - eventListener = listener; - } - - @Override - public ChannelEventListener getEventListener() { - return eventListener; - } - - @Override - public int compareTo(final InternalChannel other) { - return getName().compareTo(other.getName()); - } - - /* implementation detail */ - ->>>>>>> ae4c039 (Change listner name) @Override public String toString() { return String.format("[Public Channel: name=%s]", name); From 95d8d3e9b21e94bebd08a472c67edab609b63b4c Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Thu, 30 Jun 2022 12:40:36 +0530 Subject: [PATCH 06/14] Integer class to handle null situation --- src/main/java/com/pusher/client/channel/Channel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/pusher/client/channel/Channel.java b/src/main/java/com/pusher/client/channel/Channel.java index 6915b9c4..c869a4de 100644 --- a/src/main/java/com/pusher/client/channel/Channel.java +++ b/src/main/java/com/pusher/client/channel/Channel.java @@ -22,7 +22,7 @@ public interface Channel { * * @return Count of subscriptions */ - int getCount(); + Integer getCount(); /** * Binds a {@link SubscriptionEventListener} to an event. The From 61fae0454c2b17ee48fe2f74f696062eac930c2b Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Fri, 15 Jul 2022 11:20:03 +0200 Subject: [PATCH 07/14] CHANGELOG update --- CHANGELOG.md | 3 +++ README.md | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68ab3e47..e13e90c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # pusher-websocket-java changelog +### Version 2.3.1 - 15th July 2022 +* Add support for Subscription count events + ### Version 2.3.0 - 4th July 2022 * Added support for user sign in and server to user messages * Fixed issue with calling disconnect while the client is attempting reconnection diff --git a/README.md b/README.md index cd46483d..02adc5ef 100644 --- a/README.md +++ b/README.md @@ -270,7 +270,6 @@ Channel channel = pusher.subscribe("my-channel", new ChannelEventListener() { }); ``` -To know more about how to enbale subscription count events visit [here](https://www.pusher.com) ### Private channels From 6842c0986555e2cc55b30e3d39e8a9d65e06dab2 Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Fri, 15 Jul 2022 12:26:20 +0200 Subject: [PATCH 08/14] Refactor code --- .../com/pusher/client/channel/Channel.java | 7 ------ .../client/channel/impl/BaseChannel.java | 25 ++++++++++++++++++- .../client/channel/impl/InternalChannel.java | 2 ++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/pusher/client/channel/Channel.java b/src/main/java/com/pusher/client/channel/Channel.java index c869a4de..90289bed 100644 --- a/src/main/java/com/pusher/client/channel/Channel.java +++ b/src/main/java/com/pusher/client/channel/Channel.java @@ -17,13 +17,6 @@ public interface Channel { */ String getName(); - /** - * Gets the current subscription count of the subscribed channel. - * - * @return Count of subscriptions - */ - Integer getCount(); - /** * Binds a {@link SubscriptionEventListener} to an event. The * {@link SubscriptionEventListener} will be notified whenever the specified diff --git a/src/main/java/com/pusher/client/channel/impl/BaseChannel.java b/src/main/java/com/pusher/client/channel/impl/BaseChannel.java index 07602791..8c2a4b74 100644 --- a/src/main/java/com/pusher/client/channel/impl/BaseChannel.java +++ b/src/main/java/com/pusher/client/channel/impl/BaseChannel.java @@ -2,7 +2,6 @@ import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; @@ -11,6 +10,7 @@ import com.google.gson.GsonBuilder; import com.pusher.client.channel.*; import com.pusher.client.channel.impl.message.SubscribeMessage; +import com.pusher.client.channel.impl.message.SubscriptionCountData; import com.pusher.client.channel.impl.message.UnsubscribeMessage; import com.pusher.client.util.Factory; @@ -18,12 +18,14 @@ public abstract class BaseChannel implements InternalChannel { protected final Gson GSON; private static final String INTERNAL_EVENT_PREFIX = "pusher_internal:"; protected static final String SUBSCRIPTION_SUCCESS_EVENT = "pusher_internal:subscription_succeeded"; + protected static final String SUBSCRIPTION_COUNT_EVENT = "pusher_internal:subscription_count"; private Set globalListeners = new HashSet(); private final Map> eventNameToListenerMap = new HashMap>(); protected volatile ChannelState state = ChannelState.INITIAL; private ChannelEventListener eventListener; private final Factory factory; private final Object lock = new Object(); + private Integer subscriptionCount; public BaseChannel(final Factory factory) { GsonBuilder gsonBuilder = new GsonBuilder(); @@ -37,6 +39,11 @@ public BaseChannel(final Factory factory) { @Override abstract public String getName(); + @Override + public Integer getCount() { + return subscriptionCount; + } + @Override public void bind(final String eventName, final SubscriptionEventListener listener) { validateArguments(eventName, listener); @@ -112,6 +119,8 @@ public PusherEvent prepareEvent(String event, String message) { public void onMessage(String event, String message) { if (event.equals(SUBSCRIPTION_SUCCESS_EVENT)) { updateState(ChannelState.SUBSCRIBED); + }else if (event.equals(SUBSCRIPTION_COUNT_EVENT)) { + handleSubscriptionCountEvent(message); } else { final Set listeners = getInterestedListeners(event); if (listeners != null) { @@ -184,6 +193,20 @@ private void validateArguments(final String eventName, final SubscriptionEventLi } } + private void handleSubscriptionCountEvent(final String message) { + String channelName = this.getName(); + final SubscriptionCountData subscriptionCountMessage = GSON.fromJson(message, SubscriptionCountData.class); + subscriptionCount = subscriptionCountMessage.getCount(); + if (eventListener != null ) { + factory.queueOnEventThread(new Runnable() { + @Override + public void run() { + eventListener.onSubscriptionCountChanged(channelName, subscriptionCountMessage.getCount()); + } + }); + } + } + protected Set getInterestedListeners(String event) { synchronized (lock) { Set listeners = new HashSet(); diff --git a/src/main/java/com/pusher/client/channel/impl/InternalChannel.java b/src/main/java/com/pusher/client/channel/impl/InternalChannel.java index b6d8e032..8d7116b4 100644 --- a/src/main/java/com/pusher/client/channel/impl/InternalChannel.java +++ b/src/main/java/com/pusher/client/channel/impl/InternalChannel.java @@ -11,6 +11,8 @@ public interface InternalChannel extends Channel, Comparable { String toUnsubscribeMessage(); + Integer getCount(); + PusherEvent prepareEvent(String event, String message); void onMessage(String event, String message); From 1fe2479ab80433c81a4f87b07f1e7ca7acb5aff8 Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Fri, 15 Jul 2022 12:28:53 +0200 Subject: [PATCH 09/14] getName from current class --- src/main/java/com/pusher/client/channel/impl/BaseChannel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/pusher/client/channel/impl/BaseChannel.java b/src/main/java/com/pusher/client/channel/impl/BaseChannel.java index 8c2a4b74..24cf8ff0 100644 --- a/src/main/java/com/pusher/client/channel/impl/BaseChannel.java +++ b/src/main/java/com/pusher/client/channel/impl/BaseChannel.java @@ -194,7 +194,7 @@ private void validateArguments(final String eventName, final SubscriptionEventLi } private void handleSubscriptionCountEvent(final String message) { - String channelName = this.getName(); + String channelName = getName(); final SubscriptionCountData subscriptionCountMessage = GSON.fromJson(message, SubscriptionCountData.class); subscriptionCount = subscriptionCountMessage.getCount(); if (eventListener != null ) { From 1b8b6b41f0d9e73647fd641d30c7f166643d3090 Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Fri, 15 Jul 2022 12:39:07 +0200 Subject: [PATCH 10/14] Update Readme and build.gradle --- README.md | 4 ++-- build.gradle | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 02adc5ef..67cd4dd0 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ The pusher-java-client is available in Maven Central. com.pusher pusher-java-client - 2.3.0-beta + 2.3.1 ``` @@ -83,7 +83,7 @@ The pusher-java-client is available in Maven Central. ```groovy dependencies { - compile 'com.pusher:pusher-java-client:2.3.0-beta' + compile 'com.pusher:pusher-java-client:2.3.1' } ``` diff --git a/build.gradle b/build.gradle index c86ef0d1..8966a863 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,7 @@ def getProperty = { property -> } group = "com.pusher" -version = "2.3.0-beta" +version = "2.3.1" sourceCompatibility = "1.8" targetCompatibility = "1.8" From 80782a6c69e7295a194bde23562fd2c413f1dc96 Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Fri, 15 Jul 2022 14:06:07 +0200 Subject: [PATCH 11/14] Update version --- CHANGELOG.md | 2 +- README.md | 2 +- build.gradle | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e13e90c2..55738737 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # pusher-websocket-java changelog -### Version 2.3.1 - 15th July 2022 +### Version 2.4 - 15th July 2022 * Add support for Subscription count events ### Version 2.3.0 - 4th July 2022 diff --git a/README.md b/README.md index 67cd4dd0..b1d9ff24 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ The pusher-java-client is available in Maven Central. com.pusher pusher-java-client - 2.3.1 + 2.4 ``` diff --git a/build.gradle b/build.gradle index 8966a863..5975697f 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,7 @@ def getProperty = { property -> } group = "com.pusher" -version = "2.3.1" +version = "2.4" sourceCompatibility = "1.8" targetCompatibility = "1.8" From 5b2f9e37cc2f33563ed4d30d98839dda9d14842b Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Fri, 15 Jul 2022 14:09:56 +0200 Subject: [PATCH 12/14] Update version --- CHANGELOG.md | 2 +- README.md | 2 +- build.gradle | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55738737..0976e367 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # pusher-websocket-java changelog -### Version 2.4 - 15th July 2022 +### Version 2.4.0 - 15th July 2022 * Add support for Subscription count events ### Version 2.3.0 - 4th July 2022 diff --git a/README.md b/README.md index b1d9ff24..4c1bcea1 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ The pusher-java-client is available in Maven Central. com.pusher pusher-java-client - 2.4 + 2.4.0 ``` diff --git a/build.gradle b/build.gradle index 5975697f..55445088 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,7 @@ def getProperty = { property -> } group = "com.pusher" -version = "2.4" +version = "2.4.0" sourceCompatibility = "1.8" targetCompatibility = "1.8" From 2f42899084e35d6832eea47f24771268f54db64a Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Fri, 15 Jul 2022 16:08:15 +0200 Subject: [PATCH 13/14] Updated subscription count to be handled by renaming --- README.md | 12 ++++++------ .../client/channel/ChannelEventListener.java | 15 --------------- .../pusher/client/channel/impl/BaseChannel.java | 11 ++--------- .../com/pusher/client/example/ExampleApp.java | 6 ------ .../client/example/PresenceChannelExampleApp.java | 6 ------ .../client/example/PrivateChannelExampleApp.java | 6 ------ .../PrivateEncryptedChannelExampleApp.java | 6 ------ 7 files changed, 8 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 4c1bcea1..4d7777d0 100644 --- a/README.md +++ b/README.md @@ -257,16 +257,16 @@ Channel channel = pusher.subscribe("my-channel", new ChannelEventListener() { // Other ChannelEventListener methods }); ``` -If you wish to be informed for subscription count events, pass an implementation of the `ChannelEventListener` interface: +If you wish to be informed for subscription count events, use the `bind` function to listen to event type `pusher:subscription_count`: ```java -Channel channel = pusher.subscribe("my-channel", new ChannelEventListener() { +Channel channel = pusher.subscribe("my-channel"); +channel.bind("pusher:subscription_count", new SubscriptionEventListener() { @Override - public void onSubscriptionCountChanged(String channelName, int count) { - System.out.println("Members in channel: " + channelName + " are: " + count); + public void onEvent(PusherEvent event) { + System.out.println("Received event with data: " + event.toString()); + System.out.println("Subscription Count is: " + channel.getCount()); } - - // Other ChannelEventListener methods }); ``` diff --git a/src/main/java/com/pusher/client/channel/ChannelEventListener.java b/src/main/java/com/pusher/client/channel/ChannelEventListener.java index f9002a3e..efd5bda1 100644 --- a/src/main/java/com/pusher/client/channel/ChannelEventListener.java +++ b/src/main/java/com/pusher/client/channel/ChannelEventListener.java @@ -40,19 +40,4 @@ public interface ChannelEventListener extends SubscriptionEventListener { * The name of the channel that was successfully subscribed. */ void onSubscriptionSucceeded(String channelName); - - /** - *

- * Callback fired when a subscription success count - * of a channel changes - *

- * - * - * @param channelName - * The name of the channel for which the count changed. - * - * @param count - * The latest count of subscribers on the channel. - */ - void onSubscriptionCountChanged(String channelName, int count); } diff --git a/src/main/java/com/pusher/client/channel/impl/BaseChannel.java b/src/main/java/com/pusher/client/channel/impl/BaseChannel.java index 24cf8ff0..12f8de32 100644 --- a/src/main/java/com/pusher/client/channel/impl/BaseChannel.java +++ b/src/main/java/com/pusher/client/channel/impl/BaseChannel.java @@ -19,6 +19,7 @@ public abstract class BaseChannel implements InternalChannel { private static final String INTERNAL_EVENT_PREFIX = "pusher_internal:"; protected static final String SUBSCRIPTION_SUCCESS_EVENT = "pusher_internal:subscription_succeeded"; protected static final String SUBSCRIPTION_COUNT_EVENT = "pusher_internal:subscription_count"; + protected static final String PUBLIC_SUBSCRIPTION_COUNT_EVENT = "pusher:subscription_count"; private Set globalListeners = new HashSet(); private final Map> eventNameToListenerMap = new HashMap>(); protected volatile ChannelState state = ChannelState.INITIAL; @@ -194,17 +195,9 @@ private void validateArguments(final String eventName, final SubscriptionEventLi } private void handleSubscriptionCountEvent(final String message) { - String channelName = getName(); final SubscriptionCountData subscriptionCountMessage = GSON.fromJson(message, SubscriptionCountData.class); subscriptionCount = subscriptionCountMessage.getCount(); - if (eventListener != null ) { - factory.queueOnEventThread(new Runnable() { - @Override - public void run() { - eventListener.onSubscriptionCountChanged(channelName, subscriptionCountMessage.getCount()); - } - }); - } + onMessage(PUBLIC_SUBSCRIPTION_COUNT_EVENT, message); } protected Set getInterestedListeners(String event) { diff --git a/src/main/java/com/pusher/client/example/ExampleApp.java b/src/main/java/com/pusher/client/example/ExampleApp.java index 0a8c7166..feaaf097 100644 --- a/src/main/java/com/pusher/client/example/ExampleApp.java +++ b/src/main/java/com/pusher/client/example/ExampleApp.java @@ -78,12 +78,6 @@ public void onEvent(PusherEvent event) { System.out.println(String.format( "Received event [%s]", event.toString())); } - - @Override - public void onSubscriptionCountChanged(String channelName, int count) { - System.out.println(String.format( - "Count for channel [%s], changed to [%d]", channelName, count)); - } }; // subscribe to the channel and with the event listener for the event name diff --git a/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java b/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java index 66c5848d..45da681d 100644 --- a/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java +++ b/src/main/java/com/pusher/client/example/PresenceChannelExampleApp.java @@ -115,12 +115,6 @@ public void userUnsubscribed(String channelName, User user) { System.out.println(String.format("A user has left channel [%s]: %s", channelName, user)); printCurrentlySubscribedUsers(); } - - @Override - public void onSubscriptionCountChanged(String channelName, int count) { - System.out.println(String.format( - "Count for channel [%s], changed to [%d]", channelName, count)); - } }; // subscribe to the channel and with the event listener for the event name diff --git a/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java b/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java index 4a6add20..46d9da76 100644 --- a/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java +++ b/src/main/java/com/pusher/client/example/PrivateChannelExampleApp.java @@ -94,12 +94,6 @@ public void onAuthenticationFailure(String message, Exception e) { System.out.println(String.format( "Authorization failure due to [%s], exception was [%s]", message, e)); } - - @Override - public void onSubscriptionCountChanged(String channelName, int count) { - System.out.println(String.format( - "Count for channel [%s], changed to [%d]", channelName, count)); - } }; // subscribe to the channel and with the event listener for the event name diff --git a/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java b/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java index 54133baa..5a0242b9 100644 --- a/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java +++ b/src/main/java/com/pusher/client/example/PrivateEncryptedChannelExampleApp.java @@ -106,12 +106,6 @@ public void onDecryptionFailure(String event, String reason) { System.out.println(String.format( "An error was received decrypting message for event:[%s] - reason: [%s]", event, reason)); } - - @Override - public void onSubscriptionCountChanged(String channelName, int count) { - System.out.println(String.format( - "Count for channel [%s], changed to [%d]", channelName, count)); - } }; // subscribe to the channel and with the event listener for the event name From 1db3852df86a29a93ea8b28b6aa0e9d3706534e4 Mon Sep 17 00:00:00 2001 From: Ashmeet Singh Date: Fri, 15 Jul 2022 16:11:17 +0200 Subject: [PATCH 14/14] groovy version --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4d7777d0..7b959212 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ The pusher-java-client is available in Maven Central. ```groovy dependencies { - compile 'com.pusher:pusher-java-client:2.3.1' + compile 'com.pusher:pusher-java-client:2.4.0' } ```