Saturday, November 2, 2013

SignalR 2.0 VB.NET Push Notification

Many readers have had difficulty with the post SignalR VB.NET Push Notification as they have installed the latest version 2.0. We will require some changes to the old post to make it work.

"..No more RouteTable.Routes.MapConnection but the new Owin MapSignalR..."
Let us review the steps...


Step 1


Install gituhub Package from Package Manager Console

PM> Install-Package Microsoft.AspNet.SignalR

Step 2


Add a new Class to project and create a PersistantConnection:

Imports System.Web
Imports System.Threading.Tasks
Imports Microsoft.AspNet.SignalR

Public Class NotificationConnection
  Inherits PersistentConnection
   Protected Overrides Function OnConnected(request As IRequest, connectionId As String) As System.Threading.Tasks.Task
      Return MyBase.OnConnected(request, connectionId)
   End Function
End Class

Step 3

Add our class for notifications (for simplicity we create note with a value and a time)

Imports Microsoft.AspNet.SignalR

Public Class Notification
   Private _ExamValue As Int32
   Private _Data As String
   Public Property ExamValue As Int32
      Get
         Return _ExamValue
      End Get
      Set(value As Int32)
         _ExamValue = value
      End Set
   End Property
   Public Property Data As String
      Get
         Return _Data
      End Get
      Set(value As String)
         _Data = value
      End Set
   End Property
End Class



Step 4 (Here is the update!!!)

No longer use the file Global.asax.vb. We add a new class for mapping connection. 
No more RouteTable.Routes.MapConnection but the new Owin MapSignalR

Imports Owin
Imports Microsoft.AspNet.SignalR
Imports Microsoft.AspNet.SignalR.Infrastructure
Imports System.Threading
Imports System.Threading.Tasks

<Assembly: Microsoft.Owin.OwinStartup(GetType(SignalR.Startup))> 

Public Class Startup
   Public Sub Configuration(app As IAppBuilder)
      app.MapSignalR(Of NotificationConnection)("/NotificationConnection")
      Task.Factory.StartNew(Function()
                               Dim connectonManager As IConnectionManager = GlobalHost.DependencyResolver.Resolve(Of IConnectionManager)()
                               Dim connection = connectonManager.GetConnectionContext(Of NotificationConnection)()



                               While True
                                  Dim item = New Notification() With { _
                                 .Data = DateTime.Now.ToString("hh:mm:ss"), _
                                 .ExamValue = CInt(Math.Ceiling(Rnd() * 100))
                              }

                                  connection.Connection.Broadcast(item)

                                  Thread.Sleep(1000)
                               End While
                            End Function)
   End Sub
End Class


Step 5 (also here a small change !!!)

In html page we have to change the reference to the old script with the new one for SignalR.

<script src="scripts/jquery-1.6.4.js" type="text/javascript"></script>
<script src="scripts/json2.js" type="text/javascript"></script>
<!--<script src="scripts/jquery.signalR-1.1.1.js" type="text/javascript"></script>-->
<script src="Scripts/jquery.signalR-2.0.0.min.js"></script>
<script type="text/javascript">
    var output;
    $(document).ready(function () {

        output = $("#output");

    });

    var data;
    var options;
    connection = $.connection('/NotificationConnection');
    connection.received(function (dataItem) {
        var newItem = dataItem;
        output.text("Received exam value: " + newItem.Data + "," + newItem.ExamValue);
    });

    connection.start();
</script>
<body>
    <h2>Laboratory Exams Monitoring</h2>
    <div id="output"></div>
</body>

No comments :

Post a Comment