Tuesday, June 19, 2018

Message send to Web Application to Desktop Application using SignalR

Sample Application

Let us build a sample chat application that broadcasts the messages to all connected clients or to particular groups. In this example consider the clients to be a Windows application and a web form client.

Creating an Asp.Net SignalR Hub

Open Visual Studio and create an empty Asp.net MVC application named MyChatApplicationServer. Now open the NuGet packages, search and include Microsoft ASP.NET SignalR and Microsoft.Owin.Cors.
Now right click on the project, add the SignalR Hub class and name it as MyChatHub.  Add the following code to the class.



The HUB class also provides the properties Clients, Groups, Context and events like OnConnected, etc.
Finally you should also add Owin startup class to map the available hubs as shown below.

Pushing Data to a Web Form Client

Now create a web form client project named WebClient and add a simple HTML file ChatWebClient.html with the following code. Add the references to the required script files.

There are two ways of implementing the client function one with proxy and other without. The above example is the one without a proxy.
In the connection start you may also mention which transport SignalR should use.

Pushing Data to a Windows Application Client

Let us add a windows app and subscribe to the SignalR host. Create a new windows application named WinFormClient and from the nugget packages install Microsoft ASP.NET SignalR .NET client package. In the Program.cs add the following code.

Once done go ahead and run both the web and Windows client. Enter the messages and look at how the message is getting pushed across both the clients though they are of a different nature. Fig 2.0 shows a sample screenshot.


No comments:

Post a Comment