Monday, 25 May 2015

Continuing with SignalR

Hola amigo!!!

Ella unnav? (How are you? in Telugu :D )

Anyways, so here we continue with the second part of making apps with SignalR, a pretty cool library, for first part refer:

So struggling to figure out how to implement signalR in console apps that with .NET Client, I finally got the way through it.

You need to have two separate projects(preferably):
1. Server 
2. Client

Inside Server:

I have a main class, a startup class and a hub.
Before starting make sure you have added relevant references beforehand.
Right click the project name and select Manage NuGet Packages, install:
signalR .NET client : Microsoft ASP.NET SignalR .NET Client
and  also SelfHosting 

One thing that comes into scens and is the most important is Cross Domain Communication.

Both the server and the client are separate projects now, so the client must be able to identify and connect to the server running in parallel as a separate project. What changes do we need to incorporate with the same?

Here they are:

Majorly the Startup.cs OWIN class gets changed to support cross domain connection.
Refer this for more details on what it is:

So the startup file is changed to:

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Cors;
using Microsoft.AspNet.SignalR;
namespace BroadcasterConsole
    public class Startup
        public void Configuration(IAppBuilder app)
            app.Map("/signalr", map =>
                var hubConfiguration = new HubConfiguration
                    EnableJSONP = true

CorsOptions.AllowAll  will help your cross domain client to connect to te serevr. Also, please don't miss out the line:  [assemblyOwinStartup(typeof(BroadcasterConsole.Startup))]

Theses changes will make your server been detected and connected by the cross domain clients.

Rest of the server code is more or lesss the same as teh previous code. We need to have a hub class and a main class, I preferred to have them both in same namespace and it goes like this:

namespace BroadcasterConsole
    class Broadcast
        static void Main(string[] args)
            string url = "http://localhost:8088";
                using (WebApp.Start(url))
                  //Have your code here
//Don't forget this line above, this name helps you to connect to the hub from the client side
    public class Broadcaster : Hub
        public override Task OnConnected()
            Console.WriteLine("Hub OnConnected {0}\n", Context.ConnectionId);
            return (base.OnConnected());
 //Define your own hub methods
        public override Task OnReconnected()
            Console.WriteLine("Hub OnReconnected {0}\n", Context.ConnectionId);
            return (base.OnDisconnected(true));

So, let's come back to the client side. I tried to have clients in different languages.

A peek into Javascript Client:

Firstly, include these scripts in this particular order:

   <!--Reference the jQuery library. -->
   <script src="Scripts/jquery-2.1.4.js"></script>
   <!--Reference the SignalR library. -->
   <script src="Scripts/jquery.signalR-2.2.0.js"></script>
   <!--Reference the autogenerated SignalR hub script. -->
   <script src="http://localhost:8088/signalr/hubs" type="text/javascript"></script>

Please see I have hosted my server on 8088 port, change it accordingly, as per the url of your hosted server.

Here goes your main connection code:

<script type="text/javascript">
       $.connection.hub.url = 'http://localhost:8088/signalr';
       var hubProxy = $.connection.broadcaster;
       hubProxy.client.addMessage = function (message) {
           //message is a c# struct Request object.
           // Add the message to the page.
           $('#discussion').append('<li><strong> ' + document.getElementById('displayname').value + ' received request  Name:' + message.Name +  '\n   Program:' + message.Program +  '\n     Input:' + message.Input
               + '</strong>:&nbsp;&nbsp;</li>');
           alert('Now connected, connection ID=' + $;
           $('#displayname').val(prompt('Which process are you?'''));
           $('#sendoutput').click(function () {
               hubProxy.server.getClientOutput({ "Name""JS Client""Result": $('#output').val() });

Its fine if you don't get any of the above code since it would be a bit out of the context you are into. just focus on highlighted lines.

Set the hub url, create a hub Proxy.
Prior to starting teh connection, add the client side methods that the hub can call, for example here we have: addMessage.
Then start the connection by this line:  $.connection.hub.start().done.

You can call server side methods by : hubProxy.server.getClientOutput (//required params)

There we finish with the JavaScript client, we can do the same with clients for different languages:

.NET Client:

//Set connection
           var connection = new HubConnection("http://localhost:8088/");
//Make proxy to hub based on hub name on server
           myHub = connection.CreateHubProxy("Broadcaster");
//Add client side methods

           myHub.On<Request>("addMessage", param =>{
              //Define your method
//Start connection
           connection.Start().ContinueWith(task =>
               if (task.IsFaulted)
                   Console.WriteLine("There was an error opening the connection:{0}",
 //Invoking hub methods

           myHub.Invoke<string>(<Method Name as is>, clientName)

C++ Client:
Here is a detailed guide to do the same with C++.

It is pretty simple to use, in case you have any queries, leave in comments below. So that was all folks, see you in next post! Till then, Happy Hacking! Meanwhile, get your signals across using SignalR :D

No comments:

Post a Comment

Outreachy experience and application tips

One of the best experiences of my student life was to make it to this list: