🧱 Setup for the websocket
This commit is contained in:
		
							
								
								
									
										50
									
								
								DysonNetwork.Sphere/Connection/WebSocketController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								DysonNetwork.Sphere/Connection/WebSocketController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,50 @@ | ||||
| using System.Net.WebSockets; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace DysonNetwork.Sphere.Connection; | ||||
|  | ||||
| [ApiController] | ||||
| [Route("/ws")] | ||||
| public class WebSocketController : ControllerBase | ||||
| { | ||||
|     [Route("/ws")] | ||||
|     [Authorize] | ||||
|     public async Task TheGateway() | ||||
|     { | ||||
|         if (HttpContext.WebSockets.IsWebSocketRequest) | ||||
|         { | ||||
|             using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(); | ||||
|             await _ConnectionEventLoop(webSocket); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest; | ||||
|         } | ||||
|     } | ||||
|      | ||||
|     private static async Task _ConnectionEventLoop(WebSocket webSocket) | ||||
|     { | ||||
|         // For now, it's echo | ||||
|         var buffer = new byte[1024 * 4]; | ||||
|         var receiveResult = await webSocket.ReceiveAsync( | ||||
|             new ArraySegment<byte>(buffer), CancellationToken.None); | ||||
|  | ||||
|         while (!receiveResult.CloseStatus.HasValue) | ||||
|         { | ||||
|             await webSocket.SendAsync( | ||||
|                 new ArraySegment<byte>(buffer, 0, receiveResult.Count), | ||||
|                 receiveResult.MessageType, | ||||
|                 receiveResult.EndOfMessage, | ||||
|                 CancellationToken.None); | ||||
|  | ||||
|             receiveResult = await webSocket.ReceiveAsync( | ||||
|                 new ArraySegment<byte>(buffer), CancellationToken.None); | ||||
|         } | ||||
|  | ||||
|         await webSocket.CloseAsync( | ||||
|             receiveResult.CloseStatus.Value, | ||||
|             receiveResult.CloseStatusDescription, | ||||
|             CancellationToken.None); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user