Skip to content

Commit

Permalink
Merge pull request #126 from pusher/SubscriptionCountEvent
Browse files Browse the repository at this point in the history
Addind subscription count event handler
  • Loading branch information
singhashmeet authored Aug 4, 2022
2 parents a721e92 + 8b09cfa commit 380c77b
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 50 deletions.
19 changes: 11 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# Changelog

## 2.1.0
# Changelog

## 2.2.0
* [ADDED] Support for subscription_count events

## 2.1.0
* [ADDED] Strong name to the PusherClient assembly.
* [ADDED] Support for the authentication header on the HttpAuthorizer.
* [ADDED] End-to-end encryption for private encrypted channels.
* [ADDED] Method Channel.UnsubscribeAsync.
* [ADDED] Host to PusherOptions.
* [FIXED] The intermittent WebsocketAutoReconnect issue The socket is connecting, cannot connect again!

## 2.0.1
* [FIXED] Filter on event name in event emitter.

## 2.0.0

## 2.0.1
* [FIXED] Filter on event name in event emitter.

## 2.0.0
* [FIXED] Infinite loop when failing to connect for the first time.
* [FIXED] Bug: GenericPresenceChannel<T>'s AddMember and RemoveMember events were not being emitted.
* [FIXED] Change MemberRemovedEventHandler to MemberRemovedEventHandler<T>.
Expand Down
7 changes: 6 additions & 1 deletion ExampleApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static async Task InitPusher()
{
_pusher = new Pusher(Config.AppKey, new PusherOptions
{
Authorizer = new HttpAuthorizer("http://localhost:8888/auth/" + HttpUtility.UrlEncode(_name)),
Authorizer = new HttpAuthorizer("http://127.0.0.1:3030/pusher/auth" + HttpUtility.UrlEncode(_name)),
Cluster = Config.Cluster,
Encrypted = Config.Encrypted,
TraceLogger = new TraceLogger(),
Expand Down Expand Up @@ -96,6 +96,11 @@ private static async Task InitPusher()
}
};

_pusher.CountHandler += (sender, data) =>
{
Console.WriteLine(data);
};

// Setup private encrypted channel
void GeneralListener(string eventName, PusherEvent eventData)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using NUnit.Framework;
using PusherClient.Tests.Utilities;
using Newtonsoft.Json;

namespace PusherClient.Tests.AcceptanceTests
{
Expand Down Expand Up @@ -51,6 +52,20 @@ public async Task PublicChannelConnectThenSubscribeWithoutAnyEventHandlersAsync(
ValidateSubscribedChannel(pusher, mockChannelName, channel, ChannelTypes.Public);
}

[Test]
public async Task PublicChannelSubscribeAndRecieveCountEvent() {
var definition = new { subscription_count = 1 };
var pusher = PusherFactory.GetPusher(saveTo: _clients);

void PusherCountEventHandler(object sender, string data) {
var dataAsObj = JsonConvert.DeserializeAnonymousType(data, definition);
Assert.Equals(dataAsObj.subscription_count, 1);
}

pusher.CountHandler += PusherCountEventHandler;
await ConnectThenSubscribeTestAsync(ChannelTypes.Public, pusher: pusher);
}

[Test]
public async Task PublicChannelUnsubscribeUsingChannelUnsubscribeAsync()
{
Expand Down
33 changes: 32 additions & 1 deletion PusherClient/Channel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace PusherClient
{
Expand All @@ -20,6 +21,9 @@ public class Channel : EventEmitter
/// Fired when the Channel has successfully been subscribed to.
/// </summary>
internal event SubscriptionEventHandler Subscribed;
internal event SubscriptionCountHandler CountHandler;

public int SubscriptionCount = 0;

/// <summary>
/// Gets whether the Channel is currently Subscribed
Expand Down Expand Up @@ -93,6 +97,21 @@ internal virtual void SubscriptionSucceeded(string data)
}
}

internal virtual void SubscriberCount(string data)
{ SubscriptionCount = ParseCount(data);
if (CountHandler != null)
{
try
{
CountHandler.Invoke(this, data);
}
catch (Exception error)
{
_pusher.RaiseChannelError(new SubscribedEventHandlerException(this, error, data));
}
}
}

/// <summary>
/// Removes the channel subscription.
/// </summary>
Expand Down Expand Up @@ -158,5 +177,17 @@ public static ChannelTypes GetChannelType(string channelName)

return channelType;
}

public class SubscriptionCountData
{
[JsonProperty("subscription_count")]
public int subscriptionCount { get; set; }
}

private int ParseCount(string data)
{
var dataAsObj = JsonConvert.DeserializeObject<SubscriptionCountData>(data);
return dataAsObj.subscriptionCount;
}
}
}
}
4 changes: 4 additions & 0 deletions PusherClient/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ private bool ProcessPusherChannelEvent(string eventName, string channelName, str
case Constants.CHANNEL_MEMBER_REMOVED:
_pusher.RemoveMember(channelName, messageData);
break;

case Constants.CHANNEL_SUBSCRIPTION_COUNT:
_pusher.SubscriberCount(channelName, messageData);
break;

default:
processed = false;
Expand Down
1 change: 1 addition & 0 deletions PusherClient/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Constants
public const string CHANNEL_SUBSCRIBE = "pusher:subscribe";
public const string CHANNEL_UNSUBSCRIBE = "pusher:unsubscribe";
public const string CHANNEL_SUBSCRIPTION_SUCCEEDED = "pusher_internal:subscription_succeeded";
public const string CHANNEL_SUBSCRIPTION_COUNT = "pusher_internal:subscription_count";
public const string CHANNEL_SUBSCRIPTION_ERROR = "pusher_internal:subscription_error";
public const string CHANNEL_MEMBER_ADDED = "pusher_internal:member_added";
public const string CHANNEL_MEMBER_REMOVED = "pusher_internal:member_removed";
Expand Down
4 changes: 3 additions & 1 deletion PusherClient/EventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ namespace PusherClient
/// To be deprecated, please use <see cref="SubscribedEventHandler"/> instead.
/// </remarks>
public delegate void SubscriptionEventHandler(object sender);
}

public delegate void SubscriptionCountHandler(object sender, string data);
}
1 change: 1 addition & 0 deletions PusherClient/IPusher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal interface IPusher
void AddMember(string channelName, string member);
void RemoveMember(string channelName, string member);
void SubscriptionSuceeded(string channelName, string data);
void SubscriberCount(string channelName, string data);
void SubscriptionFailed(string channelName, string data);
IEventBinder GetEventBinder(string eventBinderKey);
IEventBinder GetChannelEventBinder(string eventBinderKey, string channelName);
Expand Down
15 changes: 15 additions & 0 deletions PusherClient/Pusher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Pusher : EventEmitter, IPusher, ITriggerChannels
/// Fires when a channel becomes subscribed.
/// </summary>
public event SubscribedEventHandler Subscribed;
public event SubscriptionCountHandler CountHandler;

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

Expand Down Expand Up @@ -213,6 +214,20 @@ void IPusher.RemoveMember(string channelName, string member)
}
}

void IPusher.SubscriberCount(string channelName, string data)
{
if (Channels.TryGetValue(channelName, out Channel channel))
{
channel.SubscriberCount(data);
if (CountHandler != null) {
Task.Run(() =>
{
CountHandler.Invoke(this, data);
});
}
}
}

void IPusher.SubscriptionSuceeded(string channelName, string data)
{
if (Channels.TryGetValue(channelName, out Channel channel))
Expand Down
95 changes: 56 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,49 @@ For integrating **Pusher Channels** with **Unity** follow the instructions at <h

## Contents

- [Installation](#installation)
- [API](#api)
- [Overview](#overview)
- [Sample application](#sample-application)
- [Configuration](#configuration)
- [The PusherOptions object](#the-pusheroptions-object)
- [Application Key](#application-key)
- [Connecting](#connecting)
- [Connection States](#connection-States)
- [Auto reconnect](#auto-reconnect)
- [Disconnecting](#disconnecting)
- [Connected and Disconnected delegates](#connected-and-disconnected-delegates)
- [Subscribing](#subscribing)
- [Error handling](#error-handling)
- [Public channels](#public-channels)
- [Private channels](#private-channels)
- [Private encrypted channels](#private-encrypted-channels)
- [Presence channels](#Presence-channels)
- [HttpAuthorizer](#httpauthorizer)
- [Subscribed delegate](#subscribed-delegate)
- [Unsubscribe](#unsubscribe)
- [Binding to events](#binding-to-events)
- [Per-channel](#per-channel)
- [Globally](#globally)
- [Triggering events](#triggering-events)
- [Developer notes](#developer-notes)
- [Testing](#testing)
- [Code signing key generation](#code-signing-key-generation)
- [Migrating from version 1 to version 2](#migrating-from-version-1-to-version-2)
- [Changed in the Pusher class](#changed-in-the-pusher-class)
- [Removed from the Pusher class](#removed-from-the-pusher-class)
- [Removed from the Channel class](#removed-from-the-channel-class)
- [Removed from the GenericPresenceChannel class](#removed-from-the-genericpresencechannel-class)
- [Removed from the ConnectionState enum](#removed-from-the-connectionstate-enum)
- [Changed in the GenericPresenceChannel class](#changed-in-the-genericpresencechannel-class)
- [Added to the Pusher class](#added-to-the-pusher-class)
- [Added to the GenericPresenceChannel class](#added-to-the-genericpresencechannel-class)
- [Added to the ErrorCodes enum](#added-to-the-errorcodes-enum)
- [License](#license)
- [Pusher Channels .NET Client library](#pusher-channels-net-client-library)
- [Supported platforms](#supported-platforms)
- [Contents](#contents)
- [Installation](#installation)
- [API](#api)
- [Overview](#overview)
- [Sample application](#sample-application)
- [Configuration](#configuration)
- [The PusherOptions object](#the-pusheroptions-object)
- [Application Key](#application-key)
- [Connecting](#connecting)
- [Connection States](#connection-states)
- [Auto reconnect](#auto-reconnect)
- [Disconnecting](#disconnecting)
- [Connected and Disconnected delegates](#connected-and-disconnected-delegates)
- [Subscribing](#subscribing)
- [Error handling](#error-handling)
- [Public channels](#public-channels)
- [Private channels](#private-channels)
- [Private encrypted channels](#private-encrypted-channels)
- [Presence channels](#presence-channels)
- [HttpAuthorizer](#httpauthorizer)
- [Subscribed delegate](#subscribed-delegate)
- [Unsubscribe](#unsubscribe)
- [Subscription Count Handler](#subscription-count-handler)
- [Binding to events](#binding-to-events)
- [Per-channel](#per-channel)
- [Globally](#globally)
- [Triggering events](#triggering-events)
- [Developer notes](#developer-notes)
- [Testing](#testing)
- [Code signing key generation](#code-signing-key-generation)
- [Migrating from version 1 to version 2](#migrating-from-version-1-to-version-2)
- [Changed in the Pusher class](#changed-in-the-pusher-class)
- [Removed from the Pusher class](#removed-from-the-pusher-class)
- [Removed from the Channel class](#removed-from-the-channel-class)
- [Removed from the GenericPresenceChannel class](#removed-from-the-genericpresencechannel-class)
- [Removed from the ConnectionState enum](#removed-from-the-connectionstate-enum)
- [Changed in the GenericPresenceChannel class](#changed-in-the-genericpresencechannel-class)
- [Added to the Pusher class](#added-to-the-pusher-class)
- [Added to the GenericPresenceChannel class](#added-to-the-genericpresencechannel-class)
- [Added to the ErrorCodes enum](#added-to-the-errorcodes-enum)
- [License](#license)

## Installation

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

```

## Subscription Count Handler

Add this handler to recieve subscription count events. Read more about it [here](https://pusher.com/docs/channels/using_channels/events/#pushersubscription_count-1165820117)

```cs
void PusherCountEventHandler(object sender, string data) {
var dataAsObj = JsonConvert.DeserializeObject<SubscriptionCountData>(data);
Console.WriteLine(dataAsObj.subscriptionCount);
}

pusher.CountHandler += PusherCountEventHandler;
```

## Binding to events

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

0 comments on commit 380c77b

Please sign in to comment.