From 1dd6300e26ae05fc5647cfb618b9040fd0045b3e Mon Sep 17 00:00:00 2001 From: Andreas Bieber Date: Wed, 17 Dec 2014 15:57:48 +0100 Subject: [PATCH] updated readme to the latest changes --- README.md | 201 +++++++++++++++++++++++------------------------------- 1 file changed, 85 insertions(+), 116 deletions(-) diff --git a/README.md b/README.md index b70c8dc..bac56dc 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ One thing that really bugged me out about SignalR is the lack of strongly typed This library will enable this feature via interface implementations. ## Get it on NuGet! + https://www.nuget.org/packages/SignalR.Client.TypedHubProxy +or Install-Package SignalR.Client.TypedHubProxy @@ -13,135 +15,102 @@ This library will enable this feature via interface implementations. [Apache 2.0 License](https://github.com/Gandalis/SignalR.Client.TypedHubProxy/blob/master/LICENSE.md) ## Example -### Prepare interfaces -#### IChatEvents -This interface will be used by the server to call methods from the client. +First of all we have to declare the interfaces: + ```csharp -namespace Sample.Shared +public interface IServerHub { - public interface IChatEvents - { - void NewMessage(string msg); - } + void DoSomething(); + void DoSomethingWithParam(int id); + int DoSomethingWithParamAndResult(int id); } -``` -#### IChatHub -This interface will be implemented by the serverhub. The client will use this interface also to call these defined interface methods on the server. -```csharp -using System.Threading.Tasks; -namespace Sample.Shared +public interface IClientContract { - public interface IChatHub - { - void SendMessage(string msg); - int GetConnectedClients(); - } + void SomeInformation(); + void SomeInformationWithParam(int id); } ``` -### The Hub -#### ChatHub -This is the chathub inside of the server, which implements IChatHub and uses IChatEvents. -```csharp -using System; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.AspNet.SignalR; -using Sample.Shared; -namespace Sample.Server +After that, we have to implement the ServerHub which implements IServerHub and uses IClientContract: + +```csharp +public class ServerHub : Microsoft.AspNet.SignalR.Hub, IServerHub { - public class ChatHub : Hub, IChatHub + public void DoSomething() { - private static int _connectedClients; - - static ChatHub() - { - new Timer(BroadcastMessage, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1)); - } - - public override Task OnConnected() - { - ++_connectedClients; - return base.OnConnected(); - } - - public override Task OnDisconnected(bool stopCalled) - { - --_connectedClients; - return base.OnDisconnected(stopCalled); - } - - /// - /// Interface implementation of IChatHub. - /// This method can be called from a client. - /// - /// The chat message. - /// - public void SendMessage(string msg) - { - Clients.All.NewMessage("CLIENT > " + msg); - } - - public int GetConnectedClients() - { - return _connectedClients; - } - - private static void BroadcastMessage(object state) - { - IHubContext hubContext = - GlobalHost.ConnectionManager.GetHubContext(); - hubContext.Clients.All.NewMessage(string.Format("SERVER > Hello client {0}", DateTime.Now)); - } + System.Console.WriteLine("DoSomething called."); } -} -``` -### The Client -### Program.cs -This is the chatclient. -```csharp -using System; -using Microsoft.AspNet.SignalR.Client; -using Sample.Shared; -namespace Sample.Client -{ - internal class Program + public void DoSomethingWithParam(int id) + { + System.Console.WriteLine("DoSomethingWithParam called."); + } + + public int DoSomethingWithParamAndResult(int id) { - private static void Main() - { - // Create the connection - var connection = new HubConnection("http://localhost:1337/signalr"); - - // Create the hubproxy - var hubProxy = connection.CreateHubProxy("chatHub"); - - // Subscribe on the event IChatEvents.NewMessage. - // When the event was fired through the server, the static method Program.NewMessage(string msg) will be invoked. - hubProxy.SubscribeOn(hub => hub.NewMessage, NewMessage); - - // Start the connection - connection.Start().Wait(); - - // Call the method IChatHub.GetConnectedClients() on the server and get the result. - int clientCount = hubProxy.Call(hub => hub.GetConnectedClients()); - Console.WriteLine("Connected clients: {0}", clientCount); - - // Call the method IChatHub.SendMessage with no result. - hubProxy.Call(hub => hub.SendMessage("Hi, i'm the client.")); - Console.ReadKey(); - connection.Stop(); - } - - /// - /// This method can be called from the server. - /// - /// The incoming chat message. - public static void NewMessage(string msg) - { - Console.WriteLine(msg); - } + System.Console.WriteLine("DoSomethingWithParamAndResult called."); + return id; } } +``` +At the client, we have to set up the connection: + +```csharp +var hubConnection = new Microsoft.AspNet.SignalR.Client.HubConnection("http://localhost:1337/signalr"); +``` +### Invocations + +The next part is the interesting one - The usage of the strongly typed HubProxy. +To understand exactly what the TypedHubProxy does, here an example of how it is used normally: + +```csharp +Microsoft.AspNet.SignalR.Client.IHubProxy hubProxy = hubConnection.CreateHubProxy("serverHub"); +hubProxy.Invoke("DoSomething", new object[0]); +``` +The part of the Invoke had really bugged me out was the lack of strongly typed invocation. +So SignalR.Client.TypedHubProxy enables strongly typed invocation and much more. The following code sample shows you exactly what can it be used for: +```csharp +IHubProxy hubProxy = hubConnection.CreateHubProxy("serverHub"); + +// Here you can define the method which should be called - strongly typed. +hubProxy.Call(hub => hub.DoSomething()); + +// For methods with parameters, just handover the parameter. +hubProxy.Call(hub => hub.DoSomethingWithParam(5)); + +// For methods with a result, you can receive it as expected. +int result = hubProxy.Call(hub => hub.DoSomethingWithParamAndResult(5)); +``` +Async calls are also possible: +```csharp +hubProxy.CallAsync(hub => hub.DoSomething()); +hubProxy.CallAsync(hub => hub.DoSomethingWithParam(5)); +System.Threading.Tasks.Task asyncResult = hubProxy.CallAsync(hub => hub.DoSomethingWithParamAndResult(5)); +int result = asyncResult.Result; // or just: int result = hubProxy.CallAsync(hub => hub.DoSomethingWithParamAndResult(5)).Result; +``` + +### Subscriptions +The old way: +```csharp +Microsoft.AspNet.SignalR.Client.IHubProxy hubProxy = hubConnection.CreateHubProxy("serverHub"); +hubProxy.On("SomeInformationWithParam", i => System.Console.WriteLine("Got new information: {0}", i)); +``` + +And the new way with SignalR.Client.TypedHubProxy: +```csharp +IHubProxy hubProxy = hubConnection.CreateHubProxy("serverHub"); +hubProxy.SubscribeOn(hub => hub.SomeInformation, () => System.Console.WriteLine("Got some new information.")); +hubProxy.SubscribeOn(hub => hub.SomeInformationWithParam, i => System.Console.WriteLine("Got new information: {0}", i)); +``` + +It's also possible to define a condition on the subscription, so that you will be only called if the condition is true: +```csharp +IHubProxy hubProxy = hubConnection.CreateHubProxy("serverHub"); +hubProxy.SubscribeOn(hub => hub.SomeInformationWithParam, i => i == 5, i => System.Console.WriteLine("Got new information where data == 5")); +``` + +### Observable +```csharp +System.IObservable observableResult = hubProxy.Observe(hub => hub.SomeInformationWithParam); ``` \ No newline at end of file