Wednesday, April 3, 2013

Deserialize with Newtonsoft

using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ShockCaperShivers.Core;
using StructureMap;
namespace ShockCaperShivers.SignalR
{
   public class EchoConnection : PersistentConnection
   {
      protected override Task OnConnected(IRequest request, string connectionId)
      {
         return base.OnConnected(request, connectionId);
      }
      
      protected override Task OnReceived(IRequest request, string connectionId,
            string data)
      {
         var json = (JObject)JsonConvert.DeserializeObject(data);
         IClockRepository clockRepository = ObjectFactory
               .GetInstance<IClockRepository>();
         Clock clock = clockRepository.RetrieveClock();
         Hand hand = new Hand();
         hand.HandAtHand = json["HandAtHand"].ToString();
         hand.IpIdentity = json["IpIdentity"].ToString();
         hand.TimestampIdentity = clock.CurrentTime;
         hand.UniqueIdentity = Guid.NewGuid();
         IHandRepository handRepository = ObjectFactory.GetInstance<IHandRepository>();
         handRepository.AddHand(hand);
         return this.Connection.Broadcast(data);
      }
   }
}

 
 

What is above is part of a first attempt at SignalR I've been building as detailed here and here. My Controller looks like so:

using ShockCaperShivers.Models;
namespace ShockCaperShivers.Controllers
{
   public class HomeController : Controller
   {
      public ActionResult Index()
      {
         string nameOfTheComputer = System.Net.Dns.GetHostName();
         string localIp = System.Net.Dns
               .GetHostByName(nameOfTheComputer).AddressList[0].ToString();
         string publicIpv4 = Request.ServerVariables["REMOTE_ADDR"];
         string publicIpv6 = System.Net.Dns
               .GetHostEntry(nameOfTheComputer).AddressList[0].ToString();
         Ip ip = new Ip();
         if (localIp == publicIpv6)
         {
            ip.Address = publicIpv4;
         } else {
            ip.Address = publicIpv6;
         }
         return View(ip);
      }
   }
}

 
 

My model like so:

namespace ShockCaperShivers.Models
{
   public class Ip
   {
      public string Address { get; set; }
   }
}

 
 

My view like so:

@model ShockCaperShivers.Models.Ip
@{
   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")
      @Html.Hidden("ip", Model.Address)
      <div id="rock" class="hand">rock</div>
      <div id="paper" class="hand">paper</div>
      <div id="scissors" class="hand">scissors</div>
      <script language="javascript">
         $(function () {
            var connection = $.connection("/signal");
            connection.start();
            $('.hand').bind('click', function () {
               var hand = {
                  HandAtHand: $(this).attr("id"),
                  IpIdentity: $('#ip').val()
               };
               var string = JSON.stringify(hand);
               connection.send(string);
            });
         });
      </script>
   </body>
</html>

No comments:

Post a Comment