public override async Task ReceiveAsync(WebSocket socket, WebSocketReceiveResult result, byte[] buffer) { var socketId = WebSocketConnectionManager.GetId(socket); ExecContainerResponse responseMessage; try { var messageStr = Encoding.UTF8.GetString(buffer, 0, result.Count); var messageJson = JsonConvert.DeserializeObject <ExecContainerRequest>(messageStr); ContainerExecSession session = new ContainerExecSession(WebSocketConnectionManager.GetSocketById(socketId), messageJson); await session.Exec(); } catch (Exception ex) { responseMessage = new ExecContainerResponse() { Success = false, Message = ex.Message }; var responseStr = JsonConvert.SerializeObject(responseMessage); await SendMessageAsync(socketId, responseStr); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseExceptionMiddleware(); app.UseDefaultFiles(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseWebSockets(); app.UseRouting(); var serviceScopeFactory = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>(); var serviceProvider = serviceScopeFactory.CreateScope().ServiceProvider; app.MapWebSocketManager("/wsExec", serviceProvider.GetService <ExecMessageHandler>()); app.Use(async(context, next) => { if (context.Request.Path.Value == "wsExec") { if (context.WebSockets.IsWebSocketRequest) { WebSocket socket = await context.WebSockets.AcceptWebSocketAsync(); ContainerExecSession session = new ContainerExecSession(socket, null); await session.Exec(); } else { context.Response.StatusCode = 400; } } else { await next(); } }); //app.UseCors(options => //{ // options // .AllowAnyOrigin() // .AllowAnyMethod() // .AllowCredentials() // .AllowAnyHeader(); //}); app.UseCors("CorsPolicy"); app.UseEndpoints(endpoints => { endpoints.MapHub <ContainerHub>("/dockerhub"); endpoints.MapControllerRoute( name: "default", pattern: "{controller}/{action=Index}/{id?}"); }); // app.UseSpa(spa => // { // // To learn more about options for serving an Angular SPA from ASP.NET Core, // // see https://go.microsoft.com/fwlink/?linkid=864501 // spa.Options.SourcePath = "ClientApp"; // }); }