/
DipSocketMiddleware.cs
118 lines (103 loc) · 4.24 KB
/
DipSocketMiddleware.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using DipSocket.Messages;
using DipSocket.Server;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DipSocket.NetCore.Extensions
{
/// <summary>
/// The middleware for handling requests to and responses from <see cref="DipSocketServer"/>.
/// </summary>
public class DipSocketMiddleware
{
private readonly DipSocketServer dipSocketServer;
/// <summary>
/// Creates an instance of the <see cref="DipSocketMiddleware"/> class.
/// </summary>
/// <param name="next">The <see cref="RequestDelegate"/>.</param>
/// <param name="dipSocketServer">A specialised instance of a class inheriting <see cref="DipSocketServer"/>.</param>
public DipSocketMiddleware(RequestDelegate next, DipSocketServer dipSocketServer)
{
this.dipSocketServer = dipSocketServer;
}
/// <summary>
/// Receives a request to the class inheriting <see cref="DipSocketServer"/>.
/// </summary>
/// <param name="context">The <see cref="HttpContext"/>.</param>
/// <returns>The response.</returns>
public async Task Invoke(HttpContext context)
{
try
{
if (context.WebSockets.IsWebSocketRequest)
{
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
var clientId = context.Request.Query["clientId"];
var data = context.Request.Query["data"];
await dipSocketServer.OnClientConnectAsync(webSocket, clientId, data);
await Receive(webSocket);
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
}
}
catch (WebSocketException wsex) when (wsex.WebSocketErrorCode.Equals(WebSocketError.ConnectionClosedPrematurely))
{
// The remote party closed the WebSocket connection
// without completing the close handshake.
}
catch (Exception ex)
{
var response = context.Response;
response.Clear();
response.ContentType = "application/json";
response.StatusCode = (int)HttpStatusCode.InternalServerError;
await response.WriteAsync(JsonConvert.SerializeObject(ex)).ConfigureAwait(false);
}
}
private async Task Receive(WebSocket webSocket)
{
try
{
var buffer = new byte[1024 * 4];
var messageBuilder = new StringBuilder();
while (webSocket.State.Equals(WebSocketState.Open))
{
WebSocketReceiveResult webSocketReceiveResult;
messageBuilder.Clear();
do
{
webSocketReceiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (webSocketReceiveResult.MessageType.Equals(WebSocketMessageType.Close))
{
await dipSocketServer.OnClientDisonnectAsync(webSocket);
continue;
}
if (webSocketReceiveResult.MessageType.Equals(WebSocketMessageType.Text))
{
messageBuilder.Append(Encoding.UTF8.GetString(buffer, 0, webSocketReceiveResult.Count));
continue;
}
}
while (!webSocketReceiveResult.EndOfMessage);
if (messageBuilder.Length > 0)
{
var json = messageBuilder.ToString();
var message = JsonConvert.DeserializeObject<Message>(json);
await dipSocketServer.ReceiveAsync(webSocket, message);
}
}
}
finally
{
webSocket?.Dispose();
}
}
}
}