private async Task Receive() { try { while (WebSocket.State == WebSocketState.Open) { var buffer = new byte[BufferSize]; var result = await WebSocket.ReceiveAsync(new ArraySegment <byte>(buffer), _objCancelationToken); if (result.MessageType == WebSocketMessageType.Close) { Disconnect(); } else { var lstMessage = new List <byte>(buffer); while (!result.EndOfMessage && result.CloseStatus == WebSocketCloseStatus.Empty) { result = await WebSocket.ReceiveAsync(new ArraySegment <byte>(buffer), _objCancelationToken); lstMessage.AddRange(buffer); } Rx?.Invoke(this, lstMessage.ToArray()); } } } catch (TaskCanceledException) { } catch (OperationCanceledException) { //ignore } catch (Exception ex) { Log.Info($"Error receiving data: {ex.Message}, closing connection"); Disconnect(); } }
/// <summary> /// ToDo: /// Test performance and ease of use from how it's done here: /// https://stackoverflow.com/a/41926694/2106514 /// /// </summary> public async void Start() { try { if (_objCancelationSource != null) { _objCancelationSource.Dispose(); } _objCancelationSource = new CancellationTokenSource(); _objCancelationToken = _objCancelationSource.Token; while (!_objCancelationToken.IsCancellationRequested) { var buffer = new byte[_intBufferSize]; var result = await _objClient.ReceiveAsync(new ArraySegment <byte>(buffer), _objCancelationToken); if (result.MessageType == WebSocketMessageType.Close) { throw new Exception("Client Disconnected"); } else { var arrMessage = new byte[result.Count]; Array.Copy(buffer, 0, arrMessage, 0, result.Count); while ( !result.EndOfMessage && ( result.CloseStatus == null || result.CloseStatus == WebSocketCloseStatus.Empty ) ) { result = await _objClient.ReceiveAsync(new ArraySegment <byte>(buffer), _objCancelationToken); Array.Resize(ref arrMessage, arrMessage.Length + result.Count); Array.Copy(buffer, 0, arrMessage, arrMessage.Length - result.Count, result.Count); } Rx?.Invoke(this, arrMessage); } } } catch (TaskCanceledException) { //ignore } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}, closing connection"); Disconnect(); } }
private void _objClient_OnMessageReceived(object sender, IMessageEventArgs e) { _objKeepAliveMonitor.Reset(); if (e.IsText) { Log.Debug($"Text received on {ID}"); var data = System.Text.Encoding.UTF8.GetBytes(e.Data); Rx?.Invoke(this, data); } else if (e.IsPing) { Log.Debug($"Ping received on {ID}"); } else if (e.IsBinary) { Log.Debug($"Data received on {ID}"); Rx?.Invoke(this, e.RawData); } }
private void Connection_OnMessage(object sender, IMessageEventArgs e) { _objKeepAliveMonitor.Reset(); if (e.IsText) { Log.Debug($"Text received on {ID}"); var data = System.Text.Encoding.UTF8.GetBytes(e.Data); Rx?.Invoke(this, data); } else if (e.IsPing) // -- do nothing, keep-alive is handled in this class { Log.Debug($"Ping received on {ID}"); } else if (e.IsBinary) { Log.Debug($"Data received on {ID}"); Rx?.Invoke(this, e.RawData); } }
private async Task Receive() { try { while (WebSocket.State == WebSocketState.Open) { var buffer = new byte[BufferSize]; var result = await WebSocket.ReceiveAsync(new ArraySegment <byte>(buffer), _objCancelationToken); if (result.MessageType == WebSocketMessageType.Close) { throw new Exception("Client Disconnected"); } else { var arrMessage = new byte[result.Count]; Array.Copy(buffer, 0, arrMessage, 0, result.Count); while ( !result.EndOfMessage && ( result.CloseStatus == null || result.CloseStatus == WebSocketCloseStatus.Empty ) ) { result = await WebSocket.ReceiveAsync(new ArraySegment <byte>(buffer), _objCancelationToken); Array.Resize(ref arrMessage, arrMessage.Length + result.Count); Array.Copy(buffer, 0, arrMessage, arrMessage.Length - result.Count, result.Count); } Rx?.Invoke(this, arrMessage); } } } catch (TaskCanceledException) { } catch (OperationCanceledException) { //ignore } catch (Exception ex) { Log.Info($"Error receiving data: {ex.Message}, closing connection"); } Disconnect(); }
/// <summary> /// Listens on the Receiver Queue for any messages /// /// When data is received the Rx event is raised /// </summary> private void StartListening(string pName) { var factory = new ConnectionFactory() { HostName = "test.crazyzone.be" }; var connection = factory.CreateConnection(); var channel = connection.CreateModel(); channel.ExchangeDeclare(exchange: QueueType.ToString(), type: "fanout"); channel.QueueDeclare(pName, false, false, !Shared); channel.QueueBind(pName, QueueType.ToString(), pName); var consumer = new EventingBasicConsumer(channel); consumer.Shutdown += Consumer_Shutdown; consumer.Received += (model, ea) => { Task.Run(() => { Rx?.Invoke(this, new RoutingFrame(ea.Body)); }); }; channel.BasicConsume(queue: pName, autoAck: true, consumer: consumer); }