SignalR将数据服务器推送到客户端

时间:2013-12-10 17:56:55

标签: c# asp.net vb.net signalr

我有2个单独的服务器,一个用于WebApp,另一个用于SignalR(IIS8 / WinSvr12)

客户端(浏览器)连接到WebApp服务器以请求网页,并且还与单独的SignalR服务器保持持久连接。

我已经设置好并且工作正常。

我需要定期将BLL(托管在WebApp服务器上)推送到通过SignalR服务器连接的客户端。

我在BLL中创建了一个简单的.NET SignalR客户端类,用于将数据推送到客户端。

我发现每次从WebApp服务器向SignalR服务器发送数据时都需要建立连接。

是否有人认为在静态变量中维护连接/代理有什么问题,以便可以共享它?

这是我的代码:

Public Class ChatHub

    Private Shared Property Connection As HubConnection 'the connection is shared across all instances
    Private Shared Property Proxy As IHubProxy 'the proxy is shared across all instances

    ''' <summary>
    ''' Establishes an unauthenticated connection to the SignalR server.
    ''' </summary>
    Public Sub New()

        'set connection to server:
        If IsNothing(Connection) Then
            Connection = New HubConnection("http://myserver/signalr")
        End If

        'define hub proxy to connect to:
        If IsNothing(Proxy) Then
            Proxy = Connection.CreateHubProxy("chatHub")
        End If

    End Sub

    ''' <summary>
    ''' Sends a message asynchronously. 
    ''' </summary>
    Public Async Sub SendMessageAsync(message As String)

        'establish connection:
        If Connection.State = ConnectionState.Disconnected Then

            'Two way communication is not required as we are only pushing data to the signalR server.
            'Therefore we should use ServerSentEventsTransport as it is more efficient than websockets.
            'Explicitly setting the transport type speeds up connection - otherwise connection transport is auto detected:
            Await Connection.Start(New ServerSentEventsTransport())

        End If

        'send message:
        If Connection.State = ConnectionState.Connected Then
            Await Proxy.Invoke("send", message)
        End If

    End Sub

End Class

然后我从BLL中的各种函数中使用类似的类。

Dim chatHubDemo As New BLL.SignalR.ChatHub()
chatHubDemo.SendMessageAsync(message)

注意:连接和代理变量是“共享”(静态),因此每次使用该类时都不需要建立连接。

一切正常,连接正在按预期维护和重复使用。

我找不到任何详细说明如何维护从网络服务器到signalR的连接的文档以及这样做的最佳做法。

是否有人发现使用静态保持持久连接可能会出现任何问题?

1 个答案:

答案 0 :(得分:2)

这是我最后写的,它运作得很好。

它允许我通过从Web服务器业务逻辑发起请求将数据推送到客户端(使用signalr .net客户端):

Imports Microsoft.AspNet.SignalR.Client
Imports Microsoft.AspNet.SignalR.Client.Hubs
Imports Microsoft.AspNet.SignalR.Client.Transports
Imports System.Threading.Tasks


''' <summary>
''' Methods and properties that relate to the Communication Hub.
''' Use to push data from the Server to the Client via SignalR.
''' </summary>
''' <remarks>
''' We are using shared variables, we can help prevent race conditions by initiating this class by calling the StartUp method from the application_start event
''' </remarks>
Public Class CommHub

    Private Shared Property Connection As HubConnection
    Private Shared Property Proxy As IHubProxy
    Private Shared ReadOnly _connectionLock As Object = New Object

    Private Sub New()
    End Sub

    ''' <summary>
    ''' Start up the connection to the realtime communication servers.
    ''' Call on application_start event.
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub Startup()
        Task.Run(Async Function()
                     'async must be run on a new thread:
                     Dim commHub As New CommHub
                     Await commHub.ConnectAsync()
                 End Function)
    End Sub


    ''' <summary>
    ''' Establishes an authenticated connection to the SignalR server.
    ''' </summary>
    ''' <remarks></remarks>
    Private Async Function ConnectAsync() As Tasks.Task

        'set connection to server:
        If IsNothing(Connection) Then
            SyncLock _connectionLock
                If IsNothing(Connection) Then

                    Dim realtimeUrl As String = "http://myserver/signalr"

                    Connection = New HubConnection(realtimeUrl)

                    'OPTIONAL:
                    'add custom headers:
                    'Connection.Headers.Add("headername", "headervalue")

                    'OPTIONAL:
                    'get realtime authentication cookie with permission to communicate with the realtime servers:
                    'Dim authCookie As Net.Cookie = Code_to_get_your_auth_cookie
                    'Connection.CookieContainer = New Net.CookieContainer
                    'Connection.CookieContainer.Add(authCookie)

                    'define hub proxy to connect to:
                    Proxy = Connection.CreateHubProxy("commHub")

                End If
            End SyncLock
        End If

        If Not IsNothing(Connection) Then
            Dim myConnection As HubConnection = Connection ' get pointer reference to shared object
            If myConnection.State = ConnectionState.Disconnected Then
                Try
                    'Two way communication is not required as we are only pushing data to the signalR server.
                    'Therefore we should use ServerSentEventsTransport as it is more efficient than websockets.
                    'Explicitly setting the transport type speeds up connection - otherwise connection transport is auto detected:
                    Await myConnection.Start(New ServerSentEventsTransport())

                Catch ex As Exception
                    'catch multiple connection attempts
                    'do nothing
                End Try
            End If
        End If

    End Function




    Public Enum CounterTypes As Integer
        Messages
        Requests
        Notifications
    End Enum

    ''' <summary>
    ''' Updates the counter on the specified client in realtime.
    ''' </summary>
    ''' <param name="mbrID">The unique id of the client.</param>
    ''' <param name="counterType">The type of counter.</param>
    ''' <param name="value">The value of the counter.</param>
    ''' <remarks></remarks>
    Friend Shared Async Sub UpdateCounterAsync(mbrID As Integer, counterType As CounterTypes, value As Integer)

        're-establish connection if it has been dropped 
        'otherwise reuses the existing connection 
        Dim commHub As New CommHub
        Await commHub.ConnectAsync()

        'call server side method:
        If Not IsNothing(Connection) Then
            Dim myConnection As HubConnection = Connection 'get pointer reference to shared object
            If myConnection.State = ConnectionState.Connected Then

                Dim myProxy As IHubProxy = Proxy 'get pointer reference to shared object
                If Not IsNothing(myProxy) Then

                    'invoke method on signalR server:
                    Await myProxy.Invoke("UpdateCounter", mbrID, counterType.ToString, value)

                End If

            End If
        End If

    End Sub

End Class

然后,我可以通过简单地调用我的BLL中的方法将数据推送到客户端:

Await CommHub.UpdateCounterAsync(12731, CommHub.CounterTypes.Messages, 10)

例如:用户ID 12731有10条新消息

请记住从application_start事件中调用StartUp方法,如下所示:

CommHub.StartUp();

这有助于在启动时建立与服务器的连接来防止竞争条件。