Last active
November 2, 2021 20:58
-
-
Save bjoerntx/d910f5e4cd20741ce36f282d2914432e to your computer and use it in GitHub Desktop.
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
using Microsoft.AspNetCore.Http; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Threading.Tasks; | |
using TXTextControl.Web; | |
public class CustomWebSocketMiddleware { | |
private RequestDelegate m_next; | |
private IPAddress m_serviceAddress; | |
private int m_servicePort; | |
// list of available backend TCP services | |
internal readonly List<byte[]> DefaultServiceAddress = | |
new List<byte[]>() { | |
new byte[] { 127, 0, 0, 2 }, | |
new byte[] { 127, 0, 0, 1 } | |
}; | |
internal const int DefaultServicePort = 4278; | |
public CustomWebSocketMiddleware(RequestDelegate next) { | |
m_next = next; | |
m_serviceAddress = new IPAddress(DefaultServiceAddress[0]); | |
m_servicePort = DefaultServicePort; | |
} | |
public async Task Invoke(HttpContext context) { | |
if (context.WebSockets.IsWebSocketRequest && | |
context.WebSockets.WebSocketRequestedProtocols.Contains("TXTextControl.Web")) { | |
// server id from query string (provided in view code) | |
int serverId = int.Parse(context.Request.Query["server"]); | |
// create a new WebSocketHandler with random server | |
var ws = new WebSocketHandler( | |
new IPAddress(DefaultServiceAddress[serverId]), | |
m_servicePort); | |
await ws.Invoke(context); | |
} | |
else if (m_next != null) { | |
await m_next.Invoke(context); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment