diff --git a/README.md b/README.md index db6c1a0..fd51bd3 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 an 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 `HttpUserAuthenticator` class supports 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 +1098,46 @@ 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) + { + Debug.Log($"UserID {userId} ONLINE"); + } + } + +void OnWatchlistOfflineEvent(WatchlistEvent watchlistEvent) + { + foreach (string userId in watchlistEvent.UserIDs) + { + Debug.Log($"UserID {userId} 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.