Esempio n. 1
0
 private void OnShutdown(ILogger <GameManager> logger)
 {
     try
     {
         GamesService.SaveState();
     }
     catch (Exception e)
     {
         logger.LogError(e.ToString());
     }
 }
Esempio n. 2
0
 private void TryRestoreState(ILogger <GameManager> logger)
 {
     try
     {
         GamesService.RestoreState(logger);
     }
     catch (Exception e)
     {
         logger.LogError(e.ToString());
     }
 }
Esempio n. 3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger <GameManager> logger, IHostApplicationLifetime applicationLifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                //app.UseSwagger();
                //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Backgammon Backend v1"));
            }

            TryRestoreState(logger);

            applicationLifetime.ApplicationStopping.Register(() => { OnShutdown(logger); });

            app.UseCors(options => options.WithOrigins("http://localhost:4200")
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowAnyOrigin()
                        );

            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            app.UseWebSockets();
            app.UseDefaultFiles();
            app.Use(async(context, next) =>
            {
                if (context.Request.Path == "/ws/game")
                {
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        logger.LogInformation($"New web socket request.");

                        var webSocket = await context.WebSockets.AcceptWebSocketAsync();
                        var userId    = context.Request.Query.FirstOrDefault(q => q.Key == "userId").Value;
                        var gameId    = context.Request.Query.FirstOrDefault(q => q.Key == "gameId").Value;
                        var playAi    = context.Request.Query.FirstOrDefault(q => q.Key == "playAi").Value == "true";
                        try
                        {
                            await GamesService.Connect(webSocket, context, logger, userId, gameId, playAi);
                        }
                        catch (Exception exc)
                        {
                            logger.LogError(exc.ToString());
                            await context.Response.WriteAsync(exc.Message, CancellationToken.None);
                            context.Response.StatusCode = 400;
                        }
                    }
                    else
                    {
                        context.Response.StatusCode = 400;
                    }
                }
                else
                {
                    await next();

                    // This enables angular routing to function on the same app as the web socket.
                    // If there's no available file and the request doesn't contain an extension, we're probably trying to access a page.
                    // Rewrite request to use app root
                    if (SinglePageAppRequestCheck(context))
                    {
                        context.Request.Path = "/index.html";
                        await next();
                    }
                }
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseStaticFiles();
        }