Skip to content

Commit 8f25ee4

Browse files
committed
Update Proxy
1 parent 34fadd0 commit 8f25ee4

2 files changed

Lines changed: 32 additions & 19 deletions

File tree

TechStacks/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using TechStacks;
1212
using TechStacks.Data;
1313
using TechStacks.ServiceInterface;
14+
using MyApp;
1415

1516
AppHost.RegisterLicense();
1617
var builder = WebApplication.CreateBuilder(args);

TechStacks/Proxy.cs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Net.WebSockets;
88
using System.Threading;
99

10-
namespace TechStacks;
10+
namespace MyApp;
1111

1212
public static class Proxy
1313
{
@@ -234,6 +234,9 @@ public static async Task HttpToNode(HttpContext context, HttpClient nextClient)
234234
await response.Content.CopyToAsync(context.Response.Body, context.RequestAborted);
235235
}
236236

237+
/// <summary>
238+
/// Proxy 404s to Node.js (except for API/backend routes) must be registered before endpoints
239+
/// </summary>
237240
public static void MapNotFoundToNode(this WebApplication app, HttpClient nextClient, string[]? ignorePaths=null)
238241
{
239242
app.Use(async (context, next) =>
@@ -295,28 +298,35 @@ static async Task PumpWebSocket(
295298
WebSocket destination,
296299
CancellationToken cancellationToken)
297300
{
298-
var buffer = new byte[8192];
299-
300-
while (source.State == WebSocketState.Open &&
301-
destination.State == WebSocketState.Open)
301+
try
302302
{
303-
var result = await source.ReceiveAsync(
304-
new ArraySegment<byte>(buffer), cancellationToken);
303+
var buffer = new byte[8192];
305304

306-
if (result.MessageType == WebSocketMessageType.Close)
305+
while (source.State == WebSocketState.Open &&
306+
destination.State == WebSocketState.Open)
307307
{
308-
await destination.CloseAsync(
309-
source.CloseStatus ?? WebSocketCloseStatus.NormalClosure,
310-
source.CloseStatusDescription,
308+
var result = await source.ReceiveAsync(
309+
new ArraySegment<byte>(buffer), cancellationToken);
310+
311+
if (result.MessageType == WebSocketMessageType.Close)
312+
{
313+
await destination.CloseAsync(
314+
source.CloseStatus ?? WebSocketCloseStatus.NormalClosure,
315+
source.CloseStatusDescription,
316+
cancellationToken);
317+
break;
318+
}
319+
320+
await destination.SendAsync(
321+
new ArraySegment<byte>(buffer, 0, result.Count),
322+
result.MessageType,
323+
result.EndOfMessage,
311324
cancellationToken);
312-
break;
313325
}
314-
315-
await destination.SendAsync(
316-
new ArraySegment<byte>(buffer, 0, result.Count),
317-
result.MessageType,
318-
result.EndOfMessage,
319-
cancellationToken);
326+
}
327+
catch (Exception e)
328+
{
329+
Console.WriteLine($"WebSocket Proxy: {e.Message}");
320330
}
321331
}
322332

@@ -354,7 +364,9 @@ public static IEndpointConventionBuilder MapNextHmr(this WebApplication app, Htt
354364
}
355365
else
356366
{
357-
await HttpToNode(context, nodeClient);
367+
// HMR endpoint expects WebSocket connections only
368+
context.Response.StatusCode = StatusCodes.Status400BadRequest;
369+
await context.Response.WriteAsync("WebSocket connection expected");
358370
}
359371
});
360372
}

0 commit comments

Comments
 (0)