Example #1
0
        public DiscordEventListener(DiscordWebSocket socket)
        {
            this.socket = socket;

            buffer      = new ArraySegment <byte>(new byte[10000]);
            serializer  = new JsonSerializer();
            CancelToken = new CancellationTokenSource();
        }
        public DiscordSocketClient(DiscordSocketConfig config = null) : base()
        {
            RequestLock = new object();

            if (config == null)
            {
                config = new DiscordSocketConfig();
            }

            Config      = new LockedSocketConfig(config);
            base.Config = Config;

            FinishConfig();

            if (Config.Cache)
            {
                GuildCache             = new ConcurrentDictionary <ulong, SocketGuild>();
                PrivateChannels        = new ConcurrentList <PrivateChannel>();
                Presences              = new ConcurrentDictionary <ulong, DiscordPresence>();
                VoiceStates            = new AutoConcurrentDictionary <ulong, DiscordVoiceStateContainer>((userId) => new DiscordVoiceStateContainer(userId));
                GuildSettings          = new ConcurrentDictionary <ulong, ClientGuildSettings>();
                PrivateChannelSettings = new List <DiscordChannelSettings>();
                ClientMembers          = new ConcurrentDictionary <ulong, GuildMember>();
            }

            WebSocket = new DiscordWebSocket <GatewayOpcode>($"wss://gateway.discord.gg/?v={Config.ApiVersion}&encoding=json");

            WebSocket.OnClosed += (s, args) =>
            {
                State = GatewayConnectionState.NotConnected;

                Reset();

                bool lostConnection = args.Code == 1006 || args.Code == 1001;

                if (lostConnection)
                {
                    Thread.Sleep(200);
                }

                GatewayCloseCode err = (GatewayCloseCode)args.Code;

                if (LoggedIn && (lostConnection || err == GatewayCloseCode.RateLimited || err == GatewayCloseCode.SessionTimedOut || err == GatewayCloseCode.UnknownError))
                {
                    Login(Token);
                }
                else
                {
                    OnLoggedOut?.Invoke(this, new LogoutEventArgs(err, args.Reason));
                }
            };

            WebSocket.OnMessageReceived += WebSocket_OnMessageReceived;

            VoiceClients = new VoiceClientDictionary(this);

            OnMediaServer += (s, e) =>
            {
                if (e.StreamKey == null)
                {
                    if (e.Guild == null)
                    {
                        VoiceClients.Private.SetServer(e);
                    }
                    else
                    {
                        VoiceClients[e.Guild.Id].SetServer(e);
                    }
                }
                else
                {
                    var key = new StreamKey(e.StreamKey);
                    VoiceClients[key.GuildId].Livestream.SetSessionServer(key.UserId, e);
                }
            };
        }
Example #3
0
        public DiscordSocketClient(DiscordSocketConfig config = null) : base()
        {
            RequestLock = new object();

            if (config == null)
            {
                config = new DiscordSocketConfig();
            }

            Config      = new LockedSocketConfig(config);
            base.Config = Config;

            FinishConfig();

            if (Config.Cache)
            {
                GuildCache             = new ConcurrentDictionary <ulong, SocketGuild>();
                PrivateChannels        = new ConcurrentList <PrivateChannel>();
                VoiceStates            = new AutoConcurrentDictionary <ulong, DiscordVoiceStateContainer>((userId) => new DiscordVoiceStateContainer(userId));
                GuildSettings          = new ConcurrentDictionary <ulong, ClientGuildSettings>();
                PrivateChannelSettings = new List <DiscordChannelSettings>();
            }

            VoiceSessions = new ConcurrentDictionary <ulong, DiscordVoiceSession>();
            Livestreams   = new ConcurrentDictionary <string, DiscordGoLiveSession>();

            WebSocket = new DiscordWebSocket <GatewayOpcode>($"wss://gateway.discord.gg/?v={Config.ApiVersion}&encoding=json");

            WebSocket.OnClosed += (s, args) =>
            {
                State = GatewayConnectionState.NotConnected;

                Reset();

                bool lostConnection = args.Code == 1006 || args.Code == 1001;

                if (lostConnection)
                {
                    Thread.Sleep(200);
                }

                GatewayCloseCode err = (GatewayCloseCode)args.Code;

                if (LoggedIn && (lostConnection || err == GatewayCloseCode.RateLimited || err == GatewayCloseCode.SessionTimedOut || err == GatewayCloseCode.UnknownError))
                {
                    Login(Token);
                }
                else
                {
                    OnLoggedOut?.Invoke(this, new LogoutEventArgs(err, args.Reason));
                }
            };

            WebSocket.OnMessageReceived += WebSocket_OnMessageReceived;

            #region media event handlers
            OnSessionVoiceState += (c, state) =>
            {
                lock (VoiceSessions.Lock)
                {
                    foreach (var session in VoiceSessions.Values)
                    {
                        if (session.GuildId == (state.Guild == null ? null : (ulong?)state.Guild.Id))
                        {
                            if (state.Channel == null || session.SessionId != state.SessionId)
                            {
                                session.Kill();
                            }
                            else if (state.SessionId == session.SessionId)
                            {
                                session.ChannelId = state.Channel.Id;
                            }

                            break;
                        }
                    }
                }
            };

            OnMediaServer += (c, args) =>
            {
                if (args.StreamKey == null)
                {
                    lock (VoiceSessions.Lock)
                    {
                        foreach (var session in VoiceSessions.Values)
                        {
                            if (args.GuildId == session.GuildId)
                            {
                                session.UpdateServer(args);

                                break;
                            }
                        }
                    }
                }
                else if (Livestreams.TryGetValue(args.StreamKey, out DiscordGoLiveSession session))
                {
                    args.GuildId = session.Guild.Id;
                    session.UpdateServer(args);

                    if (StreamKey.Deserialize(args.StreamKey).UserId == User.Id)
                    {
                        session.ParentSession.Livestream = session;
                    }
                    else
                    {
                        session.ParentSession.WatchingDictionary[args.StreamKey] = session;
                    }
                }
            };

            OnStreamUpdated += (c, update) =>
            {
                if (Livestreams.TryGetValue(update.StreamKey, out DiscordGoLiveSession session))
                {
                    session.Update(update);
                }
            };

            OnStreamDeleted += (c, delete) =>
            {
                if (Livestreams.TryGetValue(delete.StreamKey, out DiscordGoLiveSession session))
                {
                    Livestreams.Remove(delete.StreamKey);

                    if (delete.StreamKey.Split(':').Last() == User.Id.ToString())
                    {
                        session.ParentSession.Livestream = null;
                    }
                    else
                    {
                        session.ParentSession.WatchingDictionary.Remove(delete.StreamKey);
                    }

                    session.Disconnect(delete);
                }
            };
            #endregion
        }
Example #4
0
        public void Bind(IGatewayApiClient value)
        {
            _binder.Bind(value);

            _ws = new DiscordWebSocket(Logger, WebSocketClientFactory, UsesZLib);
        }