-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement start of WebSocket support
- Loading branch information
1 parent
d3e7568
commit fe45df7
Showing
7 changed files
with
128 additions
and
9 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
src/Universalis.Application/Controllers/V2/WebSocketController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Universalis.Application.Realtime; | ||
using Universalis.Application.Swagger; | ||
using Universalis.Application.Views.V2.Realtime; | ||
|
||
namespace Universalis.Application.Controllers.V2; | ||
|
||
[ApiController] | ||
[ApiVersion("1")] | ||
[ApiVersion("2")] | ||
[Route("api")] | ||
public class WebSocketController : ControllerBase | ||
{ | ||
private readonly ISocketProcessor _socketProcessor; | ||
|
||
public WebSocketController(ISocketProcessor socketProcessor) | ||
{ | ||
_socketProcessor = socketProcessor; | ||
} | ||
|
||
/// <summary> | ||
/// Connect to the WebSocket endpoint of the API. Requires a valid WebSocket client. | ||
/// </summary> | ||
[HttpGet] | ||
[MapToApiVersion("1")] | ||
[Route("ws")] | ||
[ApiTag("WebSocket")] | ||
public Task<IActionResult> Get(CancellationToken cancellationToken = default) | ||
{ | ||
return GetV2(cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Connect to the WebSocket endpoint of the API. Requires a valid WebSocket client. | ||
/// </summary> | ||
[HttpGet] | ||
[MapToApiVersion("2")] | ||
[Route("v{version:apiVersion}/ws")] | ||
[ApiTag("WebSocket")] | ||
public async Task<IActionResult> GetV2(CancellationToken cancellationToken = default) | ||
{ | ||
if (HttpContext.WebSockets.IsWebSocketRequest) | ||
{ | ||
using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync( | ||
new WebSocketAcceptContext { DangerousEnableCompression = true }); | ||
var socketFinished = new TaskCompletionSource<WebSocketCompletionResult>(); | ||
|
||
_ = _socketProcessor.AddSocket(webSocket, socketFinished); | ||
|
||
var result = await socketFinished.Task; | ||
|
||
return Ok(new WebSocketCompletionView | ||
{ | ||
Error = result.Error, | ||
Reason = result.Reason, | ||
}); | ||
} | ||
else | ||
{ | ||
return BadRequest(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Net.WebSockets; | ||
using System.Threading.Tasks; | ||
|
||
namespace Universalis.Application.Realtime; | ||
|
||
public interface ISocketProcessor | ||
{ | ||
Task AddSocket(WebSocket ws, TaskCompletionSource<WebSocketCompletionResult> cs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Net.WebSockets; | ||
using System.Threading.Tasks; | ||
|
||
namespace Universalis.Application.Realtime; | ||
|
||
public class SocketProcessor : ISocketProcessor | ||
{ | ||
public Task AddSocket(WebSocket ws, TaskCompletionSource<WebSocketCompletionResult> cs) | ||
{ | ||
cs.SetResult(new WebSocketCompletionResult | ||
{ | ||
Error = false, | ||
Reason = "", | ||
}); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Universalis.Application/Realtime/WebSocketCompletionResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Universalis.Application.Realtime; | ||
|
||
public class WebSocketCompletionResult | ||
{ | ||
public bool Error { get; init; } | ||
|
||
public string Reason { get; init; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Universalis.Application/Views/V2/Realtime/WebSocketCompletionView.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Universalis.Application.Views.V2.Realtime; | ||
|
||
public class WebSocketCompletionView | ||
{ | ||
/// <summary> | ||
/// Whether or not the WebSocket completed in an error state. | ||
/// </summary> | ||
[JsonPropertyName("error")] | ||
public bool Error { get; set; } | ||
|
||
/// <summary> | ||
/// The reason for the WebSocket completion. | ||
/// </summary> | ||
[JsonPropertyName("reason")] | ||
public string Reason { get; set; } | ||
} |