Skip to content

Commit d46d73b

Browse files
authored
Merge pull request #54 from unosquare/DropSystemNew
Full support to WebSockets in all runtimes
2 parents d810224 + 3fa3fac commit d46d73b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+9140
-465
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,4 @@ TestResult.xml
162162
/src/Unosquare.Labs.EmbedIO/nuget.config
163163
/src/Unosquare.Labs.EmbedIO.Samples/nuget.config
164164
/test/Unosquare.Labs.EmbedIO.Tests/nuget.config
165+
*.targets

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ A tiny, cross-platform, module based, MIT-licensed web server for .NET Framework
1919
* Create REST APIs quickly with the out-of-the-box Web Api module
2020
* Serve static files with 1 line of code (also out-of-the-box)
2121
* Handle sessions with the built-in LocalSessionWebModule
22-
* Web Sockets support (Not available on Mono 3.x though)
22+
* WebSockets support (see notes below)
2323
* CORS support. Origin, Header and Method validation with OPTIONS preflight
2424
* Supports HTTP 206 Partial Content
2525
* [OWIN](http://owin.org/) Middleware support via [Owin Middleware Module](https://github.com/unosquare/embedio-extras/tree/master/Unosquare.Labs.EmbedIO.OwinMiddleware).
@@ -33,6 +33,15 @@ A tiny, cross-platform, module based, MIT-licensed web server for .NET Framework
3333
* Create GUIs for Windows services or Linux daemons
3434
* Write client applications with real-time communication between them
3535

36+
Some notes regarding WebSocket support:
37+
38+
| Runtime | WebSocket support | Notes |
39+
| --- | --- | --- |
40+
| NET452 | Yes | Support Win7+ OS using a custom System.Net implementation based on Mono and [websocket-sharp](https://github.com/sta/websocket-sharp/) |
41+
| NET46 | Yes | Support Win8+ OS using native System.Net library |
42+
| MONO | Yes | Support Windows and Linux using custom System.Net implementation based on Mono and [websocket-sharp](https://github.com/sta/websocket-sharp/) |
43+
| NETCORE10 | Yes | Support using a custom System.Net implementation based on Mono and [websocket-sharp](https://github.com/sta/websocket-sharp/) |
44+
3645
NuGet Installation:
3746
-------------------
3847
```
@@ -270,7 +279,7 @@ public class PeopleController : WebApiController
270279
}
271280
```
272281

273-
Web Sockets Example:
282+
WebSockets Example:
274283
-----------------
275284

276285
*During server setup:*

src/Unosquare.Labs.EmbedIO.Samples/PeopleController.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6-
using System.Net;
6+
using Unosquare.Labs.EmbedIO.Modules;
77
using System.Threading.Tasks;
88
using Tubular;
9-
using Unosquare.Labs.EmbedIO.Modules;
109
using Unosquare.Tubular.ObjectModel;
10+
#if NET46
11+
using System.Net;
12+
#else
13+
using Unosquare.Net;
14+
#endif
1115

1216
/// <summary>
1317
/// A very simple controller to handle People CRUD.
@@ -18,7 +22,7 @@ public class PeopleController : WebApiController
1822
{
1923
private readonly AppDbContext _dbContext = new AppDbContext();
2024
private const string RelativePath = "/api/";
21-
25+
2226
/// <summary>
2327
/// Gets the people.
2428
/// This will respond to
@@ -61,7 +65,7 @@ public bool GetPeople(WebServer server, HttpListenerContext context)
6165
// here the error handler will respond with a generic 500 HTTP code a JSON-encoded object
6266
// with error info. You will need to handle HTTP status codes correctly depending on the situation.
6367
// For example, for keys that are not found, ou will need to respond with a 404 status code.
64-
return HandleError(context, ex, (int) HttpStatusCode.InternalServerError);
68+
return HandleError(context, ex, (int)System.Net.HttpStatusCode.InternalServerError);
6569
}
6670
}
6771

src/Unosquare.Labs.EmbedIO.Samples/Person.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.DataAnnotations.Schema;
1+
#if !MONO
2+
using System.ComponentModel.DataAnnotations.Schema;
23
using Unosquare.Labs.LiteLib;
34

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

1819
public string PhotoUrl => $"http://www.gravatar.com/avatar/{Extensions.ComputeMd5Hash(EmailAddress)}.png?s=100";
1920
}
20-
}
21+
}
22+
#endif

src/Unosquare.Labs.EmbedIO.Samples/Program.cs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,47 @@
55

66
internal class Program
77
{
8+
public static readonly bool IsMono = Type.GetType("Mono.Runtime") != null;
9+
810
/// <summary>
911
/// Defines the entry point of the application.
1012
/// </summary>
1113
/// <param name="args">The arguments.</param>
1214
private static void Main(string[] args)
1315
{
16+
Console.WriteLine("Running at MONO: {0}", IsMono);
17+
1418
var url = "http://localhost:9696/";
1519

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

19-
var dbContext = new AppDbContext();
23+
#if !MONO
24+
var dbContext = new AppDbContext();
2025

21-
foreach (var person in dbContext.People.SelectAll())
22-
dbContext.People.Delete(person);
26+
foreach (var person in dbContext.People.SelectAll())
27+
dbContext.People.Delete(person);
2328

24-
dbContext.People.Insert(new Person()
25-
{
26-
Name = "Mario Di Vece",
27-
Age = 31,
28-
EmailAddress = "[email protected]"
29-
});
30-
dbContext.People.Insert(new Person()
31-
{
32-
Name = "Geovanni Perez",
33-
Age = 32,
34-
EmailAddress = "[email protected]"
35-
});
29+
dbContext.People.Insert(new Person()
30+
{
31+
Name = "Mario Di Vece",
32+
Age = 31,
33+
EmailAddress = "[email protected]"
34+
});
35+
dbContext.People.Insert(new Person()
36+
{
37+
Name = "Geovanni Perez",
38+
Age = 32,
39+
EmailAddress = "[email protected]"
40+
});
3641

37-
dbContext.People.Insert(new Person()
38-
{
39-
Name = "Luis Gonzalez",
40-
Age = 29,
41-
EmailAddress = "[email protected]"
42-
});
42+
dbContext.People.Insert(new Person()
43+
{
44+
Name = "Luis Gonzalez",
45+
Age = 29,
46+
EmailAddress = "[email protected]"
47+
});
48+
#endif
4349

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

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

6975
// Register the Web Api Module. See the Setup method to find out how to do it
7076
// It registers the WebApiModule and registers the controller(s) -- that's all.
7177
server.WithWebApiController<PeopleController>();
72-
73-
#if NET452
78+
7479
// Register the WebSockets module. See the Setup method to find out how to do it
7580
// It registers the WebSocketsModule and registers the server for the given paths(s)
7681
WebSocketsSample.Setup(server);
77-
#endif
7882

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

8589
// Fire up the browser to show the content!
8690
#if DEBUG
87-
#if NET452
91+
#if !NETCOREAPP1_0
8892
var browser = new System.Diagnostics.Process()
8993
{
9094
StartInfo = new System.Diagnostics.ProcessStartInfo(url)

src/Unosquare.Labs.EmbedIO.Samples/StaticFilesSample.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public static string HtmlRootPath
1919
{
2020
get
2121
{
22-
var assemblyPath = Path.GetDirectoryName(typeof (Program).GetTypeInfo().Assembly.Location);
22+
var assemblyPath = Path.GetDirectoryName(typeof(Program).GetTypeInfo().Assembly.Location);
2323

24-
#if DEBUG
25-
#if NET452
2624
// This lets you edit the files without restarting the server.
27-
return Path.Combine(Directory.GetParent(assemblyPath).Parent.Parent.Parent.FullName, "html");
28-
#else
25+
#if DEBUG && !MONO
26+
#if NETCOREAPP1_0
2927
return Path.Combine(Directory.GetParent(assemblyPath).Parent.Parent.FullName, "html");
28+
#else
29+
return Path.Combine(Directory.GetParent(assemblyPath).Parent.Parent.Parent.FullName, "html");
3030
#endif
3131
#else
3232
// This is when you have deployed the server.
@@ -39,12 +39,13 @@ public static string HtmlRootPath
3939
/// Setups the specified server.
4040
/// </summary>
4141
/// <param name="server">The server.</param>
42-
public static void Setup(WebServer server)
42+
public static void Setup(WebServer server, bool useGzip)
4343
{
4444
server.RegisterModule(new StaticFilesModule(HtmlRootPath));
4545
// The static files module will cache small files in ram until it detects they have been modified.
4646
server.Module<StaticFilesModule>().UseRamCache = false;
4747
server.Module<StaticFilesModule>().DefaultExtension = ".html";
48+
server.Module<StaticFilesModule>().UseGzip = useGzip;
4849
// We don't need to add the line below. The default document is always index.html.
4950
//server.Module<StaticFilesWebModule>().DefaultDocument = "index.html";
5051
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{A45D13E0-4138-41A1-9C42-8F6BB9474BA6}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Unosquare.Labs.EmbedIO.Samples</RootNamespace>
11+
<AssemblyName>Unosquare.Labs.EmbedIO.Samples</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>TRACE;DEBUG;MONO</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.ComponentModel.DataAnnotations" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Net.Http" />
44+
<Reference Include="System.Xml" />
45+
</ItemGroup>
46+
<ItemGroup>
47+
<Compile Include="AppDbContext.cs" />
48+
<Compile Include="PeopleController.cs" />
49+
<Compile Include="Person.cs" />
50+
<Compile Include="Program.cs" />
51+
<Compile Include="Properties\AssemblyInfo.cs" />
52+
<Compile Include="StaticFilesSample.cs" />
53+
<Compile Include="WebSocketsSample.cs" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<Content Include="html\css\embedio-icon.png">
57+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
58+
</Content>
59+
<Content Include="html\css\embedio.png">
60+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
61+
</Content>
62+
<Content Include="html\css\theme.css">
63+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
64+
</Content>
65+
<Content Include="html\favicon.ico">
66+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
67+
</Content>
68+
<Content Include="html\index.html">
69+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
70+
</Content>
71+
<Content Include="html\partials\app-menu.html">
72+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
73+
</Content>
74+
<Content Include="html\partials\app-person.html">
75+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
76+
</Content>
77+
<Content Include="html\scripts\app.controllers.js">
78+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
79+
</Content>
80+
<Content Include="html\scripts\app.directives.js">
81+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
82+
</Content>
83+
<Content Include="html\scripts\app.js">
84+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
85+
</Content>
86+
<Content Include="html\scripts\app.routes.js">
87+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
88+
</Content>
89+
<Content Include="html\scripts\app.services.js">
90+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
91+
</Content>
92+
<Content Include="html\scripts\tubular\tubular-bundle.css">
93+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
94+
</Content>
95+
<Content Include="html\scripts\tubular\tubular-bundle.js">
96+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
97+
</Content>
98+
<Content Include="html\scripts\tubular\tubular-bundle.min.css">
99+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
100+
</Content>
101+
<Content Include="html\scripts\tubular\tubular-bundle.min.js">
102+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
103+
</Content>
104+
<Content Include="html\views\chat.html">
105+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
106+
</Content>
107+
<Content Include="html\views\cmd.html">
108+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
109+
</Content>
110+
<Content Include="html\views\home.html">
111+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
112+
</Content>
113+
<Content Include="html\views\people.html">
114+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
115+
</Content>
116+
<Content Include="html\views\tubular.html">
117+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
118+
</Content>
119+
</ItemGroup>
120+
<ItemGroup>
121+
<ProjectReference Include="..\Unosquare.Labs.EmbedIO\Unosquare.Labs.EmbedIO.csproj">
122+
<Project>{7d7c29b4-9493-4ebd-8f20-6fac1e7161ee}</Project>
123+
<Name>Unosquare.Labs.EmbedIO</Name>
124+
</ProjectReference>
125+
</ItemGroup>
126+
<ItemGroup>
127+
<None Include="app.config" />
128+
<None Include="packages.config" />
129+
</ItemGroup>
130+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
131+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
132+
Other similar extension points exist, see Microsoft.Common.targets.
133+
<Target Name="BeforeBuild">
134+
</Target>
135+
<Target Name="AfterBuild">
136+
</Target>
137+
-->
138+
</Project>

0 commit comments

Comments
 (0)