在下面的代碼中,當我連接或斷開客戶端時,OnConnected和Disconnected事件觸發兩次。如何避免這種情況? 我有所有組件的最後一個版本。OnConnected和OnDisconnected事件觸發兩次?
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
顯示您的客戶代碼 – Tester
編輯並添加客戶代碼的問題。謝謝 – SomeOneMore