示例#1
0
        /// <summary>
        /// Отправляет сообщение на сервер.
        /// </summary>
        /// <param name="message">Сообщения для отправки.</param>
        /// <returns></returns>
        public void SendMessage(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException(nameof(message));
            }

            try
            {
                var buffer = Encoding.Default.GetBytes(message);
                _client.GetStream().Write(buffer, 0, buffer.Length);
                MessageSent?.Invoke(this, EventArgs.Empty);
            }
            catch (SocketException ex)
            {
                NetworkStreamError?.Invoke(this, new SingleMessageEventArgs(ex.Message));
            }
        }
示例#2
0
        /// <summary>
        /// Отправляет сообщение на сервер.
        /// </summary>
        /// <param name="message">Сообщения для отправки.</param>
        /// <param name="token">Токен отмены.</param>
        /// <returns></returns>
        public async ValueTask SendMessageAsync(string message, CancellationToken token)
        {
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentNullException(nameof(message));
            }

            try
            {
                var buffer = Encoding.Default.GetBytes(message);
                await _client.GetStream().WriteAsync(buffer, 0, buffer.Length, token);

                MessageSent?.Invoke(this, EventArgs.Empty);
            }
            catch (SocketException ex)
            {
                NetworkStreamError?.Invoke(this, new SingleMessageEventArgs(ex.Message));
            }
        }
示例#3
0
        /// <summary>
        /// Принимает сообщения от сервера.
        /// </summary>
        /// <param name="token">Токен отмены.</param>
        /// <returns></returns>
        public async ValueTask ReceiveMessagesAsync(CancellationToken token)
        {
            var messageBuilder = new StringBuilder();

            while (!token.IsCancellationRequested)
            {
                var stream = _client.GetStream();
                var buffer = new byte[256];
                messageBuilder.Clear();
                try
                {
                    do
                    {
                        var bytes = await stream.ReadAsync(buffer, 0, buffer.Length, token);

                        messageBuilder.Append(Encoding.Default.GetString(buffer, 0, bytes));
                    } while (stream.DataAvailable && !token.IsCancellationRequested);
                }
                catch (SocketException ex)
                {
                    NetworkStreamError?.Invoke(this, new SingleMessageEventArgs(ex.Message));
                    break;
                }

                if (token.IsCancellationRequested)
                {
                    break;
                }

                var message = messageBuilder.ToString();
                MessageReceived?.Invoke(this, new SingleMessageEventArgs(message));
                if (ServerShutdownMessage.Equals(message))
                {
                    ServerShutdown?.Invoke(this, EventArgs.Empty);
                }
            }
        }