Ejemplo n.º 1
0
        protected override async Task <bool> SendMessageAsync(Message message)
        {
            if (!connected)
            {
                throw new InvalidOperationException();
            }
            if (disposed > 0)
            {
                return(false);
            }
            byte[] buffer = message.ToArray();
            CancellationTokenSource source = new CancellationTokenSource(10000);

            try
            {
                await stream.WriteAsync(buffer, 0, buffer.Length, source.Token);

                return(true);
            }
            catch (ObjectDisposedException) { }
            catch (Exception ex) when(ex is IOException || ex is OperationCanceledException)
            {
                Disconnect(false);
            }
            finally
            {
                source.Dispose();
            }
            return(false);
        }
Ejemplo n.º 2
0
        protected override async Task <bool> SendMessageAsync(Message message)
        {
            if (!connected)
            {
                throw new InvalidOperationException();
            }
            if (disposed > 0)
            {
                return(false);
            }
            ArraySegment <byte>     segment = new ArraySegment <byte>(message.ToArray());
            CancellationTokenSource source  = new CancellationTokenSource(10000);

            try
            {
                await socket.SendAsync(segment, WebSocketMessageType.Binary, true, source.Token);

                return(true);
            }
            catch (ObjectDisposedException) { }
            catch (Exception ex) when(ex is WebSocketException || ex is OperationCanceledException)
            {
                Disconnect(false);
            }
            finally
            {
                source.Dispose();
            }
            return(false);
        }
Ejemplo n.º 3
0
 private bool SendMessage(Message message)
 {
     if (!connected)
     {
         throw new InvalidOperationException();
     }
     if (disposed > 0)
     {
         return(false);
     }
     byte[] buffer = message.ToArray();
     try
     {
         stream.Write(buffer, 0, buffer.Length);
         return(true);
     }
     catch (ObjectDisposedException) { }
     catch (IOException)
     {
         Disconnect(false);
     }
     return(false);
 }
Ejemplo n.º 4
0
 private async Task<bool> SendMessageAsync(Message message)
 {
     if (!connected)
         throw new InvalidOperationException();
     if (disposed > 0)
         return false;
     byte[] buffer = message.ToArray();
     try
     {
         await stream.WriteAsync(buffer, 0, buffer.Length);
         return true;
     }
     catch (ObjectDisposedException) { }
     catch (IOException)
     {
         Disconnect(true);
     }
     return false;
 }