在以下代码中,当我连接或断开客户端时,事件OnConnected和Disconnected将触发两次。怎么避免这个? 我有所有组件的最后一个版本。
Imports Microsoft.AspNet.SignalR
Imports Microsoft.Owin.Cors
Imports Microsoft.Owin.Hosting
Imports Owin
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim url As String = "http://localhost:8080/arc/"
WebApp.Start(url)
End Sub
End Class
Class Startup
Public Sub Configuration(app As IAppBuilder)
app.UseCors(CorsOptions.AllowAll)
app.MapSignalR()
End Sub
End Class
Public Class MyHub
Inherits Hub
Public Sub Send(name As String, message As String)
Clients.All.addMessage(name, message)
End Sub
Public Overrides Function OnConnected() As Task
Debug.Print("Client connected: " + Context.ConnectionId)
Return MyBase.OnConnected()
End Function
Public Overrides Function OnDisconnected(stopCalled As Boolean) As Task
Debug.Print("Client DISconnected: " + Context.ConnectionId)
Return MyBase.OnDisconnected(True)
End Function
End Class
Vb.net中的客户端代码如下:
Imports Microsoft.AspNet.SignalR.Client
Imports System.ComponentModel
Imports Microsoft.AspNet.SignalR.Client.Hubs
Partial Public Class MainWindow
Inherits Window
Implements INotifyPropertyChanged
Public connection As HubConnection = New HubConnection("http://localhost:8080/arc/")
Public myHub As IHubProxy
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
DataContext = Me
connection = New HubConnection("http://localhost:8080/arc/")
myHub = connection.CreateHubProxy("myHub")
myHub.On(Of String)("addMessage", AddressOf addMessage)
End Sub
Async Sub btnShowSignal_Click(sender As Object, e As RoutedEventArgs)
Await connection.Start()
End Sub