Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
        private void Connection_OnMessage(DiscordMediaConnection connection, 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;
                }
                break;

            case DiscordMediaOpcode.SSRCUpdate:
                SSRCUpdate update = message.Data.ToObject <SSRCUpdate>();
                _ssrcToUserDictionary[update.Audio] = 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);
                }
                break;
            }
        }
Ejemplo n.º 3
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;
            }
        }