通过 SignalR 的 WCF SSE

WCF SSE via SignalR

我有几个不同的 WCF 服务 运行。我想向 运行 IE 11 的客户端发送单向消息。我想以 class (ServiceName,Message,MessageDT) 的形式发送消息,然后发送任何收听特定服务的客户端将收到消息,并将其显示给用户。我看过一些关于 SignalR 的教程,它们似乎都是略有不同的场景,比如聊天应用程序。即使是 Stock Ticker 示例,也有这个随机 class 可以在计时器上工作以更新股票价格。令人困惑。有人有关于如何做我想做的事情的好的示例教程吗?

我终于弄明白了,所以我想我会 post 为有需要的人提供答案。

首先,我用我的消息 class:

创建了一个 class 库 (WMAppLib)
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

然后在我的 Web 应用程序中,我添加了 SignalR 引用,以及对我的 class 库的引用。然后我创建了一个集线器:

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);
        }
    }
}

我的网页 (VirtualConsole) 具有以下标记:

<%@ 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>

我的 VirtualConsole.js 有以下内容:

$(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();
});

在我的 WCF 服务中,我添加了 SignalR 和 SignalR.Client。我还添加了对我的 WMAppLib class 库的引用。在我的主代码中,我添加了两个全局变量:

Dim virtualConsoleHubConnection As HubConnection
Dim virtualConsoleHubProxy As IHubProxy

在我的 Public Sub New() 中,我将这些设置为 App.config 中的值:

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

这些在App.Config中设置为:

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

然后我添加了一个 Private Sub 来处理所有消息到 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

然后任何时候我做一个 Console.WriteLine 我就叫这个子:

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

然后它会显示在我的 WebPage 虚拟控制台页面上。