Beispiel #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;
			};*/
        }
		/// <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();
		}
		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;
		}