public async Task Handle(FileSocketListener listener) { OnChanged += listener.Update; while (listener.IsOpen) { await listener.Receive(); } OnChanged -= listener.Update; }
public static IApplicationBuilder UseLiveReload(this IApplicationBuilder builder) { builder.UseWebSockets(); builder.Use(async(context, next) => { var options = builder.ApplicationServices.GetService <IOptions <LiveReloadOptions> >(); if (options?.Value != null) { if (context.Request.Path == options.Value.LiveReloadLocalScriptPath) { if (options.Value.UseFile != null) { var path = options.Value.UseFile; if (File.Exists(path)) { context.Response.ContentType = "application/javascript"; await context.Response.SendFileAsync(path); } else { context.Response.StatusCode = 404; } return; } context.Response.ContentType = "application/javascript"; await context.Response.WriteAsync(Properties.Resources.live_reload); return; } } await next(); }); builder.Use(async(context, next) => { var options = builder.ApplicationServices.GetService <IOptions <LiveReloadOptions> >(); if (options?.Value != null) { if (context.Request.Path == options.Value.Url) { var livereloadwatcher = context.RequestServices.GetService <LiveReloadWatcher>(); if (livereloadwatcher == null) { throw new InvalidOperationException(""); } livereloadwatcher.Start(); if (context.WebSockets.IsWebSocketRequest) { using var socket = await context.WebSockets.AcceptWebSocketAsync(); using var listener = new FileSocketListener(socket); await livereloadwatcher.Handle(listener); } else { context.Response.StatusCode = 400; } return; } } await next(); }); return(builder); }