diff --git a/samples/Owin.Nowin.HelloWorld/NowinServer.cs b/samples/Owin.Nowin.HelloWorld/NowinServer.cs new file mode 100644 index 00000000..7ddf2b85 --- /dev/null +++ b/samples/Owin.Nowin.HelloWorld/NowinServer.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNet.Hosting.Server; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Http.Internal; +using Microsoft.AspNet.Owin; +using Nowin; + +namespace NowinWebSockets +{ + public class NowinServer : IServer + { + private RequestDelegate _callback; + private INowinServer _nowinServer; + private ServerBuilder _serverBuilder; + + IFeatureCollection IServer.Features { get; } + + public NowinServer(ServerBuilder serverBuilder) + { + if (serverBuilder == null) + { + throw new ArgumentNullException(nameof(serverBuilder)); + } + _serverBuilder = serverBuilder; + } + + public void Start(RequestDelegate requestDelegate) + { + _callback = requestDelegate; + _nowinServer = _serverBuilder.SetOwinApp(OwinWebSocketAcceptAdapter.AdaptWebSockets(HandleRequest)).Build(); + _nowinServer.Start(); + } + + public void Dispose() + { + if (_nowinServer != null) + { + _nowinServer.Dispose(); + } + } + + private Task HandleRequest(IDictionary env) + { + return _callback(new DefaultHttpContext(new OwinFeatureCollection(env))); + } + + } +} diff --git a/samples/Owin.Nowin.HelloWorld/NowinServerFactory.cs b/samples/Owin.Nowin.HelloWorld/NowinServerFactory.cs index 9c19eece..cd8f0b53 100644 --- a/samples/Owin.Nowin.HelloWorld/NowinServerFactory.cs +++ b/samples/Owin.Nowin.HelloWorld/NowinServerFactory.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; using System.Net; -using System.Threading.Tasks; using Microsoft.AspNet.Hosting.Server; -using Microsoft.AspNet.Http.Features; -using Microsoft.AspNet.Owin; using Microsoft.Extensions.Configuration; using Nowin; @@ -12,29 +7,9 @@ namespace NowinWebSockets { public class NowinServerFactory : IServerFactory { - private Func _callback; - - private Task HandleRequest(IDictionary env) - { - return _callback(new OwinFeatureCollection(env)); - } - - public IFeatureCollection Initialize(IConfiguration configuration) - { - // TODO: Parse config - return new FeatureCollection(); - } - - public IDisposable Start(IFeatureCollection serverFeatures, Func application) + public IServer CreateServer(IConfiguration configuration) { - var builder = ServerBuilder.New() - .SetAddress(IPAddress.Any) - .SetPort(5000) - .SetOwinApp(OwinWebSocketAcceptAdapter.AdaptWebSockets(HandleRequest)); - _callback = application; - var server = builder.Build(); - server.Start(); - return server; + return new NowinServer(ServerBuilder.New().SetAddress(IPAddress.Any).SetPort(5000)); } } } \ No newline at end of file diff --git a/samples/Owin.Nowin.WebSockets/NowinServer.cs b/samples/Owin.Nowin.WebSockets/NowinServer.cs new file mode 100644 index 00000000..ed8826cf --- /dev/null +++ b/samples/Owin.Nowin.WebSockets/NowinServer.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNet.Hosting.Server; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Http.Internal; +using Microsoft.AspNet.Owin; +using Nowin; + +namespace NowinWebSockets +{ + public class NowinServer : IServer + { + private RequestDelegate _callback; + private INowinServer _nowinServer; + private ServerBuilder _serverBuilder; + + IFeatureCollection IServer.Features { get; } + + public NowinServer(ServerBuilder serverBuilder) + { + if (serverBuilder == null) + { + throw new ArgumentNullException(nameof(serverBuilder)); + } + _serverBuilder = serverBuilder; + } + + public void Start(RequestDelegate requestDelegate) + { + _callback = requestDelegate; + _nowinServer = _serverBuilder.SetOwinApp(OwinWebSocketAcceptAdapter.AdaptWebSockets(HandleRequest)).Build(); + _nowinServer.Start(); + } + + public void Dispose() + { + if (_nowinServer != null) + { + _nowinServer.Dispose(); + } + } + + private Task HandleRequest(IDictionary env) + { + return _callback(new DefaultHttpContext(new OwinFeatureCollection(env))); + } + } +} diff --git a/samples/Owin.Nowin.WebSockets/NowinServerFactory.cs b/samples/Owin.Nowin.WebSockets/NowinServerFactory.cs index 9c19eece..cd8f0b53 100644 --- a/samples/Owin.Nowin.WebSockets/NowinServerFactory.cs +++ b/samples/Owin.Nowin.WebSockets/NowinServerFactory.cs @@ -1,10 +1,5 @@ -using System; -using System.Collections.Generic; using System.Net; -using System.Threading.Tasks; using Microsoft.AspNet.Hosting.Server; -using Microsoft.AspNet.Http.Features; -using Microsoft.AspNet.Owin; using Microsoft.Extensions.Configuration; using Nowin; @@ -12,29 +7,9 @@ namespace NowinWebSockets { public class NowinServerFactory : IServerFactory { - private Func _callback; - - private Task HandleRequest(IDictionary env) - { - return _callback(new OwinFeatureCollection(env)); - } - - public IFeatureCollection Initialize(IConfiguration configuration) - { - // TODO: Parse config - return new FeatureCollection(); - } - - public IDisposable Start(IFeatureCollection serverFeatures, Func application) + public IServer CreateServer(IConfiguration configuration) { - var builder = ServerBuilder.New() - .SetAddress(IPAddress.Any) - .SetPort(5000) - .SetOwinApp(OwinWebSocketAcceptAdapter.AdaptWebSockets(HandleRequest)); - _callback = application; - var server = builder.Build(); - server.Start(); - return server; + return new NowinServer(ServerBuilder.New().SetAddress(IPAddress.Any).SetPort(5000)); } } } \ No newline at end of file