Skip to content

Commit 380c77b

Browse files
authored
Merge pull request #126 from pusher/SubscriptionCountEvent
Addind subscription count event handler
2 parents a721e92 + 8b09cfa commit 380c77b

File tree

10 files changed

+144
-50
lines changed

10 files changed

+144
-50
lines changed

CHANGELOG.md

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
# Changelog
2-
3-
## 2.1.0
1+
# Changelog
2+
3+
## 2.2.0
4+
* [ADDED] Support for subscription_count events
5+
6+
## 2.1.0
47
* [ADDED] Strong name to the PusherClient assembly.
58
* [ADDED] Support for the authentication header on the HttpAuthorizer.
69
* [ADDED] End-to-end encryption for private encrypted channels.
710
* [ADDED] Method Channel.UnsubscribeAsync.
811
* [ADDED] Host to PusherOptions.
912
* [FIXED] The intermittent WebsocketAutoReconnect issue The socket is connecting, cannot connect again!
10-
11-
## 2.0.1
12-
* [FIXED] Filter on event name in event emitter.
13-
14-
## 2.0.0
13+
14+
## 2.0.1
15+
* [FIXED] Filter on event name in event emitter.
16+
17+
## 2.0.0
1518
* [FIXED] Infinite loop when failing to connect for the first time.
1619
* [FIXED] Bug: GenericPresenceChannel<T>'s AddMember and RemoveMember events were not being emitted.
1720
* [FIXED] Change MemberRemovedEventHandler to MemberRemovedEventHandler<T>.

ExampleApplication/Program.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private static async Task InitPusher()
6767
{
6868
_pusher = new Pusher(Config.AppKey, new PusherOptions
6969
{
70-
Authorizer = new HttpAuthorizer("http://localhost:8888/auth/" + HttpUtility.UrlEncode(_name)),
70+
Authorizer = new HttpAuthorizer("http://127.0.0.1:3030/pusher/auth" + HttpUtility.UrlEncode(_name)),
7171
Cluster = Config.Cluster,
7272
Encrypted = Config.Encrypted,
7373
TraceLogger = new TraceLogger(),
@@ -96,6 +96,11 @@ private static async Task InitPusher()
9696
}
9797
};
9898

99+
_pusher.CountHandler += (sender, data) =>
100+
{
101+
Console.WriteLine(data);
102+
};
103+
99104
// Setup private encrypted channel
100105
void GeneralListener(string eventName, PusherEvent eventData)
101106
{

PusherClient.Tests/AcceptanceTests/SubscriptionTest.PublicChannels.cs

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Threading.Tasks;
22
using NUnit.Framework;
33
using PusherClient.Tests.Utilities;
4+
using Newtonsoft.Json;
45

56
namespace PusherClient.Tests.AcceptanceTests
67
{
@@ -51,6 +52,20 @@ public async Task PublicChannelConnectThenSubscribeWithoutAnyEventHandlersAsync(
5152
ValidateSubscribedChannel(pusher, mockChannelName, channel, ChannelTypes.Public);
5253
}
5354

55+
[Test]
56+
public async Task PublicChannelSubscribeAndRecieveCountEvent() {
57+
var definition = new { subscription_count = 1 };
58+
var pusher = PusherFactory.GetPusher(saveTo: _clients);
59+
60+
void PusherCountEventHandler(object sender, string data) {
61+
var dataAsObj = JsonConvert.DeserializeAnonymousType(data, definition);
62+
Assert.Equals(dataAsObj.subscription_count, 1);
63+
}
64+
65+
pusher.CountHandler += PusherCountEventHandler;
66+
await ConnectThenSubscribeTestAsync(ChannelTypes.Public, pusher: pusher);
67+
}
68+
5469
[Test]
5570
public async Task PublicChannelUnsubscribeUsingChannelUnsubscribeAsync()
5671
{

PusherClient/Channel.cs

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading;
33
using System.Threading.Tasks;
4+
using Newtonsoft.Json;
45

56
namespace PusherClient
67
{
@@ -20,6 +21,9 @@ public class Channel : EventEmitter
2021
/// Fired when the Channel has successfully been subscribed to.
2122
/// </summary>
2223
internal event SubscriptionEventHandler Subscribed;
24+
internal event SubscriptionCountHandler CountHandler;
25+
26+
public int SubscriptionCount = 0;
2327

2428
/// <summary>
2529
/// Gets whether the Channel is currently Subscribed
@@ -93,6 +97,21 @@ internal virtual void SubscriptionSucceeded(string data)
9397
}
9498
}
9599

100+
internal virtual void SubscriberCount(string data)
101+
{ SubscriptionCount = ParseCount(data);
102+
if (CountHandler != null)
103+
{
104+
try
105+
{
106+
CountHandler.Invoke(this, data);
107+
}
108+
catch (Exception error)
109+
{
110+
_pusher.RaiseChannelError(new SubscribedEventHandlerException(this, error, data));
111+
}
112+
}
113+
}
114+
96115
/// <summary>
97116
/// Removes the channel subscription.
98117
/// </summary>
@@ -158,5 +177,17 @@ public static ChannelTypes GetChannelType(string channelName)
158177

159178
return channelType;
160179
}
180+
181+
public class SubscriptionCountData
182+
{
183+
[JsonProperty("subscription_count")]
184+
public int subscriptionCount { get; set; }
185+
}
186+
187+
private int ParseCount(string data)
188+
{
189+
var dataAsObj = JsonConvert.DeserializeObject<SubscriptionCountData>(data);
190+
return dataAsObj.subscriptionCount;
191+
}
161192
}
162-
}
193+
}

PusherClient/Connection.cs

+4
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ private bool ProcessPusherChannelEvent(string eventName, string channelName, str
233233
case Constants.CHANNEL_MEMBER_REMOVED:
234234
_pusher.RemoveMember(channelName, messageData);
235235
break;
236+
237+
case Constants.CHANNEL_SUBSCRIPTION_COUNT:
238+
_pusher.SubscriberCount(channelName, messageData);
239+
break;
236240

237241
default:
238242
processed = false;

PusherClient/Constants.cs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Constants
1010
public const string CHANNEL_SUBSCRIBE = "pusher:subscribe";
1111
public const string CHANNEL_UNSUBSCRIBE = "pusher:unsubscribe";
1212
public const string CHANNEL_SUBSCRIPTION_SUCCEEDED = "pusher_internal:subscription_succeeded";
13+
public const string CHANNEL_SUBSCRIPTION_COUNT = "pusher_internal:subscription_count";
1314
public const string CHANNEL_SUBSCRIPTION_ERROR = "pusher_internal:subscription_error";
1415
public const string CHANNEL_MEMBER_ADDED = "pusher_internal:member_added";
1516
public const string CHANNEL_MEMBER_REMOVED = "pusher_internal:member_removed";

PusherClient/EventHandler.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,6 @@ namespace PusherClient
6161
/// To be deprecated, please use <see cref="SubscribedEventHandler"/> instead.
6262
/// </remarks>
6363
public delegate void SubscriptionEventHandler(object sender);
64-
}
64+
65+
public delegate void SubscriptionCountHandler(object sender, string data);
66+
}

PusherClient/IPusher.cs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal interface IPusher
99
void AddMember(string channelName, string member);
1010
void RemoveMember(string channelName, string member);
1111
void SubscriptionSuceeded(string channelName, string data);
12+
void SubscriberCount(string channelName, string data);
1213
void SubscriptionFailed(string channelName, string data);
1314
IEventBinder GetEventBinder(string eventBinderKey);
1415
IEventBinder GetChannelEventBinder(string eventBinderKey, string channelName);

PusherClient/Pusher.cs

+15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class Pusher : EventEmitter, IPusher, ITriggerChannels
3838
/// Fires when a channel becomes subscribed.
3939
/// </summary>
4040
public event SubscribedEventHandler Subscribed;
41+
public event SubscriptionCountHandler CountHandler;
4142

4243
private static string Version { get; } = typeof(Pusher).GetTypeInfo().Assembly.GetName().Version.ToString(3);
4344

@@ -213,6 +214,20 @@ void IPusher.RemoveMember(string channelName, string member)
213214
}
214215
}
215216

217+
void IPusher.SubscriberCount(string channelName, string data)
218+
{
219+
if (Channels.TryGetValue(channelName, out Channel channel))
220+
{
221+
channel.SubscriberCount(data);
222+
if (CountHandler != null) {
223+
Task.Run(() =>
224+
{
225+
CountHandler.Invoke(this, data);
226+
});
227+
}
228+
}
229+
}
230+
216231
void IPusher.SubscriptionSuceeded(string channelName, string data)
217232
{
218233
if (Channels.TryGetValue(channelName, out Channel channel))

README.md

+56-39
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,49 @@ For integrating **Pusher Channels** with **Unity** follow the instructions at <h
2323

2424
## Contents
2525

26-
- [Installation](#installation)
27-
- [API](#api)
28-
- [Overview](#overview)
29-
- [Sample application](#sample-application)
30-
- [Configuration](#configuration)
31-
- [The PusherOptions object](#the-pusheroptions-object)
32-
- [Application Key](#application-key)
33-
- [Connecting](#connecting)
34-
- [Connection States](#connection-States)
35-
- [Auto reconnect](#auto-reconnect)
36-
- [Disconnecting](#disconnecting)
37-
- [Connected and Disconnected delegates](#connected-and-disconnected-delegates)
38-
- [Subscribing](#subscribing)
39-
- [Error handling](#error-handling)
40-
- [Public channels](#public-channels)
41-
- [Private channels](#private-channels)
42-
- [Private encrypted channels](#private-encrypted-channels)
43-
- [Presence channels](#Presence-channels)
44-
- [HttpAuthorizer](#httpauthorizer)
45-
- [Subscribed delegate](#subscribed-delegate)
46-
- [Unsubscribe](#unsubscribe)
47-
- [Binding to events](#binding-to-events)
48-
- [Per-channel](#per-channel)
49-
- [Globally](#globally)
50-
- [Triggering events](#triggering-events)
51-
- [Developer notes](#developer-notes)
52-
- [Testing](#testing)
53-
- [Code signing key generation](#code-signing-key-generation)
54-
- [Migrating from version 1 to version 2](#migrating-from-version-1-to-version-2)
55-
- [Changed in the Pusher class](#changed-in-the-pusher-class)
56-
- [Removed from the Pusher class](#removed-from-the-pusher-class)
57-
- [Removed from the Channel class](#removed-from-the-channel-class)
58-
- [Removed from the GenericPresenceChannel class](#removed-from-the-genericpresencechannel-class)
59-
- [Removed from the ConnectionState enum](#removed-from-the-connectionstate-enum)
60-
- [Changed in the GenericPresenceChannel class](#changed-in-the-genericpresencechannel-class)
61-
- [Added to the Pusher class](#added-to-the-pusher-class)
62-
- [Added to the GenericPresenceChannel class](#added-to-the-genericpresencechannel-class)
63-
- [Added to the ErrorCodes enum](#added-to-the-errorcodes-enum)
64-
- [License](#license)
26+
- [Pusher Channels .NET Client library](#pusher-channels-net-client-library)
27+
- [Supported platforms](#supported-platforms)
28+
- [Contents](#contents)
29+
- [Installation](#installation)
30+
- [API](#api)
31+
- [Overview](#overview)
32+
- [Sample application](#sample-application)
33+
- [Configuration](#configuration)
34+
- [The PusherOptions object](#the-pusheroptions-object)
35+
- [Application Key](#application-key)
36+
- [Connecting](#connecting)
37+
- [Connection States](#connection-states)
38+
- [Auto reconnect](#auto-reconnect)
39+
- [Disconnecting](#disconnecting)
40+
- [Connected and Disconnected delegates](#connected-and-disconnected-delegates)
41+
- [Subscribing](#subscribing)
42+
- [Error handling](#error-handling)
43+
- [Public channels](#public-channels)
44+
- [Private channels](#private-channels)
45+
- [Private encrypted channels](#private-encrypted-channels)
46+
- [Presence channels](#presence-channels)
47+
- [HttpAuthorizer](#httpauthorizer)
48+
- [Subscribed delegate](#subscribed-delegate)
49+
- [Unsubscribe](#unsubscribe)
50+
- [Subscription Count Handler](#subscription-count-handler)
51+
- [Binding to events](#binding-to-events)
52+
- [Per-channel](#per-channel)
53+
- [Globally](#globally)
54+
- [Triggering events](#triggering-events)
55+
- [Developer notes](#developer-notes)
56+
- [Testing](#testing)
57+
- [Code signing key generation](#code-signing-key-generation)
58+
- [Migrating from version 1 to version 2](#migrating-from-version-1-to-version-2)
59+
- [Changed in the Pusher class](#changed-in-the-pusher-class)
60+
- [Removed from the Pusher class](#removed-from-the-pusher-class)
61+
- [Removed from the Channel class](#removed-from-the-channel-class)
62+
- [Removed from the GenericPresenceChannel class](#removed-from-the-genericpresencechannel-class)
63+
- [Removed from the ConnectionState enum](#removed-from-the-connectionstate-enum)
64+
- [Changed in the GenericPresenceChannel class](#changed-in-the-genericpresencechannel-class)
65+
- [Added to the Pusher class](#added-to-the-pusher-class)
66+
- [Added to the GenericPresenceChannel class](#added-to-the-genericpresencechannel-class)
67+
- [Added to the ErrorCodes enum](#added-to-the-errorcodes-enum)
68+
- [License](#license)
6569

6670
## Installation
6771

@@ -930,6 +934,19 @@ await pusher.UnsubscribeAllAsync().ConfigureAwait(false);
930934

931935
```
932936

937+
## Subscription Count Handler
938+
939+
Add this handler to recieve subscription count events. Read more about it [here](https://pusher.com/docs/channels/using_channels/events/#pushersubscription_count-1165820117)
940+
941+
```cs
942+
void PusherCountEventHandler(object sender, string data) {
943+
var dataAsObj = JsonConvert.DeserializeObject<SubscriptionCountData>(data);
944+
Console.WriteLine(dataAsObj.subscriptionCount);
945+
}
946+
947+
pusher.CountHandler += PusherCountEventHandler;
948+
```
949+
933950
## Binding to events
934951

935952
Events can be bound to at two levels; per-channel or globally.

0 commit comments

Comments
 (0)