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); }
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); }
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); }
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; }