예제 #1
0
        public AudioClient(AudioService service, int clientId, Server server, GatewaySocket gatewaySocket, Logger logger)
        {
            Service       = service;
            Id            = clientId;
            GatewaySocket = gatewaySocket;
            Logger        = logger;

            _connectionLock = new AsyncLock();

            _serializer = new JsonSerializer();
            _serializer.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
            _serializer.Error += (s, e) =>
            {
                e.ErrorContext.Handled = true;
                Logger.Error("Serialization Failed", e.ErrorContext.Error);
            };

            GatewaySocket.ReceivedDispatch += OnReceivedDispatch;

            VoiceSocket        = new VoiceWebSocket(service.Client, this, _serializer, logger);
            VoiceSocket.Server = server;

            /*_voiceSocket.Connected += (s, e) => RaiseVoiceConnected();
             *          _voiceSocket.Disconnected += async (s, e) =>
             *          {
             *                  _voiceSocket.CurrentServerId;
             *                  if (voiceServerId != null)
             *                          _gatewaySocket.SendLeaveVoice(voiceServerId.Value);
             *                  await _voiceSocket.Disconnect().ConfigureAwait(false);
             *                  RaiseVoiceDisconnected(socket.CurrentServerId.Value, e);
             *                  if (e.WasUnexpected)
             *                          await socket.Reconnect().ConfigureAwait(false);
             *          };*/

            /*_voiceSocket.IsSpeaking += (s, e) =>
             *          {
             *                  if (_voiceSocket.State == WebSocketState.Connected)
             *                  {
             *                          var user = _users[e.UserId, socket.CurrentServerId];
             *                          bool value = e.IsSpeaking;
             *                          if (user.IsSpeaking != value)
             *                          {
             *                                  user.IsSpeaking = value;
             *                                  var channel = _channels[_voiceSocket.CurrentChannelId];
             *                                  RaiseUserIsSpeaking(user, channel, value);
             *                                  if (Config.TrackActivity)
             *                                          user.UpdateActivity();
             *                          }
             *                  }
             *          };*/

            /*this.Connected += (s, e) =>
             *          {
             *                  _voiceSocket.ParentCancelToken = _cancelToken;
             *          };*/
        }
예제 #2
0
        /// <summary> Initializes a new instance of the DiscordClient class. </summary>
        public DiscordWSClient(DiscordWSClientConfig config = null)
        {
            _config = config ?? new DiscordWSClientConfig();
            _config.Lock();

            _state             = (int)DiscordClientState.Disconnected;
            _cancelToken       = new CancellationToken(true);
            _disconnectedEvent = new ManualResetEvent(true);
            _connectedEvent    = new ManualResetEventSlim(false);

            _dataSocket = CreateDataSocket();
            if (_config.EnableVoice)
            {
                _voiceSocket = CreateVoiceSocket();
            }
        }
예제 #3
0
        internal virtual VoiceWebSocket CreateVoiceSocket()
        {
            var socket = new VoiceWebSocket(this);

            socket.LogMessage   += (s, e) => RaiseOnLog(e.Severity, LogMessageSource.VoiceWebSocket, e.Message);
            socket.Connected    += (s, e) => RaiseVoiceConnected();
            socket.Disconnected += async(s, e) =>
            {
                RaiseVoiceDisconnected(socket.CurrentServerId, e);
                if (e.WasUnexpected)
                {
                    await socket.Reconnect().ConfigureAwait(false);
                }
            };
            if (_config.LogLevel >= LogMessageSeverity.Info)
            {
                socket.Connected    += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.VoiceWebSocket, "Connected");
                socket.Disconnected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.VoiceWebSocket, "Disconnected");
            }
            return(socket);
        }
        Task CreateVoiceWebSocket()
        {
            if (webSocket != null && webSocket.IsConnected)
            {
                throw new InvalidOperationException("[CreateVoiceWebSocket] webSocket must be null or disconnected!");
            }
            if (guildId == Snowflake.None)
            {
                throw new InvalidOperationException("[CreateVoiceWebSocket] guildId must be set!");
            }

            webSocket = new VoiceWebSocket($"VoiceWebSocket:{guildId}");
            webSocket.OnUnexpectedClose     += WebSocket_OnUnexpectedClose;
            webSocket.OnTimedOut            += WebSocket_OnTimedOut;
            webSocket.OnUserSpeaking        += WebSocket_OnUserSpeaking;
            webSocket.OnNewSessionRequested += WebSocket_OnNewSessionRequested;
            webSocket.OnResumeRequested     += WebSocket_OnResumeRequested;

            log.LogVerbose("[CreateVoiceWebSocket] Created VoiceWebSocket.");

            return(Task.CompletedTask);
        }