Find sample codes snippets for iPhone applications development, ASP.NET, J2ME and many important articles related to IT such as Web 2.0.

2013-03-31

Connecting a .NET client (Console application) to a website with SignalR

On one of my previous posts, I’ve shown you how to build a web based chat application with the use of SignalR. SignalR isn’t only restricted to web clients. It also works with many other clients like iOS, Android (There are no official support for them yet though. Those projects are community driven at this stage.) and Silverlight, .NET clients. In this blog post, I’m going to show you how to integrate a .NET client to my already built web application.

Following is the link to the web application I’ve created using SignalR

Now I’m going to connect my .NET client (Console application) to that hosted website. For that, first of all I have to install SignalR .NET Client for my Console application. I can either do it by manually adding the DLL files and adding the references to the project or install it directly using NuGet Package manager. Since Installing it via NuGet Package manager is a lot easier, I’m going to use that.

After installing the Microsoft ASP.NET SignalR .NET Client, we are ready to develop our application. Following is the code for the Console app

At the first line, I’m connecting to the hosted website. In this example, the chat server. I’m using localhost because I’m using Visual Studio to run the application. If you’ve already hosted the application in somewhere else, then you have to give that web address to the HubConnection Constructor.

The above line will create a Connection to the Hub hosted in http://localhost:53748. We can use that to create a Hub Proxy. In my example, the hub name is ChatHub (The class name in server). So I’ve passed that to CreateHubProxy method. Then I’ve registered the method “braodcastMessage”, to display the messages on Console, app which I get from the other clients.

As you can see, there I’m calling the method “broadcastMessage” on all clients. In the next line I’m starting the connection with the server.

hubConnection.Start().Wait();

I’m calling the Wait() method in order to wait till the connection made between server and my .NET client.

Next, I’m calling the Notify method in the server. It’s responsible for adding the connection ID to the client list and notifying other clients about the arrival of Console client.

chat.Invoke("Notify", "Console app", hubConnection.ConnectionId);

Here I’ve hardcoded the name “Console app”.

After that, I’m running a loop to get the messages from Console app. Then I invoke the server method as soon as I get a message from the .NET Client user. Then the server method, in this case “Send”, is responsible for broadcasting the message to all other connected clients.

So that’s all what it takes to build up a simple .NET client application which connects to a SignalR hub to do the real time communication. Following is a screenshot of how it looks like when it communicates with a web client.

Is it possible to interact my web based signalR chat system with android app do u have any idea about this?? or if you have any blog post related to it then please tell me any help is really appriciated

We implemented signal R in SL4 using javascript without moving to SL5 . Now the problem we are getting with Out of browser app in SL due to javascript is not loading because of Default.aspx file is not invoked which is web browser related. Can you please suggest any solutions how we can implement in OOB app using Javascript or any other options. If possible can you please give a sample how we can implement signalr client using websockets using WCF.