|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Linq; |
| 6 | +using System.Net.WebSockets; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNetCore.Builder; |
| 10 | +using Microsoft.AspNetCore.Hosting; |
| 11 | +using Microsoft.AspNetCore.Hosting.Server; |
| 12 | +using Microsoft.AspNetCore.Hosting.Server.Features; |
| 13 | +using Microsoft.AspNetCore.Http; |
| 14 | +using Microsoft.Extensions.DependencyInjection; |
| 15 | +using Microsoft.Extensions.Hosting; |
| 16 | +using Microsoft.Extensions.Logging; |
| 17 | +using Microsoft.Extensions.Tools.Internal; |
| 18 | + |
| 19 | +namespace Microsoft.DotNet.Watcher.Tools |
| 20 | +{ |
| 21 | + public class BrowserRefreshServer : IAsyncDisposable |
| 22 | + { |
| 23 | + private readonly IReporter _reporter; |
| 24 | + private readonly TaskCompletionSource _taskCompletionSource; |
| 25 | + private IHost _refreshServer; |
| 26 | + private WebSocket _webSocket; |
| 27 | + |
| 28 | + public BrowserRefreshServer(IReporter reporter) |
| 29 | + { |
| 30 | + _reporter = reporter; |
| 31 | + _taskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 32 | + } |
| 33 | + |
| 34 | + public async ValueTask<string> StartAsync(CancellationToken cancellationToken) |
| 35 | + { |
| 36 | + _refreshServer = new HostBuilder() |
| 37 | + .ConfigureWebHost(builder => |
| 38 | + { |
| 39 | + builder.UseKestrel(); |
| 40 | + builder.UseUrls("http://127.0.0.1:0"); |
| 41 | + |
| 42 | + builder.Configure(app => |
| 43 | + { |
| 44 | + app.UseWebSockets(); |
| 45 | + app.Run(WebSocketRequest); |
| 46 | + }); |
| 47 | + }) |
| 48 | + .Build(); |
| 49 | + |
| 50 | + await _refreshServer.StartAsync(cancellationToken); |
| 51 | + |
| 52 | + var serverUrl = _refreshServer.Services |
| 53 | + .GetRequiredService<IServer>() |
| 54 | + .Features |
| 55 | + .Get<IServerAddressesFeature>() |
| 56 | + .Addresses |
| 57 | + .First(); |
| 58 | + |
| 59 | + return serverUrl.Replace("http://", "ws://"); |
| 60 | + } |
| 61 | + |
| 62 | + private async Task WebSocketRequest(HttpContext context) |
| 63 | + { |
| 64 | + if (!context.WebSockets.IsWebSocketRequest) |
| 65 | + { |
| 66 | + context.Response.StatusCode = 400; |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + _webSocket = await context.WebSockets.AcceptWebSocketAsync(); |
| 71 | + await _taskCompletionSource.Task; |
| 72 | + } |
| 73 | + |
| 74 | + public async Task SendMessage(byte[] messageBytes, CancellationToken cancellationToken = default) |
| 75 | + { |
| 76 | + if (_webSocket == null || _webSocket.CloseStatus.HasValue) |
| 77 | + { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + try |
| 82 | + { |
| 83 | + await _webSocket.SendAsync(messageBytes, WebSocketMessageType.Text, endOfMessage: true, cancellationToken); |
| 84 | + } |
| 85 | + catch (Exception ex) |
| 86 | + { |
| 87 | + _reporter.Output($"Refresh server error: {ex}"); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public async ValueTask DisposeAsync() |
| 92 | + { |
| 93 | + if (_webSocket != null) |
| 94 | + { |
| 95 | + await _webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, default); |
| 96 | + _webSocket.Dispose(); |
| 97 | + } |
| 98 | + |
| 99 | + if (_refreshServer != null) |
| 100 | + { |
| 101 | + await _refreshServer.StopAsync(); |
| 102 | + _refreshServer.Dispose(); |
| 103 | + } |
| 104 | + |
| 105 | + _taskCompletionSource.TrySetResult(); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments