127 lines
3.9 KiB
C#
127 lines
3.9 KiB
C#
#define UseOptions // or NoOptions or UseOptionsAO
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging.Console;
|
|
using Microsoft.Extensions.Logging.Debug;
|
|
using System;
|
|
using System.Net.WebSockets;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EchoApp
|
|
{
|
|
public class Startup
|
|
{
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddLogging(builder =>
|
|
{
|
|
builder.AddConsole()
|
|
.AddDebug()
|
|
.AddFilter<ConsoleLoggerProvider>(category: null, level: LogLevel.Debug)
|
|
.AddFilter<DebugLoggerProvider>(category: null, level: LogLevel.Debug);
|
|
});
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
|
|
{
|
|
if (env.EnvironmentName == "Development")
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
|
|
#if NoOptions
|
|
|
|
#region UseWebSockets
|
|
|
|
app.UseWebSockets();
|
|
|
|
#endregion UseWebSockets
|
|
|
|
#endif
|
|
#if UseOptions
|
|
|
|
#region UseWebSocketsOptions
|
|
|
|
var webSocketOptions = new WebSocketOptions()
|
|
{
|
|
KeepAliveInterval = TimeSpan.FromSeconds(120),
|
|
ReceiveBufferSize = 4 * 1024
|
|
};
|
|
|
|
app.UseWebSockets(webSocketOptions);
|
|
|
|
#endregion UseWebSocketsOptions
|
|
|
|
#endif
|
|
|
|
#if UseOptionsAO
|
|
|
|
#region UseWebSocketsOptionsAO
|
|
|
|
var webSocketOptions = new WebSocketOptions()
|
|
{
|
|
KeepAliveInterval = TimeSpan.FromSeconds(120),
|
|
ReceiveBufferSize = 4 * 1024
|
|
};
|
|
webSocketOptions.AllowedOrigins.Add("https://client.com");
|
|
webSocketOptions.AllowedOrigins.Add("https://www.client.com");
|
|
|
|
app.UseWebSockets(webSocketOptions);
|
|
|
|
#endregion UseWebSocketsOptionsAO
|
|
|
|
#endif
|
|
|
|
#region AcceptWebSocket
|
|
|
|
app.Use(async (context, next) =>
|
|
{
|
|
if (context.Request.Path == "/ws")
|
|
{
|
|
if (context.WebSockets.IsWebSocketRequest)
|
|
{
|
|
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
|
|
await Echo(context, webSocket);
|
|
}
|
|
else
|
|
{
|
|
context.Response.StatusCode = 400;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
await next();
|
|
}
|
|
});
|
|
|
|
#endregion AcceptWebSocket
|
|
|
|
app.UseFileServer();
|
|
}
|
|
|
|
#region Echo
|
|
|
|
private async Task Echo(HttpContext context, WebSocket webSocket)
|
|
{
|
|
var buffer = new byte[1024 * 4];
|
|
WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
|
while (!result.CloseStatus.HasValue)
|
|
{
|
|
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
|
|
|
|
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
|
|
}
|
|
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
|
|
}
|
|
|
|
#endregion Echo
|
|
}
|
|
} |