-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Add support for launching a browser on file change #24220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Net.WebSockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.Hosting.Server.Features; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Tools.Internal; | ||
|
||
namespace Microsoft.DotNet.Watcher.Tools | ||
{ | ||
public class BrowserRefreshServer : IAsyncDisposable | ||
{ | ||
private readonly IReporter _reporter; | ||
private readonly TaskCompletionSource _taskCompletionSource; | ||
private IHost _refreshServer; | ||
private WebSocket _webSocket; | ||
|
||
public BrowserRefreshServer(IReporter reporter) | ||
{ | ||
_reporter = reporter; | ||
_taskCompletionSource = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
} | ||
|
||
public async ValueTask<string> StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_refreshServer = new HostBuilder() | ||
.ConfigureWebHost(builder => | ||
{ | ||
builder.UseKestrel(); | ||
builder.UseUrls("http://127.0.0.1:0"); | ||
|
||
builder.Configure(app => | ||
{ | ||
app.UseWebSockets(); | ||
app.Run(WebSocketRequest); | ||
}); | ||
}) | ||
.Build(); | ||
|
||
await _refreshServer.StartAsync(cancellationToken); | ||
|
||
var serverUrl = _refreshServer.Services | ||
.GetRequiredService<IServer>() | ||
.Features | ||
.Get<IServerAddressesFeature>() | ||
.Addresses | ||
.First(); | ||
|
||
return serverUrl.Replace("http://", "ws://"); | ||
} | ||
|
||
private async Task WebSocketRequest(HttpContext context) | ||
{ | ||
if (!context.WebSockets.IsWebSocketRequest) | ||
{ | ||
context.Response.StatusCode = 400; | ||
return; | ||
} | ||
|
||
_webSocket = await context.WebSockets.AcceptWebSocketAsync(); | ||
await _taskCompletionSource.Task; | ||
} | ||
|
||
public async Task SendMessage(byte[] messageBytes, CancellationToken cancellationToken = default) | ||
{ | ||
if (_webSocket == null || _webSocket.CloseStatus.HasValue) | ||
{ | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
await _webSocket.SendAsync(messageBytes, WebSocketMessageType.Text, endOfMessage: true, cancellationToken); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_reporter.Output($"Refresh server error: {ex}"); | ||
} | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
BrennanConroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (_webSocket != null) | ||
{ | ||
await _webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, null, default); | ||
_webSocket.Dispose(); | ||
} | ||
|
||
if (_refreshServer != null) | ||
{ | ||
await _refreshServer.StopAsync(); | ||
_refreshServer.Dispose(); | ||
} | ||
|
||
_taskCompletionSource.TrySetResult(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,16 +11,19 @@ public class FileSet : IFileSet | |
{ | ||
private readonly HashSet<string> _files; | ||
|
||
public FileSet(IEnumerable<string> files) | ||
public FileSet(bool isNetCoreApp31OrNewer, IEnumerable<string> files) | ||
{ | ||
IsNetCoreApp31OrNewer = isNetCoreApp31OrNewer; | ||
_files = new HashSet<string>(files, StringComparer.OrdinalIgnoreCase); | ||
} | ||
|
||
public bool Contains(string filePath) => _files.Contains(filePath); | ||
|
||
public int Count => _files.Count; | ||
|
||
public static IFileSet Empty = new FileSet(Array.Empty<string>()); | ||
public bool IsNetCoreApp31OrNewer { get; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is stopping 2.1 from working? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The HTML injection middleware has to target whatever version we choose here. We could have it just launch the browser the first time, but that's not very useful in itself. Picking 3.1 seems like a good middleground. |
||
|
||
public static IFileSet Empty = new FileSet(false, Array.Empty<string>()); | ||
|
||
public IEnumerator<string> GetEnumerator() => _files.GetEnumerator(); | ||
IEnumerator IEnumerable.GetEnumerator() => _files.GetEnumerator(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Who is attaching with a WebSocket connection? (The browser I know, but how?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a follow up which will inject a hosting startup via environment variable.