Ejemplo n.º 1
0
        public override string Speak()
        {
            base.Speak();
            switch (_currState)
            {
            case JokeState.KnockKnock1:
                ConversationIsOver = false;
                _currState         = JokeState.KnockKnock2;
                return("Knock knock");

            case JokeState.KnockKnock2:
                ConversationIsOver = false;
                _currState         = JokeState.KnockKnock3;
                return("Cash");

            case JokeState.KnockKnock3:
                ConversationIsOver = true;
                return("No thanks, but I’d like some peanuts!");

            default:
                ConversationIsOver = true;
                return("I forgot what joke I was telling!");
            }
        }
Ejemplo n.º 2
0
        private async Task TellJokes(HttpContext context, WebSocket webSocket)
        {
            JokeModel[] jokes     = LoadJokes();
            Random      rand      = new Random();
            int         jokeIndex = rand.Next(jokes.Length);

            ArraySegment <byte> bytesToSend = new ArraySegment <byte>();

            byte[] readBuffer = new byte[1024];
            string clientResponse;

            JokeState jokeState = JokeState.None;

            // send knock knock
            string kk = "Knock, knock.";

            // send greeting
            bytesToSend = StringToBytes("Hi there, prepare yourself for hilarious jokes!");

            // send over greeting message, endOfMessage is true will force the client to read this right away
            await webSocket.SendAsync(
                bytesToSend,
                WebSocketMessageType.Text,
                true,
                CancellationToken.None
                );

            WebSocketReceiveResult result;
            int bytesReceived = 1024;

            // while they don't close
            while (true)
            {
                // cleanse the buffer
                for (int i = 0; i < bytesReceived; i++)
                {
                    byte.TryParse("\0", out readBuffer[i]);
                }

                // await whose there
                result = await webSocket.ReceiveAsync(
                    new ArraySegment <byte>(readBuffer),
                    CancellationToken.None
                    );

                bytesReceived = result.Count < 1024 ? result.Count : 1024;

                if (result.CloseStatus.HasValue)
                {
                    break;
                }

                // send setup
                clientResponse = Encoding.ASCII.GetString(readBuffer).Trim('\0');

                jokeState = GetJokeState(clientResponse);

                switch (jokeState)
                {
                case JokeState.KnockKnock:
                    bytesToSend = StringToBytes(kk);

                    await webSocket.SendAsync(
                        bytesToSend,
                        WebSocketMessageType.Text,
                        true,
                        CancellationToken.None
                        );

                    break;

                case JokeState.Who:
                    bytesToSend = StringToBytes(jokes[jokeIndex].Who);

                    await webSocket.SendAsync(
                        bytesToSend,
                        WebSocketMessageType.Text,
                        true,
                        CancellationToken.None
                        );

                    break;

                case JokeState.Punchline:
                    bytesToSend = StringToBytes(jokes[jokeIndex].Punchline);

                    jokeIndex = rand.Next(jokes.Length);

                    await webSocket.SendAsync(
                        bytesToSend,
                        WebSocketMessageType.Text,
                        true,
                        CancellationToken.None
                        );

                    break;

                default:
                    bytesToSend = StringToBytes("That doesn't make sense.");

                    await webSocket.SendAsync(
                        bytesToSend,
                        WebSocketMessageType.Text,
                        true,
                        CancellationToken.None
                        );

                    break;
                }
            }
        }