Exemplo n.º 1
0
        public void Listen()
        {
            var context  = HttpListener.GetContext();
            var request  = context.Request;
            var response = context.Response;

            var logger     = new Logger();
            var repository = new ParticipantsRepository();
            var service    = new ParticipantsService(repository);

            var indexController        = new IndexController(service, logger);
            var voteController         = new VoteController(service, logger);
            var participantsController = new ParticipantsController(service, logger);

            if (context.Request.IsWebSocketRequest)
            {
                HandleWebsocket(context);
            }
            else
            {
                try
                {
                    var fileName = request.RawUrl.Substring(1);
                    fileName = string.IsNullOrWhiteSpace(fileName) ? "index.html" : fileName;
                    Console.WriteLine($"Client is looking for {fileName}");

                    switch (fileName)
                    {
                    case "index.html":
                        indexController.Handle(context);
                        break;

                    case "vote.html":
                        voteController.Handle(context);
                        break;

                    case "participants.html":
                        participantsController.Handle(context);
                        break;

                    default:
                        HandleStaticFile(context);
                        break;
                    }
                }
                finally
                {
                    response.OutputStream.Close();
                }
            }
        }