-
Notifications
You must be signed in to change notification settings - Fork 0
/
Middleware.cs
45 lines (39 loc) · 1.17 KB
/
Middleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using WebSockets;
public class Middleware
{
private readonly RequestDelegate next;
private readonly IServer server;
public Middleware(RequestDelegate next, IServer server)
{
this.next = next;
this.server = server;
}
public async Task InvokeAsync(HttpContext context)
{
if (context.Request.Path.StartsWithSegments(PathString.FromUriComponent("/ws")))
{
if (context.WebSockets.IsWebSocketRequest)
{
using (var socket = await context.WebSockets.AcceptWebSocketAsync())
{
if (context.Request.Path.HasValue)
{
var segments = context.Request.Path.Value.Split('/');
if (Guid.TryParse(segments.LastOrDefault(), out var id))
{
await server.Accept(id, socket);
}
}
}
}
else
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
}
}
else
{
await next(context);
}
}
}