Skip to content

Instantly share code, notes, and snippets.

@neuecc
Created May 19, 2013 08:07
Show Gist options
  • Save neuecc/5607035 to your computer and use it in GitHub Desktop.
Save neuecc/5607035 to your computer and use it in GitHub Desktop.
FileSystemWatcher+SignalRでうまくいかにゃいんですー。最初Hubでやってうまくいかなかったので、とりあえずPersistentConnectionにしたけれどやっぱりうまくいかにゃい。一発目は発行してくれるんだけど、二発目以降はうんともすんともになってもげー。FileSystemWatcher自体は正常に動いていて、Changedイベントでデータ取って、までは行くけど、それをSignalRでBroadcastする、そのBroadcastが無反応系。
namespace MvcApplication4.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
// 後ろから30件、末尾から読んでるわけじゃないので割と手抜き
var initialData = System.IO.File.ReadLines(GlobalConfig.LogPath).Reverse().Take(30);
return View(initialData);
}
}
}
@model IEnumerable<string>
<h1>@MvcApplication4.SignalR.Sample.GlobalConfig.LogPath</h1>
<h2>アプリケーションログ</h2>
<button onclick="location.reload();">再読み込み</button>
<p>↑新しい</p>
<div id="newLogArea"></div>
@foreach (var item in @Model)
{
@item<br />
}
<script src="~/bundles/jquery"></script>
<script src="~/Scripts/jquery.signalR-1.1.0.js"></script>
<script src="~/signalr/hubs"></script>
<script>
var logArea = $("#newLogArea");
var connection = $.connection("/echo");
connection.received(function (data) {
alert(data);
});
connection.start().done(function(){
alert("start");
}).fail(function (x) {
alert(x);
});
</script>
namespace MvcApplication4.SignalR.Sample
{
public static class GlobalConfig
{
public static readonly string LogPath = @"C:\Users\user-pc\Documents\visual studio 2012\Projects\MvcApplication4\MvcApplication4\bin\log.txt";
}
public class LogWatchConnection : PersistentConnection
{
StreamReader reader;
protected override Task OnConnected(IRequest request, string connectionId)
{
InitializeWatcher();
return base.OnConnected(request, connectionId);
}
protected override Task OnReconnected(IRequest request, string connectionId)
{
InitializeWatcher();
return base.OnReconnected(request, connectionId);
}
void InitializeWatcher()
{
if (reader != null) return;
var stream = new FileStream(GlobalConfig.LogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
stream.Seek(0, SeekOrigin.End); // set to end
this.reader = new StreamReader(stream, Encoding.UTF8);
var fsw = new FileSystemWatcher(Path.GetDirectoryName(GlobalConfig.LogPath), Path.GetFileName(GlobalConfig.LogPath));
fsw.EnableRaisingEvents = true;
fsw.Changed += fsw_Changed;
}
void fsw_Changed(object sender, FileSystemEventArgs e)
{
var str = reader.ReadToEnd();
if (string.IsNullOrWhiteSpace(str)) return;
Connection.Broadcast(str);
}
}
}
namespace MvcApplication4
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapConnection<LogWatchConnection>("echo", "/echo");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment