Sunday, March 31, 2013

A SignalR Hello World

I have some good news. Today I got SignalR working, at least minimally working in a hello world sort of way. In attempting to follow this example, I got nowhere. The only part of it that I ended up using was the upfront use of NuGet to get SignalR to begin with. In refactoring this, I have the following Global.asax.cs:

using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using ShockCaperShivers.Core;
using ShockCaperShivers.Infrastructure;
using ShockCaperShivers.SignalR;
using StructureMap;
namespace ShockCaperShivers
{
   public class WebApiApplication : System.Web.HttpApplication
   {
      protected void Application_Start()
      {
         ObjectFactory.Initialize(x =>
         {
            x.ForRequestedType<IClockRepository>
                  ().TheDefaultIsConcreteType<ClockRepository>();
            x.ForRequestedType<IHandRepository>
                  ().TheDefaultIsConcreteType<HandRepository>();
         });
         RouteTable.Routes.MapConnection<EchoConnection>(
            name: "signalservice",
            url: "/signal"
         );
         AreaRegistration.RegisterAllAreas();
         WebApiConfig.Register(GlobalConfiguration.Configuration);
         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
         RouteConfig.RegisterRoutes(RouteTable.Routes);
         BundleConfig.RegisterBundles(BundleTable.Bundles);
      }
   }
}

 
 

The new thing to see about the above is a reference to EchoConnection. EchoConnection is a new class I made in a new folder I made at the root of the UI called "SignalR." It subclasses PersistentConnection.

using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using ShockCaperShivers.Core;
using StructureMap;
namespace ShockCaperShivers.SignalR
{
   public class EchoConnection : PersistentConnection
   {
      protected override Task OnConnected(IRequest request, string connectionId)
      {
         IClockRepository clockRepository = ObjectFactory.
               GetInstance<IClockRepository>();
         Clock clock = clockRepository.RetrieveClock();
         Hand hand = new Hand();
         hand.HandAtHand = "paper";
         hand.IpIdentity = "1.2.3.4";
         hand.TimestampIdentity = clock.CurrentTime;
         hand.UniqueIdentity = Guid.NewGuid();
         IHandRepository handRepository = ObjectFactory.GetInstance<IHandRepository>();
         handRepository.AddHand(hand);
         return base.OnConnected(request, connectionId);
      }
   }
}

 
 

Now here is the magic. I can speak into the method above from a JavaScript function! Observe this view. Loading the view will pump a record into the database!

@{
   Layout = null;
}
<!DOCTYPE html>
<html>
<head>
   <title>Shock Caper Shivers</title>
</head>
   <body>
      @Styles.Render("~/Content/css")
      @Scripts.Render("~/bundles/modernizr")
      @Scripts.Render("~/bundles/jquery")
      @Scripts.Render("~/bundles/signalr")
      <script language="javascript">
         $(function () {
            var connection = $.connection("/signal");
            connection.start();
            connection.send("Hi there!");
         });
      </script>
   </body>
</html>

No comments:

Post a Comment