예제 #1
0
		internal virtual DataWebSocket CreateDataSocket()
		{
			var socket = new DataWebSocket(this);
			socket.Connected += (s, e) => 
			{
				if (_state == (int)DiscordClientState.Connecting)
					CompleteConnect(); }
			;
			socket.Disconnected += async (s, e) =>
			{
				RaiseDisconnected(e);
				if (e.WasUnexpected)
					await socket.Reconnect(_token).ConfigureAwait(false);
			};

			if (!_config.VoiceOnly)
			{
				socket.LogMessage += (s, e) => RaiseOnLog(e.Severity, LogMessageSource.DataWebSocket, e.Message);
				if (_config.LogLevel >= LogMessageSeverity.Info)
				{
					socket.Connected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.DataWebSocket, "Connected");
					socket.Disconnected += (s, e) => RaiseOnLog(LogMessageSeverity.Info, LogMessageSource.DataWebSocket, "Disconnected");
				}
			}

			socket.ReceivedEvent += async (s, e) => await OnReceivedEvent(e).ConfigureAwait(false);
			return socket;
		}
예제 #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();
		}