Skip to content

Commit

Permalink
Merge pull request #54 from unosquare/DropSystemNew
Browse files Browse the repository at this point in the history
Full support to WebSockets in all runtimes
  • Loading branch information
geoperez authored Nov 23, 2016
2 parents d810224 + 3fa3fac commit d46d73b
Show file tree
Hide file tree
Showing 71 changed files with 9,140 additions and 465 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ TestResult.xml
/src/Unosquare.Labs.EmbedIO/nuget.config
/src/Unosquare.Labs.EmbedIO.Samples/nuget.config
/test/Unosquare.Labs.EmbedIO.Tests/nuget.config
*.targets
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ A tiny, cross-platform, module based, MIT-licensed web server for .NET Framework
* Create REST APIs quickly with the out-of-the-box Web Api module
* Serve static files with 1 line of code (also out-of-the-box)
* Handle sessions with the built-in LocalSessionWebModule
* Web Sockets support (Not available on Mono 3.x though)
* WebSockets support (see notes below)
* CORS support. Origin, Header and Method validation with OPTIONS preflight
* Supports HTTP 206 Partial Content
* [OWIN](http://owin.org/) Middleware support via [Owin Middleware Module](https://github.com/unosquare/embedio-extras/tree/master/Unosquare.Labs.EmbedIO.OwinMiddleware).
Expand All @@ -33,6 +33,15 @@ A tiny, cross-platform, module based, MIT-licensed web server for .NET Framework
* Create GUIs for Windows services or Linux daemons
* Write client applications with real-time communication between them

Some notes regarding WebSocket support:

| Runtime | WebSocket support | Notes |
| --- | --- | --- |
| NET452 | Yes | Support Win7+ OS using a custom System.Net implementation based on Mono and [websocket-sharp](https://github.com/sta/websocket-sharp/) |
| NET46 | Yes | Support Win8+ OS using native System.Net library |
| MONO | Yes | Support Windows and Linux using custom System.Net implementation based on Mono and [websocket-sharp](https://github.com/sta/websocket-sharp/) |
| NETCORE10 | Yes | Support using a custom System.Net implementation based on Mono and [websocket-sharp](https://github.com/sta/websocket-sharp/) |

NuGet Installation:
-------------------
```
Expand Down Expand Up @@ -270,7 +279,7 @@ public class PeopleController : WebApiController
}
```

Web Sockets Example:
WebSockets Example:
-----------------

*During server setup:*
Expand Down
12 changes: 8 additions & 4 deletions src/Unosquare.Labs.EmbedIO.Samples/PeopleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Unosquare.Labs.EmbedIO.Modules;
using System.Threading.Tasks;
using Tubular;
using Unosquare.Labs.EmbedIO.Modules;
using Unosquare.Tubular.ObjectModel;
#if NET46
using System.Net;
#else
using Unosquare.Net;
#endif

/// <summary>
/// A very simple controller to handle People CRUD.
Expand All @@ -18,7 +22,7 @@ public class PeopleController : WebApiController
{
private readonly AppDbContext _dbContext = new AppDbContext();
private const string RelativePath = "/api/";

/// <summary>
/// Gets the people.
/// This will respond to
Expand Down Expand Up @@ -61,7 +65,7 @@ public bool GetPeople(WebServer server, HttpListenerContext context)
// here the error handler will respond with a generic 500 HTTP code a JSON-encoded object
// with error info. You will need to handle HTTP status codes correctly depending on the situation.
// For example, for keys that are not found, ou will need to respond with a 404 status code.
return HandleError(context, ex, (int) HttpStatusCode.InternalServerError);
return HandleError(context, ex, (int)System.Net.HttpStatusCode.InternalServerError);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Unosquare.Labs.EmbedIO.Samples/Person.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
#if !MONO
using System.ComponentModel.DataAnnotations.Schema;
using Unosquare.Labs.LiteLib;

namespace Unosquare.Labs.EmbedIO.Samples
Expand All @@ -17,4 +18,5 @@ public class Person : LiteModel

public string PhotoUrl => $"http://www.gravatar.com/avatar/{Extensions.ComputeMd5Hash(EmailAddress)}.png?s=100";
}
}
}
#endif
56 changes: 30 additions & 26 deletions src/Unosquare.Labs.EmbedIO.Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,47 @@

internal class Program
{
public static readonly bool IsMono = Type.GetType("Mono.Runtime") != null;

/// <summary>
/// Defines the entry point of the application.
/// </summary>
/// <param name="args">The arguments.</param>
private static void Main(string[] args)
{
Console.WriteLine("Running at MONO: {0}", IsMono);

var url = "http://localhost:9696/";

if (args.Length > 0)
url = args[0];

var dbContext = new AppDbContext();
#if !MONO
var dbContext = new AppDbContext();

foreach (var person in dbContext.People.SelectAll())
dbContext.People.Delete(person);
foreach (var person in dbContext.People.SelectAll())
dbContext.People.Delete(person);

dbContext.People.Insert(new Person()
{
Name = "Mario Di Vece",
Age = 31,
EmailAddress = "[email protected]"
});
dbContext.People.Insert(new Person()
{
Name = "Geovanni Perez",
Age = 32,
EmailAddress = "[email protected]"
});
dbContext.People.Insert(new Person()
{
Name = "Mario Di Vece",
Age = 31,
EmailAddress = "[email protected]"
});
dbContext.People.Insert(new Person()
{
Name = "Geovanni Perez",
Age = 32,
EmailAddress = "[email protected]"
});

dbContext.People.Insert(new Person()
{
Name = "Luis Gonzalez",
Age = 29,
EmailAddress = "[email protected]"
});
dbContext.People.Insert(new Person()
{
Name = "Luis Gonzalez",
Age = 29,
EmailAddress = "[email protected]"
});
#endif

// Our web server is disposable. Note that if you don't want to use logging,
// there are alternate constructors that allow you to skip specifying an ILog object.
Expand All @@ -64,17 +70,15 @@ private static void Main(string[] args)

// Register the static files server. See the html folder of this project. Also notice that
// the files under the html folder have Copy To Output Folder = Copy if Newer
StaticFilesSample.Setup(server);
StaticFilesSample.Setup(server, !IsMono);

// Register the Web Api Module. See the Setup method to find out how to do it
// It registers the WebApiModule and registers the controller(s) -- that's all.
server.WithWebApiController<PeopleController>();

#if NET452

// Register the WebSockets module. See the Setup method to find out how to do it
// It registers the WebSocketsModule and registers the server for the given paths(s)
WebSocketsSample.Setup(server);
#endif

// Once we've registered our modules and configured them, we call the Run() method.
// This is a non-blocking method (it return immediately) so in this case we avoid
Expand All @@ -84,7 +88,7 @@ private static void Main(string[] args)

// Fire up the browser to show the content!
#if DEBUG
#if NET452
#if !NETCOREAPP1_0
var browser = new System.Diagnostics.Process()
{
StartInfo = new System.Diagnostics.ProcessStartInfo(url)
Expand Down
13 changes: 7 additions & 6 deletions src/Unosquare.Labs.EmbedIO.Samples/StaticFilesSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public static string HtmlRootPath
{
get
{
var assemblyPath = Path.GetDirectoryName(typeof (Program).GetTypeInfo().Assembly.Location);
var assemblyPath = Path.GetDirectoryName(typeof(Program).GetTypeInfo().Assembly.Location);

#if DEBUG
#if NET452
// This lets you edit the files without restarting the server.
return Path.Combine(Directory.GetParent(assemblyPath).Parent.Parent.Parent.FullName, "html");
#else
#if DEBUG && !MONO
#if NETCOREAPP1_0
return Path.Combine(Directory.GetParent(assemblyPath).Parent.Parent.FullName, "html");
#else
return Path.Combine(Directory.GetParent(assemblyPath).Parent.Parent.Parent.FullName, "html");
#endif
#else
// This is when you have deployed the server.
Expand All @@ -39,12 +39,13 @@ public static string HtmlRootPath
/// Setups the specified server.
/// </summary>
/// <param name="server">The server.</param>
public static void Setup(WebServer server)
public static void Setup(WebServer server, bool useGzip)
{
server.RegisterModule(new StaticFilesModule(HtmlRootPath));
// The static files module will cache small files in ram until it detects they have been modified.
server.Module<StaticFilesModule>().UseRamCache = false;
server.Module<StaticFilesModule>().DefaultExtension = ".html";
server.Module<StaticFilesModule>().UseGzip = useGzip;
// We don't need to add the line below. The default document is always index.html.
//server.Module<StaticFilesWebModule>().DefaultDocument = "index.html";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{A45D13E0-4138-41A1-9C42-8F6BB9474BA6}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Unosquare.Labs.EmbedIO.Samples</RootNamespace>
<AssemblyName>Unosquare.Labs.EmbedIO.Samples</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;MONO</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AppDbContext.cs" />
<Compile Include="PeopleController.cs" />
<Compile Include="Person.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="StaticFilesSample.cs" />
<Compile Include="WebSocketsSample.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="html\css\embedio-icon.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\css\embedio.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\css\theme.css">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\favicon.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\index.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\partials\app-menu.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\partials\app-person.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\scripts\app.controllers.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\scripts\app.directives.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\scripts\app.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\scripts\app.routes.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\scripts\app.services.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\scripts\tubular\tubular-bundle.css">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\scripts\tubular\tubular-bundle.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\scripts\tubular\tubular-bundle.min.css">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\scripts\tubular\tubular-bundle.min.js">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\views\chat.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\views\cmd.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\views\home.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\views\people.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="html\views\tubular.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Unosquare.Labs.EmbedIO\Unosquare.Labs.EmbedIO.csproj">
<Project>{7d7c29b4-9493-4ebd-8f20-6fac1e7161ee}</Project>
<Name>Unosquare.Labs.EmbedIO</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading

0 comments on commit d46d73b

Please sign in to comment.