From 76a3de7dcc5d465331b82024aba7611f25579e8e Mon Sep 17 00:00:00 2001 From: Benjamin Tang <83954069+benjamin-tang-pusher@users.noreply.github.com> Date: Mon, 18 Sep 2023 16:03:57 -0400 Subject: [PATCH 1/3] Add signin and watchlist instructions to readme --- README.md | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index db6c1a0..06bb4d8 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ For integrating **Pusher Channels** with **Unity** follow the instructions at (data); Console.WriteLine(dataAsObj.subscriptionCount); } pusher.CountHandler += PusherCountEventHandler; + +``` + +## User authentication + +Allows you to send events to a user based on user id or terminating a user’s connections immediately. Read more about it [here](https://pusher.com/docs/channels/using_channels/user-authentication/) + +### Signing in + +The library provides an `HttpUserAuthenticator` implementation of `IUserAuthenticator` which makes an HTTP `POST` request to an authenticating endpoint. However, you can implement your own authentication mechanism if required. + +Setting up a authenticated user + +```cs + +// Create client +Pusher pusher = new Pusher(Config.AppKey, new PusherOptions +{ + UserAuthenticator = new HttpUserAuthenticator("https:/some.authenticator.com/auth") + Cluster = Config.Cluster, +}); +pusher.Error += ErrorHandler; + +// Sign in +pusher.User.Signin(); + +// Connect +await pusher.ConnectAsync().ConfigureAwait(false); +await pusher.User.SigninDoneAsync(); + +``` + +### HttpUserAuthenticator + +The implementation of the `HttpUserAuthenticator` class which provides the default implementation of an +`IUserAuthenticator` has been modified to support the setting of an authentication header. + +Here is an example of how to set the bearer token in an authentication header: + +```cs +var authorizer = new HttpUserAuthenticator("https:/some.authenticator.com/auth") +{ + AuthenticationHeader = new AuthenticationHeaderValue("Authorization", "Bearer noo6xaeN3cohYoozai4ar8doang7ai1elaeTh1di"), +}; +var authToken = await authorizer.Authenticate("test-user", "1234.9876"); +``` + +If you require setting other headers you can override the `PreRequest` method on the `HttpUserAuthenticator` class. + +```cs +public override void PreRequest(HttpClient httpClient) +{ + base.PreRequest(httpClient); + + // Add headers or other settings to the httpClient before authorizing. +} ``` ## Binding to events @@ -1041,6 +1099,48 @@ pusher.UnbindAll(); ``` +### Watchlist + +If you have signed in as an [authenticated user](#user-authentication), you can bind to [watchlist events](https://pusher.com/docs/channels/using_channels/watchlist-events/). + +```cs + +pusher.User.Watchlist.Bind("online", OnWatchlistOnlineEvent); +pusher.User.Watchlist.Bind("offline", OnWatchlistOfflineEvent); + +``` + +```cs + +void OnWatchlistOnlineEvent(WatchlistEvent watchlistEvent) + { + foreach (string userId in watchlistEvent.UserIDs) + { + int x = Int32.Parse(userId); + Debug.Log($"UserID {x} ONLINE"); + } + } + +void OnWatchlistOfflineEvent(WatchlistEvent watchlistEvent) + { + foreach (string userId in watchlistEvent.UserIDs) + { + int x = Int32.Parse(userId); + Debug.Log($"UserID {x} OFFLINE"); + } + } + +//Bind to all watchlist events +static void OnWatchlistEvent(string eventName, WatchlistEvent watchlistEvent) +{ + foreach (var id in watchlistEvent.UserIDs) + { + Debug.Log($"{Environment.NewLine} OnWatchlistEvent {eventName} {watchlistEvent.Name} {id}"); + } +} + +``` + ## Triggering events Once a [private](https://pusher.com/docs/channels/using_channels/private-channels) or [presence](https://pusher.com/docs/channels/using_channels/presence-channels) subscription has been authorized (see [authenticating users](https://pusher.com/docs/channels/server_api/authenticating-users)) and the subscription has succeeded, it is possible to trigger events on those channels. From c5220d2ae03fa8384bba3da9159009c630fb68f6 Mon Sep 17 00:00:00 2001 From: Benjamin Tang <83954069+benjamin-tang-pusher@users.noreply.github.com> Date: Mon, 18 Sep 2023 16:10:35 -0400 Subject: [PATCH 2/3] tidy readme code snippet --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 06bb4d8..3e5a555 100644 --- a/README.md +++ b/README.md @@ -1116,8 +1116,7 @@ void OnWatchlistOnlineEvent(WatchlistEvent watchlistEvent) { foreach (string userId in watchlistEvent.UserIDs) { - int x = Int32.Parse(userId); - Debug.Log($"UserID {x} ONLINE"); + Debug.Log($"UserID {userId} ONLINE"); } } @@ -1125,8 +1124,7 @@ void OnWatchlistOfflineEvent(WatchlistEvent watchlistEvent) { foreach (string userId in watchlistEvent.UserIDs) { - int x = Int32.Parse(userId); - Debug.Log($"UserID {x} OFFLINE"); + Debug.Log($"UserID {userId} OFFLINE"); } } From ac5780572accdffcb065a352fa2b6694e5a56fa5 Mon Sep 17 00:00:00 2001 From: Benjamin Tang <83954069+benjamin-tang-pusher@users.noreply.github.com> Date: Tue, 19 Sep 2023 10:33:07 -0400 Subject: [PATCH 3/3] Tidy HttpUserAuthenticator description readme --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3e5a555..fd51bd3 100644 --- a/README.md +++ b/README.md @@ -958,14 +958,14 @@ Allows you to send events to a user based on user id or terminating a user’s c The library provides an `HttpUserAuthenticator` implementation of `IUserAuthenticator` which makes an HTTP `POST` request to an authenticating endpoint. However, you can implement your own authentication mechanism if required. -Setting up a authenticated user +Setting up an authenticated user ```cs // Create client Pusher pusher = new Pusher(Config.AppKey, new PusherOptions { - UserAuthenticator = new HttpUserAuthenticator("https:/some.authenticator.com/auth") + UserAuthenticator = new HttpUserAuthenticator("https://some.authenticator.com/auth") Cluster = Config.Cluster, }); pusher.Error += ErrorHandler; @@ -981,8 +981,7 @@ await pusher.User.SigninDoneAsync(); ### HttpUserAuthenticator -The implementation of the `HttpUserAuthenticator` class which provides the default implementation of an -`IUserAuthenticator` has been modified to support the setting of an authentication header. +The `HttpUserAuthenticator` class supports the setting of an authentication header. Here is an example of how to set the bearer token in an authentication header: