private void _webSocket_OnSendFailed(string data, Exception ex) { Task.Run(() => { OnSendFailed?.Invoke(data, ex); }); if (Debug) { WriteLine($"Send Failed, Data: {data}, Exception: {ex.Message}"); } }
private void StartSender() { Log($"Starting sender. {_ws.State}"); _senderTask = Task.Run(async() => { Log("Entering sender loop."); _senderRunning = true; try { while (!_reconnecting && _ws.State != WebSocketState.Aborted) { if (_ws.State == WebSocketState.Open && !_reconnecting) { if (SendQueueLength == 0) { continue; } var msg = _sendQueue.Take(_tokenSource.Token); if (msg.Key.Add(_options.SendCacheItemTimeout) < DateTime.UtcNow) { Log($"Message expired skipping: {msg.Key} {msg.Value}."); continue; } var buffer = Encoding.UTF8.GetBytes(msg.Value); try { Log($"Sending message: {msg.Key} {msg.Value}."); await _ws.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, _tokenSource.Token).ConfigureAwait(false); } catch (Exception ex) { Log($"Sender threw sending exception: {ex.Message}."); // Most likely socket error OnSendFailed?.Invoke(msg.Value, ex); _ws.Abort(); break; } } // limit to N ms per iteration Thread.Sleep(_options.SendDelay); } } catch (Exception ex) { Log($"Sender threw exception: {ex.Message}."); OnSendFailed?.Invoke("", ex); OnError?.Invoke(ex); } _senderRunning = false; Log($"Exiting sender. { _ws.State}"); return(Task.CompletedTask); }); }
private void StartSender() { _senderTask = Task.Run(async() => { _senderRunning = true; try { while (!_disposedValue && !_reconnecting) { if (_ws.State == WebSocketState.Open && !_reconnecting) { var msg = _sendQueue.Take(_tokenSource.Token); if (msg.Key.Add(SendCacheItemTimeout) < DateTime.UtcNow) { continue; } var buffer = Encoding.UTF8.GetBytes(msg.Value); try { await _ws.SendAsync(new ArraySegment <byte>(buffer), WebSocketMessageType.Text, true, _tokenSource.Token); } catch (Exception ex) { // Most likely socket error OnSendFailed?.Invoke(msg.Value, ex); _ws.Abort(); break; } } // limit to N ms per iteration Thread.Sleep(SendDelay); } } catch (Exception ex) { OnSendFailed?.Invoke("", ex); OnError?.Invoke(ex); } _senderRunning = false; return(Task.CompletedTask); }); }
public void SendFailed(OnSendFailedEventArgs eventArgs) { OnSendFailed?.Invoke(this, eventArgs); }
private void On_SendFailed(string data, Exception ex) => OnSendFailed?.Invoke(data, ex);
private void _scClient_OnSendFailed(string data, Exception ex) => OnSendFailed?.Invoke(data, ex);
private void Socket_OnSendFailed(object sender, string data, Exception ex) { Log($"Send failed: Ex: {ex.Message}, Data: {data}"); OnSendFailed?.Invoke(this, data, ex); }