aspnetcore/test/AutobahnTestServer/Startup.cs

85 lines
3.0 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
namespace AutobahnTestServer
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Map("/Managed", managedWebSocketsApp =>
{
// Comment this out to test native server implementations
managedWebSocketsApp.UseWebSockets(new WebSocketOptions
{
ReplaceFeature = true
});
managedWebSocketsApp.Use(async (context, next) =>
{
if (context.WebSockets.IsWebSocketRequest)
{
Console.WriteLine("Echo: " + context.Request.Path);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Echo(webSocket);
return;
}
await next();
});
});
app.Map("/Native", nativeWebSocketsApp =>
{
nativeWebSocketsApp.Use(async (context, next) =>
{
if (context.WebSockets.IsWebSocketRequest)
{
Console.WriteLine("Echo: " + context.Request.Path);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Echo(webSocket);
return;
}
await next();
});
});
app.Run(context =>
{
Console.WriteLine("Hello World");
return context.Response.WriteAsync("Hello World");
});
}
private async Task Echo(WebSocket webSocket)
{
byte[] buffer = new byte[1024 * 4];
var 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);
}
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}