Skip to content

Commit fb3cd89

Browse files
committed
Support for standalone LR host when watching.
1 parent 26d707e commit fb3cd89

7 files changed

Lines changed: 61 additions & 29 deletions

File tree

src/clients/Wyam/Commands/BuildCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ protected override ExitCode RunCommand(Preprocessor preprocessor)
168168
if (runLiveReloadServer)
169169
{
170170
liveReloadServer = new LiveReloadServer();
171+
liveReloadServer.StartStandaloneHost();
171172
}
172173

173174
// Start the preview server

src/clients/Wyam/LiveReload/LiveReloadServer.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,55 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Reflection;
45

56
using Microsoft.Owin;
67
using Microsoft.Owin.FileSystems;
8+
using Microsoft.Owin.Hosting;
9+
using Microsoft.Owin.Hosting.Tracing;
710
using Microsoft.Owin.StaticFiles;
811

912
using Owin;
1013
using Owin.WebSocket.Extensions;
1114

15+
using Wyam.Common.Tracing;
16+
using Wyam.Owin;
17+
1218
namespace Wyam.LiveReload
1319
{
1420
internal class LiveReloadServer : IDisposable
1521
{
1622
private readonly ReloadClientServiceLocator _clientServiceLocator;
23+
private IDisposable _server;
1724

1825
public LiveReloadServer()
1926
{
2027
_clientServiceLocator = new ReloadClientServiceLocator();
2128
}
2229

30+
public void StartStandaloneHost(int port = 35729)
31+
{
32+
try
33+
{
34+
// This must be 127.0.0.1 due to http.sys hostname verification, LiveReload hardcodes it.
35+
StartOptions options = new StartOptions("http://127.0.0.1:" + port);
36+
options.Settings.Add(typeof(ITraceOutputFactory).FullName, typeof(NullTraceOutputFactory).AssemblyQualifiedName);
37+
_server = WebApp.Start(options, InjectOwinMiddleware);
38+
}
39+
catch (Exception ex)
40+
{
41+
Trace.Warning($"Error while running the LiveReload server: {ex.Message}");
42+
}
43+
44+
Trace.Verbose($"LiveReload server listening on port {port}.");
45+
}
46+
2347
public void InjectOwinMiddleware(IAppBuilder app)
2448
{
2549
// Host livereload.js
26-
var liveReloadAssembly = typeof(LiveReloadServer).Assembly;
27-
var rootNamespace = typeof(LiveReloadServer).Namespace;
28-
var reloadFilesystem = new EmbeddedResourceFileSystem(liveReloadAssembly, $"{rootNamespace}");
50+
Assembly liveReloadAssembly = typeof(LiveReloadServer).Assembly;
51+
string rootNamespace = typeof(LiveReloadServer).Namespace;
52+
IFileSystem reloadFilesystem = new EmbeddedResourceFileSystem(liveReloadAssembly, $"{rootNamespace}");
2953
app.UseStaticFiles(new StaticFileOptions
3054
{
3155
RequestPath = PathString.Empty,
@@ -39,10 +63,10 @@ public void InjectOwinMiddleware(IAppBuilder app)
3963

4064
public void RebuildCompleted(ICollection<string> filesChanged)
4165
{
42-
var clientsToNotify = _clientServiceLocator?.ReloadClients ?? Enumerable.Empty<ReloadClient>();
43-
foreach (var client in clientsToNotify.Where(x => x.IsConnected))
66+
IEnumerable<ReloadClient> clientsToNotify = _clientServiceLocator.ReloadClients;
67+
foreach (ReloadClient client in clientsToNotify.Where(x => x.IsConnected))
4468
{
45-
foreach (var modifiedFile in filesChanged)
69+
foreach (string modifiedFile in filesChanged)
4670
{
4771
client.NotifyOfChanges(modifiedFile);
4872
}
@@ -51,6 +75,7 @@ public void RebuildCompleted(ICollection<string> filesChanged)
5175

5276
public void Dispose()
5377
{
78+
_server?.Dispose();
5479
}
5580
}
5681
}

src/clients/Wyam/LiveReload/ReloadClient.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public class ReloadClient : WebSocketConnection
3535

3636
public override Task OnMessageReceived(ArraySegment<byte> message, WebSocketMessageType type)
3737
{
38-
var json = Encoding.UTF8.GetString(message.Array, message.Offset, message.Count);
38+
string json = Encoding.UTF8.GetString(message.Array, message.Offset, message.Count);
3939
HandleClientMessage(json);
40-
40+
4141
return Task.CompletedTask;
4242
}
4343

@@ -57,7 +57,7 @@ public override void OnClose(WebSocketCloseStatus? closeStatus, string closeStat
5757
public void NotifyOfChanges(string modifiedFile, bool supportCssReload = true)
5858
{
5959
// Asume changes have been rebuilt by this time
60-
var reloadMessage = new ReloadMessage
60+
ReloadMessage reloadMessage = new ReloadMessage
6161
{
6262
Path = modifiedFile,
6363
LiveCss = supportCssReload
@@ -69,15 +69,15 @@ public void NotifyOfChanges(string modifiedFile, bool supportCssReload = true)
6969

7070
private ILiveReloadMessage HandleClientMessage(string json)
7171
{
72-
var parsedMessage = JsonConvert.DeserializeObject<BasicMessage>(json, _defaultSettings);
72+
BasicMessage parsedMessage = JsonConvert.DeserializeObject<BasicMessage>(json, _defaultSettings);
7373
switch (parsedMessage.Command)
7474
{
7575
case "info":
76-
var info = JsonConvert.DeserializeObject<InfoMessage>(json, _defaultSettings);
77-
LogVerbose($"LiveReload sent info ({info.Url}).");
76+
InfoMessage info = JsonConvert.DeserializeObject<InfoMessage>(json, _defaultSettings);
77+
LogVerbose($"LiveReload client sent info ({info.Url}).");
7878
break;
7979
case "hello":
80-
var hello = JsonConvert.DeserializeObject<HelloMessage>(json, _defaultSettings);
80+
HelloMessage hello = JsonConvert.DeserializeObject<HelloMessage>(json, _defaultSettings);
8181
HandleHello(hello);
8282
break;
8383
default:
@@ -90,14 +90,14 @@ private ILiveReloadMessage HandleClientMessage(string json)
9090

9191
private void HandleHello(HelloMessage message)
9292
{
93-
var negotiatedVersion = message.Protocols
93+
string negotiatedVersion = message.Protocols
9494
.Intersect(_supportedVersion)
9595
.OrderByDescending(x => x)
9696
.FirstOrDefault();
9797

9898
if (negotiatedVersion == null)
9999
{
100-
var incompatibleMessage = "LiveReload client is not compatible with this server, aborting connection. " +
100+
string incompatibleMessage = "LiveReload client is not compatible with this server, aborting connection. " +
101101
$"Client=({string.Join(",", message.Protocols)}) " +
102102
$"Server=({string.Join(",", _supportedVersion)})";
103103
LogVerbose(incompatibleMessage);
@@ -112,7 +112,7 @@ private void HandleHello(HelloMessage message)
112112

113113
private void SayHello()
114114
{
115-
var helloMessage = new HelloMessage
115+
HelloMessage helloMessage = new HelloMessage
116116
{
117117
Protocols = _supportedVersion
118118
};
@@ -122,8 +122,8 @@ private void SayHello()
122122

123123
private void SendObject<T>(T obj)
124124
{
125-
var json = JsonConvert.SerializeObject(obj, _defaultSettings);
126-
var bytes = Encoding.UTF8.GetBytes(json); // UTF-8 by spec
125+
string json = JsonConvert.SerializeObject(obj, _defaultSettings);
126+
byte[] bytes = Encoding.UTF8.GetBytes(json); // UTF-8 by spec
127127
SendText(bytes, true);
128128
}
129129

src/clients/Wyam/LiveReload/ReloadClientServiceLocator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public TService GetInstance<TService>()
3636
{
3737
if (typeof(TService) == typeof(ReloadClient))
3838
{
39-
var client = CreateClient();
39+
object client = CreateClient();
4040
return (TService) client;
4141
}
4242

@@ -55,7 +55,7 @@ public IEnumerable<TService> GetAllInstances<TService>()
5555

5656
private object CreateClient()
5757
{
58-
var client = new ReloadClient();
58+
ReloadClient client = new ReloadClient();
5959
_clients.Add(client);
6060
return client;
6161
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.IO;
2+
3+
using Microsoft.Owin.Hosting.Tracing;
4+
5+
namespace Wyam.Owin
6+
{
7+
public class NullTraceOutputFactory : ITraceOutputFactory
8+
{
9+
public TextWriter Create(string outputFile)
10+
{
11+
return StreamWriter.Null;
12+
}
13+
}
14+
}

src/clients/Wyam/PreviewServer.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
43
using System.Linq;
54

65
using Microsoft.Owin;
@@ -90,13 +89,5 @@ public static IDisposable Start(DirectoryPath path, int port, bool forceExtensio
9089

9190
return server;
9291
}
93-
94-
private class NullTraceOutputFactory : ITraceOutputFactory
95-
{
96-
public TextWriter Create(string outputFile)
97-
{
98-
return StreamWriter.Null;
99-
}
100-
}
10192
}
10293
}

src/clients/Wyam/Wyam.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<Compile Include="LiveReload\Messages\ILiveReloadMessage.cs" />
130130
<Compile Include="LiveReload\ReloadClient.cs" />
131131
<Compile Include="LiveReload\Messages\ReloadMessage.cs" />
132+
<Compile Include="Owin\NullTraceOutputFactory.cs" />
132133
<Compile Include="Owin\VirtualDirectoryExtensions.cs" />
133134
<Compile Include="Owin\VirtualDirectoryMiddleware.cs" />
134135
<Compile Include="PreviewServer.cs" />

0 commit comments

Comments
 (0)