/// <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> /// Opens the websocket connection and sends the CONNECT message. /// </summary> /// <returns><System.Boolean> true if successful.</returns> public async Task <bool> ConnectAsync(Action <string> errorsEventHandler) { const string WEBSOCKETPATH = "/w/messages/websocket"; // Based on https://docs.nem.io/en/nem-dev-basics-docker/blockchain-monitoring await ClientWs.ConnectAsync(new Uri(string.Concat("ws://", Domain, ":", Port, WEBSOCKETPATH)), CancellationToken.None); if (ClientWs.State != WebSocketState.Open) { return(false); } // Send CONNECT StompMessage connect = new StompMessage(StompMessage.ClientCommands.CONNECT); // Set message headers, might not be needed //connect["accept-version"] = "1.1,1.0"; //connect["heart-beat"] = "10000, 10000"; // out, in await SendAsync(connect); // Read the answer from the server StompMessage connectedMsg = StompMessage.Deserialize(await ReadSocketAsync()); if (connectedMsg.Command != StompMessage.ServerResponses.CONNECTED) { return(false); } // Subscribe to errors channel await SubscribeToErrorsAsync(errorsEventHandler); // Start the Loop to read answers Task loop = LoopReadStompMsgsAsync(); // Explicitely not using await, to allow the Loop to run asynchronously without waiting for it to complete! return(true); }
/// <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); }
/// <summary> /// Sends the StompMessage Asynchronously. Call LoopReadStompMsgsAsync to process answers from the server. /// </summary> /// <param name="stompMsg"></param> async Task SendAsync(StompMessage stompMsg) { byte[] encoded = Encoding.UTF8.GetBytes(stompMsg.Serialize()); ArraySegment <byte> segment = new ArraySegment <byte>(encoded, 0, encoded.Length); await ClientWs.SendAsync(segment, WebSocketMessageType.Text, true, CancellationToken.None); }