gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:01:55 25 4
gpt4 key购买 nike

我有 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)

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

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

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

有没有人看到使用静态维护持久连接可能出现的任何问题?

最佳答案

这是我最终写的,效果很好。

它允许我通过从 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();

这将通过在启动时建立与服务器的连接来帮助防止竞争条件。

关于c# - SignalR 将数据服务器推送到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20501836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com