示例#1
0
        protected async Task HandleResults()
        {
            var buffer = new byte[1024];

            while (true)
            {
                var segment = new ArraySegment <byte>(buffer);

                var result = await BaseClient.ReceiveAsync(segment, CancellationToken.None);

                if (result.MessageType == WebSocketMessageType.Close)
                {
                    return;
                }

                int count = result.Count;
                while (!result.EndOfMessage)
                {
                    if (count >= buffer.Length)
                    {
                        await BaseClient.CloseAsync(WebSocketCloseStatus.InvalidPayloadData, "That's too long", CancellationToken.None);

                        return;
                    }

                    segment = new ArraySegment <byte>(buffer, count, buffer.Length - count);
                    result  = await BaseClient.ReceiveAsync(segment, CancellationToken.None);

                    count += result.Count;
                }

                var message = Encoding.UTF8.GetString(buffer, 0, count);

                // you'll probably want to parse the JSON into a useful object here,
                // see ServiceState and IsDelimeter for a light-weight example of that.
                if (IsDelimeter(message))
                {
                    return;
                }
                else
                {
                    OnMessage(message);
                }
            }
        }
示例#2
0
        public override void Send(FileStream file, string openingMessage)
        {
            // connect the websocket
            Action connectAction = () => BaseClient.ConnectAsync(UriBuilder.Uri, CancellationToken.None).Wait();

            // send opening message and wait for initial delimeter
            Action <ArraySegment <byte> > openAction = (message) => Task.WaitAll(BaseClient.SendAsync(message, WebSocketMessageType.Text, true, CancellationToken.None), HandleResults());

            // send all audio and then a closing message; simltaneously print all results until delimeter is recieved
            Action sendAction = () => Task.WaitAll(SendAudio(file), HandleResults());

            // close down the websocket
            Action closeAction = () => BaseClient.CloseAsync(WebSocketCloseStatus.NormalClosure, "Close", CancellationToken.None).Wait();

            ArraySegment <byte> openMessage = new ArraySegment <byte>(Encoding.UTF8.GetBytes(openingMessage));

            Task.Factory.StartNew(() => connectAction())
            .ContinueWith((antecedent) =>
            {
                if (antecedent.Status == TaskStatus.Faulted)
                {
                    if (antecedent.Exception != null)
                    {
                        OnError(antecedent.Exception.InnerException);
                    }
                }
            })
            .ContinueWith((antecedent) => openAction(openMessage), TaskContinuationOptions.OnlyOnRanToCompletion)
            .ContinueWith((antecedent) =>
            {
                if (antecedent.Status == TaskStatus.Faulted)
                {
                    if (antecedent.Exception != null)
                    {
                        OnError(antecedent.Exception.InnerException);
                    }
                }
            })
            .ContinueWith((antecedent) => sendAction(), TaskContinuationOptions.OnlyOnRanToCompletion)
            .ContinueWith((antecedent) =>
            {
                if (antecedent.Status == TaskStatus.Faulted)
                {
                    if (antecedent.Exception != null)
                    {
                        OnError(antecedent.Exception.InnerException);
                    }
                }
            })
            .ContinueWith((antecedent) => closeAction(), TaskContinuationOptions.OnlyOnRanToCompletion)
            .ContinueWith((antecedent) =>
            {
                if (antecedent.Status == TaskStatus.Faulted)
                {
                    if (antecedent.Exception != null)
                    {
                        OnError(antecedent.Exception.InnerException);
                    }
                }
            })
            .Wait();
        }