2017-08-10 179 views
0

在下面的代碼中,當我連接或斷開客戶端時,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 
+0

顯示您的客戶代碼 – Tester

+0

編輯並添加客戶代碼的問題。謝謝 – SomeOneMore

回答

0

有一天,我的MVC方法解僱了2倍(不signalr,我的網頁是)。

我研究,我發現這一點:

從佈局(母版)中刪除您的圖標(在HTML頭),如果它與404錯誤代碼來。如果它帶有404,請從瀏覽器的控制檯檢查您的圖標。

我不知道彼此之間是什麼關係。但這一天對我來說是個解決方案。

只是一個想法。

+0

Ÿ嘗試與其他項目和相同的行爲... – SomeOneMore

+0

1 idea> 0 idea –