Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Reacting to Hosting changes #71

Merged
merged 1 commit into from
Oct 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions samples/Owin.Nowin.HelloWorld/NowinServer.cs
Original file line number Diff line number Diff line change
@@ -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<string, object> env)
{
return _callback(new DefaultHttpContext(new OwinFeatureCollection(env)));
}

}
}
29 changes: 2 additions & 27 deletions samples/Owin.Nowin.HelloWorld/NowinServerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,15 @@
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;

namespace NowinWebSockets
{
public class NowinServerFactory : IServerFactory
{
private Func<IFeatureCollection, Task> _callback;

private Task HandleRequest(IDictionary<string, object> env)
{
return _callback(new OwinFeatureCollection(env));
}

public IFeatureCollection Initialize(IConfiguration configuration)
{
// TODO: Parse config
return new FeatureCollection();
}

public IDisposable Start(IFeatureCollection serverFeatures, Func<IFeatureCollection, Task> 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));
}
}
}
50 changes: 50 additions & 0 deletions samples/Owin.Nowin.WebSockets/NowinServer.cs
Original file line number Diff line number Diff line change
@@ -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<string, object> env)
{
return _callback(new DefaultHttpContext(new OwinFeatureCollection(env)));
}
}
}
29 changes: 2 additions & 27 deletions samples/Owin.Nowin.WebSockets/NowinServerFactory.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,15 @@
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;

namespace NowinWebSockets
{
public class NowinServerFactory : IServerFactory
{
private Func<IFeatureCollection, Task> _callback;

private Task HandleRequest(IDictionary<string, object> env)
{
return _callback(new OwinFeatureCollection(env));
}

public IFeatureCollection Initialize(IConfiguration configuration)
{
// TODO: Parse config
return new FeatureCollection();
}

public IDisposable Start(IFeatureCollection serverFeatures, Func<IFeatureCollection, Task> 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));
}
}
}