잉여장비목록 화면 추가(최효준s)
This commit is contained in:
76
OwinProject/OWIN/Startup.cs
Normal file
76
OwinProject/OWIN/Startup.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using Microsoft.Owin;
|
||||
using Owin;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Routing;
|
||||
|
||||
namespace OwinProject.OWIN
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public void Configuration(IAppBuilder appBuilder)
|
||||
{
|
||||
// Configure Web API for Self-Host
|
||||
HttpConfiguration config = new HttpConfiguration();
|
||||
config.MapHttpAttributeRoutes();
|
||||
|
||||
//메인파일 처리 방법
|
||||
IHttpRoute defaultRoute =
|
||||
config.Routes.CreateRoute("{controller}/{action}/{id}",
|
||||
new { controller = "home", action = "index", id = RouteParameter.Optional },
|
||||
null);
|
||||
|
||||
//기타파일들 처리 방법
|
||||
IHttpRoute cssRoute =
|
||||
config.Routes.CreateRoute("{path}/{subdir}/{resource}.{ext}",
|
||||
new { controller = "resource", action = "file", id = RouteParameter.Optional },
|
||||
null);
|
||||
|
||||
IHttpRoute mifRoute =
|
||||
config.Routes.CreateRoute("{path}/{resource}.{ext}",
|
||||
new { controller = "resource", action = "file", id = RouteParameter.Optional },
|
||||
null);
|
||||
|
||||
IHttpRoute icoRoute =
|
||||
config.Routes.CreateRoute("{resource}.{ext}",
|
||||
new { controller = "resource", action = "file", id = RouteParameter.Optional },
|
||||
null);
|
||||
|
||||
config.Routes.Add("mifRoute", mifRoute);
|
||||
config.Routes.Add("icoRoute", icoRoute);
|
||||
config.Routes.Add("cssRoute", cssRoute);
|
||||
config.Routes.Add("defaultRoute", defaultRoute);
|
||||
appBuilder.UseWebApi(config);
|
||||
|
||||
|
||||
//appBuilder.UseFileServer(new FileServerOptions
|
||||
//{
|
||||
// RequestPath = new PathString(string.Empty),
|
||||
// FileSystem = new PhysicalFileSystem("./MySubFolder"),
|
||||
// EnableDirectoryBrowsing = true,
|
||||
//});
|
||||
|
||||
//appBuilder.UseStageMarker(PipelineStage.MapHandler);
|
||||
|
||||
|
||||
//config.Routes.MapHttpRoute(
|
||||
// name: "ignore",
|
||||
// routeTemplate: @".*\.(css|js|gif|jpg)(/.*)?",
|
||||
// defaults: new
|
||||
// {
|
||||
// controller = "file",
|
||||
// action = "readtext",
|
||||
// id = RouteParameter.Optional
|
||||
// }
|
||||
// );
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
101
OwinProject/OWIN/StartupSSE.cs
Normal file
101
OwinProject/OWIN/StartupSSE.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using Microsoft.Owin;
|
||||
using Owin;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.Http;
|
||||
|
||||
namespace OwinProject.OWIN
|
||||
{
|
||||
public class StartupSSE
|
||||
{
|
||||
|
||||
|
||||
public void Configuration(IAppBuilder app)
|
||||
{
|
||||
var api = new Api();
|
||||
app.Run(context => api.Invoke(context));
|
||||
}
|
||||
|
||||
public class Subscriber
|
||||
{
|
||||
private StreamWriter _writer;
|
||||
private TaskCompletionSource<bool> _tcs;
|
||||
public Subscriber(Stream body, TaskCompletionSource<bool> tcs)
|
||||
{
|
||||
this._writer = new StreamWriter(body);
|
||||
this._tcs = tcs;
|
||||
}
|
||||
|
||||
public async void WriteAsync(string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
_writer.Write(message);
|
||||
_writer.Flush();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (e.HResult == -2146232800) // non-existent connection
|
||||
_tcs.SetResult(true);
|
||||
else
|
||||
_tcs.SetException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Api
|
||||
{
|
||||
System.Timers.Timer _timer = new System.Timers.Timer(500);
|
||||
List<Subscriber> _subscribers = new List<Subscriber>();
|
||||
public Api()
|
||||
{
|
||||
_timer.Elapsed += _timer_Elapsed;
|
||||
}
|
||||
|
||||
void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
||||
{
|
||||
UpdateSubscribers();
|
||||
}
|
||||
|
||||
public void UpdateSubscribers()
|
||||
{
|
||||
Console.WriteLine("updating {0} subscribers", _subscribers.Count);
|
||||
var subscribersCopy = _subscribers.ToList<Subscriber>();
|
||||
var msg = String.Format("Hello async at {0}\n", DateTime.Now);
|
||||
subscribersCopy.ForEach(w => w.WriteAsync(msg));
|
||||
_timer.Start();
|
||||
}
|
||||
|
||||
|
||||
public Task Invoke(IOwinContext context)
|
||||
{
|
||||
SetEventHeaders(context);
|
||||
System.IO.Stream responseStream = context.Environment["owin.ResponseBody"] as Stream;
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
var s = CreateSubscriber(responseStream, tcs);
|
||||
tcs.Task.ContinueWith(_ => _subscribers.Remove(s));
|
||||
Console.WriteLine("Add subscriber. Now have {0}", _subscribers.Count);
|
||||
s.WriteAsync("Registered\n");
|
||||
_timer.Start();
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
private Subscriber CreateSubscriber(System.IO.Stream responseStream, TaskCompletionSource<bool> tcs)
|
||||
{
|
||||
var s = new Subscriber(responseStream, tcs);
|
||||
_subscribers.Add(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
private static void SetEventHeaders(IOwinContext context)
|
||||
{
|
||||
context.Response.ContentType = "text/eventstream";
|
||||
context.Response.Headers["Transfer-Encoding"] = "chunked";
|
||||
context.Response.Headers["cache-control"] = "no-cache";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user