断开连接后的SignalR连接

时间:2017-03-29 17:25:07

标签: vb.net signalr

亲爱的,我需要在断开连接后再次重新连接SingalR,以下代码是好的,还是有另一种方式:

Dim Proxy As IHubProxy
Dim Hub As HubConnection

Private Function ConnectToCallCenterHub()

    Try

        Hub = New HubConnection(CallCenterHubAddress)
        Proxy = Hub.CreateHubProxy("CallCenterHub")

        AddHandler Hub.StateChanged, (AddressOf HubConnectionState)

        Hub.Start()

        Catch ex As Exception
    End Try

End Function

Private Sub HubConnectionState(ByVal State As Microsoft.AspNet.SignalR.Client.StateChange)

    Try

        Invoke(New OnConnectionSateChangedDelegate(AddressOf OnConnectionSateChanged), State.NewState)

    Catch ex As Exception
    End Try

End Sub

Public Delegate Function OnConnectionSateChangedDelegate(ByVal State As Microsoft.AspNet.SignalR.Client.ConnectionState)
Public Function OnConnectionSateChanged(ByVal State As Microsoft.AspNet.SignalR.Client.ConnectionState)

    Try
        If State = Microsoft.AspNet.SignalR.Client.ConnectionState.Connected Then
            sbConnectionStatus.Text = "Connected"
            pbConnecting.Visible = False
        End If
        If State = Microsoft.AspNet.SignalR.Client.ConnectionState.Connecting Then
            sbConnectionStatus.Text = "Connecting"
        End If
        If State = Microsoft.AspNet.SignalR.Client.ConnectionState.Reconnecting Then
            sbConnectionStatus.Text = "Reconnecting"
        End If
        If State = Microsoft.AspNet.SignalR.Client.ConnectionState.Disconnected Then
            sbConnectionStatus.Text = "Disconnected"
            ConnectToCallCenterHub()
        End If

    Catch ex As Exception
    End Try

End Function

调用函数ConnectToCallCenterHub连接到集线器,如果断开连接,我再次调用它。

1 个答案:

答案 0 :(得分:1)

我没有在vb.net工作,但根据我对signalR的了解,要断开连接,你需要覆盖从HubBase派生的disconnect方法。

这是代码

public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
    {
        if(stopCalled==true)
        {
           //your code here
        }
        else
        {
           //your code here
        }
        return base.OnDisconnected(stopCalled);
    }

希望它对你有用。