/// <summary> /// Reads the socket. /// </summary> /// <returns>Task<System.String>.</returns> private async Task <string> ReadSocketAsync() { var segment = new ArraySegment <byte>(new byte[8192]); // 8Kb using (var stream = new MemoryStream()) { WebSocketReceiveResult result; do { result = await ClientWs.ReceiveAsync(segment, CancellationToken.None); switch (result.MessageType) { case WebSocketMessageType.Text: stream.Write(segment.Array, segment.Offset, result.Count); break; case WebSocketMessageType.Close: await ClientWs.CloseAsync(WebSocketCloseStatus.NormalClosure, DONE, CancellationToken.None); break; case WebSocketMessageType.Binary: Debug.WriteLine("Received Unsupported Binary Websocket message: " + segment.ToString()); break; } }while (ClientWs.State == WebSocketState.Open && !result.EndOfMessage); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream)) return(reader.ReadToEnd()); } }
/// <summary> /// Sends the DISCONNECT message and Closes the websocket connection. /// </summary> /// <returns><System.Boolean>.</returns> public async Task <bool> DisconnectAsync() { //// Send DISCONNECT, does not seem to be needed //StompMessage disconnect = new StompMessage(StompMessage.ClientCommands.DISCONNECT); //await SendAsync(disconnect); // Close the Websocket await ClientWs.CloseAsync(WebSocketCloseStatus.NormalClosure, DONE, CancellationToken.None); if (ClientWs.State != WebSocketState.Open) { return(true); } return(false); }