Example #1
0
        public async Task AddUserConnectionId(string username, string connectionId, UserManager <ApplicationUser> userManager)
        {
            var semaphore = _lockDictionary.GetOrAdd(username, new SemaphoreSlim(1, 1));
            await semaphore.WaitAsync();

            try
            {
                if (IsUserOnline(username))
                {
                    _onlineUsers[username].ConnectionIds.Add(connectionId);
                }
                else
                {
                    var user = await userManager.FindByNameAsync(username);

                    var status = new OnlineUserStatus(username,
                                                      DateTimeOffset.UtcNow,
                                                      new HashSet <string> {
                        connectionId
                    },
                                                      user.RoomId,
                                                      new StopWatchWithOffset(user.TimeAccumulated),
                                                      user.Money,
                                                      user.MoneyAssignedCounter);
                    status.TimeAccumulated.Start();
                    AddUserStatus(username, status);
                }
            }
            finally
            {
                semaphore.Release();
            }
        }
Example #2
0
 private void AddUserStatus(string username, OnlineUserStatus userStatus)
 {
     if (!_onlineUsers.TryAdd(username, userStatus))
     {
         throw new InvalidOperationException($"Couldn't add {userStatus.Username} to the dictionary");
     }
 }