示例#1
0
        private void MsgQueue_ClientEventReaded(object sender, ClientEvent e)
        {
            if (e.EventType == ClientEvent.Event.Connected)
            {
                Console.WriteLine(GetType().Name + " :Start authentication Id = " + e.Client.ClientId);
                _scsClientCount++;
                _authenticationStates.Add(e.Client, new UserAuthenticationState(e.Client));
            }


            // Disconnect
            if (e.EventType == ClientEvent.Event.Disconnected)
            {
                var  client = e.Client;
                User disconectedUser;
                if (_authenticatedUsers.TryGetValue(client, out disconectedUser))
                {
                    //_msgQueue.RemoveMessenger(e.Client);

                    //TODO оставить только один способ проброса события
                    //Бросаем событие уже после удаления из спаска. Все равно ему уже ничего не послать.
                    OnUserDisconnected?.Invoke(this, new UserEventArgs(disconectedUser));
                    disconectedUser.OnDisconnected();
                }

                _authenticatedUsers.Remove(client);
                _msgQueueForAutentification.RemoveMessenger(e.Client);
            }
        }
示例#2
0
        protected override void HandleMessage(DiscordWebSocketMessage <DiscordMediaOpcode> message)
        {
            if (message.Opcode == DiscordMediaOpcode.SSRCUpdate)
            {
                SSRCUpdate ssrc = message.Data.ToObject <SSRCUpdate>();

                if (!Viewers.Contains(ssrc.UserId))
                {
                    List <ulong> viewers = Viewers.ToList();
                    viewers.Add(ssrc.UserId);
                    Viewers = viewers;
                    OnUserConnected?.Invoke(this, ssrc.UserId);
                }
            }
            else if (message.Opcode == DiscordMediaOpcode.UserDisconnect)
            {
                ulong userId = message.Data.ToObject <JObject>().Value <ulong>("user_id");

                List <ulong> viewers = Viewers.ToList();
                if (viewers.Remove(userId))
                {
                    Viewers = viewers;
                }

                OnUserDisconnected?.Invoke(this, userId);
            }
        }
示例#3
0
        public static void UserDisconnected(object sender, EndPoint e)
        {
            OnUserDisconnected?.Invoke(null, e);

            if (UsersInfo.ContainsKey(e))
            {
                UsersInfo.Remove(e);
            }
        }
 /// <summary>
 /// Al desconectarse un canal
 /// </summary>
 /// <param name="sender">Emisor</param>
 /// <param name="e">Canal</param>
 private void Manager_OnChannelDisconnected(object sender, IChannel e)
 {
     lock (m_users) {
         User[] users = m_users.Where(u => u.Channel == e).ToArray();
         foreach (User user in users)
         {
             m_users.Remove(user);
             OnUserDisconnected?.Invoke(this, user);
         }
     }
 }
示例#5
0
        protected override void HandleMessage(DiscordWebSocketMessage <DiscordMediaOpcode> message)
        {
            switch (message.Opcode)
            {
            case DiscordMediaOpcode.Speaking:
                var state = message.Data.ToObject <DiscordSpeakingState>();

                if (state.UserId.HasValue)
                {
                    _ssrcToUserDictionary[state.SSRC] = state.UserId.Value;
                }
                else
                {
                    state.UserId = _ssrcToUserDictionary[state.SSRC];
                }

                break;

            case DiscordMediaOpcode.SSRCUpdate:     // this is fired whenever a user connects to the channel or updates their ssrc
                SSRCUpdate update = message.Data.ToObject <SSRCUpdate>();

                bool newUser = !_ssrcToUserDictionary.Values.Contains(update.UserId);

                _ssrcToUserDictionary[update.Audio] = update.UserId;

                if (newUser && OnUserConnected != null)
                {
                    Task.Run(() => OnUserConnected.Invoke(this, update.UserId));
                }
                break;

            case DiscordMediaOpcode.UserDisconnect:
                ulong userId = message.Data.ToObject <JObject>().Value <ulong>("user_id");

                if (_ssrcToUserDictionary.TryGetKey(userId, out uint ssrc))
                {
                    _ssrcToUserDictionary.Remove(ssrc);
                }

                if (_receivers.TryGetValue(userId, out IncomingVoiceStream receiver))
                {
                    receiver.Close();
                    _receivers.Remove(receiver.UserId);
                }

                if (OnUserDisconnected != null)
                {
                    Task.Run(() => OnUserDisconnected.Invoke(this, userId));
                }
                break;
            }
        }
示例#6
0
        /// <summary>
        /// User disconnected, remove from OnlineUsers and save all points
        /// </summary>
        /// <param name="username"></param>
        public void DisconnectUser(string username)
        {
            var disconnectingUser = FindOnlineUser(username);

            if (disconnectingUser == null)
            {
                return;
            }

            SaveData();
            // TODO: Pass object to remove method?
            OnlineUsers.RemoveAll(x => x.Username == username);
            OnUserDisconnected?.Invoke(this, new OnUserArgs {
                User = disconnectingUser
            });
            _logger.LogInformation("[DISCONNECT] {name} left the broadcast!", username);
        }
示例#7
0
        public async Task TraceConnectionMessages(TcpClient connection, string username, bool IsLocalConnection)
        {
            if (IsLocalConnection)
            {
                return;
            }

            var address = connection.Client.GetRemoteEndPointIpAddress(false);

            try
            {
                var connectionStream = connection.GetStream();
                var reader           = new StreamReader(connectionStream);
                while (connection.Connected)
                {
                    // this blocks until a message is received.
                    var message = await reader.ReadLineAsync();

                    if (string.IsNullOrEmpty(message))
                    {
                        return;
                    }

                    var normalizedMessage = GetNormalizedMessage(username, message);

                    OnMessageReceived?.Invoke(this, new MessageReceivedEventArgs()
                    {
                        normalizedMessage = normalizedMessage
                    });
                }
            }
            finally
            {
                OnUserDisconnected.Invoke(this, new UserDisconnectedEventArgs()
                {
                    username = username,
                    address  = address
                });
            }
        }
示例#8
0
 private void SetupEvents()
 {
     _usersManager.OnUserJoined       += (sender, e) => OnUserJoined?.Invoke(sender, e);
     _usersManager.OnUserDisconnected += (sender, e) => OnUserDisconnected?.Invoke(sender, e);
     _experienceManager.OnRankUp      += ExperienceManager_OnRankUp;
 }
示例#9
0
 public void Handle(UserDisconnected message) =>
 OnUserDisconnected?.Invoke(User.FromDTO(message.User));