1

我尝试与 SignalR ised 和 ASP.Net (VB) 聊天 运行时,与第一个用户完美配合,但与另一个用户登录时出现错误:

错误:溢出

请帮忙,我是实时应用程序的新手

Public Overrides Function OnConnected() As Task
        Dim myRoomId = "XXXX"
        Dim myUserId = IdentityUser.UserCode 
        SyncLock connections
            If Not connections.ContainsKey(myRoomId) Then
                connections(myRoomId) = New Dictionary(Of Integer, List(Of String))()
            End If
            If Not connections(myRoomId).ContainsKey(myUserId) Then ' <<<<<--- Error
                connections(myRoomId)(myUserId) = New List(Of String)()
            End If
            connections(myRoomId)(myUserId).Add(Me.Context.ConnectionId)
        End SyncLock
        Return MyBase.OnConnected()
End Function
4

1 回答 1

0

您正在锁定对象“连接”并尝试在不允许的 SyncLock 内更改它。

使用空对象锁定 SyncLock,它应该是公共的和共享的。此外,您不应使用“OnConnect()”事件,因为它会在每次调用集线器方法时触发

像这样

Private Shared _lock_connections As New Object

Public Overrides Function OnConnected() As Task
Dim myRoomId = "XXXX"
Dim myUserId = IdentityUser.UserCode
SyncLock _lock_connections
    If Not connections.ContainsKey(myRoomId) Then
        connections(myRoomId) = New Dictionary(Of Integer, List(Of String))()
    End If
    If Not connections(myRoomId).ContainsKey(myUserId) Then ' <<<<<--- Error
        connections(myRoomId)(myUserId) = New List(Of String)()
    End If
    connections(myRoomId)(myUserId).Add(Me.Context.ConnectionId)
End SyncLock
Return MyBase.OnConnected()
End Function
于 2016-06-13T22:35:25.400 回答