|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
| 2 | +using System.Net.WebSockets; |
| 3 | +using System.Threading; |
4 | 4 | using System.Threading.Tasks; |
5 | 5 | using Microsoft.AspNetCore.Builder; |
6 | 6 | using Microsoft.AspNetCore.Hosting; |
7 | | -using Microsoft.Extensions.Configuration; |
8 | | -using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using Microsoft.AspNetCore.Http.Features; |
9 | 9 | using Microsoft.Extensions.Logging; |
10 | 10 |
|
11 | 11 | namespace AutobahnTestApp |
12 | 12 | { |
13 | 13 | public class Startup |
14 | 14 | { |
15 | | - public Startup(IHostingEnvironment env) |
| 15 | + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
| 16 | + public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) |
16 | 17 | { |
17 | | - var builder = new ConfigurationBuilder() |
18 | | - .SetBasePath(env.ContentRootPath) |
19 | | - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) |
20 | | - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) |
21 | | - .AddEnvironmentVariables(); |
22 | | - Configuration = builder.Build(); |
23 | | - } |
| 18 | + if (!env.IsEnvironment("NativeSockets")) |
| 19 | + { |
| 20 | + // Register a middleware that disables the server-provided WebSockets feature |
| 21 | + app.Use((context, next) => |
| 22 | + { |
| 23 | + context.Features.Set<IHttpWebSocketFeature>(null); |
| 24 | + return next(); |
| 25 | + }); |
| 26 | + } |
| 27 | + app.UseWebSockets(); |
24 | 28 |
|
25 | | - public IConfigurationRoot Configuration { get; } |
| 29 | + app.Use(async (context, next) => |
| 30 | + { |
| 31 | + if (context.WebSockets.IsWebSocketRequest) |
| 32 | + { |
| 33 | + var webSocket = await context.WebSockets.AcceptWebSocketAsync(); |
| 34 | + await Echo(webSocket); |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + var wsScheme = context.Request.IsHttps ? "wss" : "ws"; |
| 39 | + var wsUrl = $"{wsScheme}://{context.Request.Host.Host}:{context.Request.Host.Port}{context.Request.Path}"; |
| 40 | + await context.Response.WriteAsync($"Ready to accept a WebSocket request at: {wsUrl}"); |
| 41 | + } |
| 42 | + }); |
26 | 43 |
|
27 | | - // This method gets called by the runtime. Use this method to add services to the container. |
28 | | - public void ConfigureServices(IServiceCollection services) |
29 | | - { |
30 | | - // Add framework services. |
31 | | - services.AddMvc(); |
32 | 44 | } |
33 | 45 |
|
34 | | - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. |
35 | | - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) |
| 46 | + private async Task Echo(WebSocket webSocket) |
36 | 47 | { |
37 | | - loggerFactory.AddConsole(Configuration.GetSection("Logging")); |
38 | | - loggerFactory.AddDebug(); |
39 | | - |
40 | | - app.UseMvc(); |
| 48 | + var buffer = new byte[1024 * 4]; |
| 49 | + var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 50 | + while (!result.CloseStatus.HasValue) |
| 51 | + { |
| 52 | + await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None); |
| 53 | + result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 54 | + } |
| 55 | + await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); |
41 | 56 | } |
42 | 57 | } |
43 | 58 | } |
0 commit comments