Beispiel #1
0
        /// <summary>
        /// Begins receiving data from a websocket.
        /// </summary>
        /// <param name="ws">The ws.</param>
        private async void BeginReceive(WebSocket ws)
        {
            try
            {
                foreach (var pendingMessage in ws.Streams())
                {
                    using (var reader = new StreamReader(await pendingMessage))
                    {
                        var obj = JsonObject.Parse(await reader.ReadToEndAsync());
                        switch (obj["type"])
                        {
                            case "hello": ConnectionState = ClientConnectionState.Established; break;
                            case "message": HandleMessage(obj); break;
                            default: break;
                        }
                    }
                }

            }
            catch (Exception e)
            {
                var err = Interlocked.CompareExchange(ref Error, null, null);
                if (err != null) err(this, e);
            }

            await CloseAsync();
        }