Esempio n. 1
0
        /// <summary>
        /// Reads the socket.
        /// </summary>
        /// <returns>Task&lt;System.String&gt;.</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());
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Sends the DISCONNECT message and Closes the websocket connection.
        /// </summary>
        /// <returns>&lt;System.Boolean&gt;.</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);
        }