0

I have a few different WCF Services running. I would like to send one-way messages to clients who will be running IE 11. I would like to send the messages in the form of a class (ServiceName,Message,MessageDT), and then any clients listening to a particular Service would receive the message, and display it to user. I have viewed a few tutorials on SignalR and they all seem to be slightly different scenarios, like a chat application. Even the Stock Ticker example, has this random class that works on a timer to update stock prices. Its confusing. Does anybody have a good sample tutorial on how to do what I want to do?

David P
  • 2,027
  • 3
  • 15
  • 27

1 Answers1

0

Finally I was able to figure this out, and so I thought I would post the answer for those who are need.

First I created a class library (WMAppLib) with my message class:

Public Class VirtualConsoleMessage
    Private m_AppName As String
    Public Property AppName() As String
        Get
            Return m_AppName
        End Get
        Set(value As String)
            m_AppName = value
        End Set
    End Property
    Private m_MessageDT As DateTime
    Public Property MessageDT() As DateTime
        Get
            Return m_MessageDT
        End Get
        Set(value As DateTime)
            m_MessageDT = value
        End Set
    End Property
    Private m_Message As String
    Public Property Message() As String
        Get
            Return m_Message
        End Get
        Set(value As String)
            m_Message = value
        End Set
    End Property
End Class

Then in my Web Application, I added SignalR references, and also a reference to my class library. I then created a Hub:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System.Threading.Tasks;
using WMAppLib;

namespace WMApp_WebAdmin
{
    [HubName("VirtualConsoleMessageHub")]
    public class Virtual_ConsoleHub : Hub
    {
        public void AddSubscription(string AppName)
        {
            Groups.Add(Context.ConnectionId,AppName);
        }
        public void BroadcastMessage(VirtualConsoleMessage vcm)
        {
            string strClientMessage = vcm.MessageDT.ToString("MM/dd/yyyy hh:mm:ss tt") + " : " + vcm.Message;
            Clients.Group(vcm.AppName).newMessageReceived(strClientMessage);
        }
    }
}

My web page (VirtualConsole) has the following markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="VirtualConsole.aspx.cs" Inherits="WMApp_WebAdmin.VirtualConsole" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Virtual Console</title>
    <link href="/CSS/styleVC.css" type="text/css" rel="stylesheet" />
    <script src="/JS/jquery-1.11.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.signalR-2.2.0.min.js" type="text/javascript"></script>
    <script src="signalr/hubs"></script>
    <script src="/JS/VirtualConsole.js" type="text/javascript""></script>
</head>
<body>
    <form id="form1" runat="server">
        <div id="divConsole" runat="server">
        </div>
        <asp:HiddenField ID="AppName" runat="server" />
        <asp:HiddenField ID="Endpoint" runat="server" />
        <asp:HiddenField ID="AssemblyName" runat="server" />
        <asp:HiddenField ID="ClassName" runat="server" />
    </form>
</body>
</html>

My VirtualConsole.js has the following:

$(function () {
    var myVirtualConsoleHub = $.connection.VirtualConsoleMessageHub;
    myVirtualConsoleHub.client.newMessageReceived = function (message) {
        $("#divConsole").append("<p>" + message + "</p>");
    }
    $.connection.hub.url = "/signalr";
    $.connection.hub.start().done(function () {
        myVirtualConsoleHub.server.addSubscription($("#AppName").val());
    }).fail(function (error) {
        $("#divConsole").append("<p>" + error + "</p>");
    });;
});
$().ready(function () {
    var endpoint = $("#Endpoint").val();
});

In my WCF Service I added the SignalR, and the SignalR.Client. I also added a reference to my WMAppLib class library. In my main code I added two global variables:

Dim virtualConsoleHubConnection As HubConnection
Dim virtualConsoleHubProxy As IHubProxy

In My Public Sub New() I set these to values in my App.config:

virtualConsoleHubConnection = New HubConnection(ConfigurationManager.AppSettings("virtualConsoleHubURL"))
virtualConsoleHubProxy = virtualConsoleHubConnection.CreateHubProxy(ConfigurationManager.AppSettings("virtualConsoleHubName"))

These are set in the App.Config as:

<add key="virtualConsoleHubURL" value="http://localhost:49716/signalr/hubs"/>
<add key="virtualConsoleHubName" value="VirtualConsoleMessageHub"/>

Then I added one Private Sub to handle the broadcasting of all messages to the SignalR hub:

Private Sub WriteToVirtualConsole(ByVal vcm As VirtualConsoleMessage)
    Try
        virtualConsoleHubConnection.Start()
        virtualConsoleHubProxy.Invoke("BroadcastMessage", vcm)
    Catch ex As Exception
    Finally
        If virtualConsoleHubConnection.State = Microsoft.AspNet.SignalR.Client.ConnectionState.Connected Then
            virtualConsoleHubConnection.Stop()
        End If
    End Try
End Sub

Then anytime I do a Console.WriteLine I just call this sub:

WriteToVirtualConsole(New VirtualConsoleMessage With {.AppName = "MyWCFService", .MessageDT = Now, .Message = "I just did something"})

It then gets displayed on my WebPage Virtual Console page.

David P
  • 2,027
  • 3
  • 15
  • 27