我尝试与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
答案 0 :(得分:0)
您正在锁定对象&#34;连接&#34;并试图在SyncLock中更改它,这是不允许的。
使用空对象锁定SyncLock,它应该是公共的并且是共享的。 此外,您不应该使用&#34; OnConnect()&#34;事件因为它会在hub方法调用中每次触发
像这样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