Skip to content
This repository has been archived by the owner on Dec 20, 2019. It is now read-only.

Commit

Permalink
Renaming; base cookie parsing - ASP Core
Browse files Browse the repository at this point in the history
  • Loading branch information
dajuric committed Oct 21, 2017
1 parent 02c9194 commit 15c936c
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<p align="center">
<a href="https://www.nuget.org/packages/WebsocketRPC/"> <img src="https://img.shields.io/badge/WebSokcetRPC-v1.x-blue.svg?style=flat-square" alt="NuGet packages version"/> </a>
<a href="https://www.nuget.org/packages/WebsocketRPC.JS/"> <img src="https://img.shields.io/badge/WebSokcetRPC.JS-v1.x-blue.svg?style=flat-square" alt="NuGet packages version"/> </a>
<a href="https://www.nuget.org/packages/WebsocketRPC.AspCore/"> <img src="https://img.shields.io/badge/WebSokcetRPC.AspCore-v1.x-blue.svg?style=flat-square" alt="NuGet packages version"/> </a>
</p>

**WebSokcetRPC** - RPC over weboskcets for .NET
Expand Down
5 changes: 2 additions & 3 deletions Samples/AspRpc/AspRpc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<PropertyGroup>
<OutputPath>bin\</OutputPath>
<DocumentationFile>bin\netcoreapp2.0\AspRpc.xml</DocumentationFile>
<DocumentationFile>bin\$(TargetFramework)\AspRpc.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -26,7 +26,6 @@

<ItemGroup>
<ProjectReference Include="..\..\Source\WebSocketRPC.AspCore\WebSocketRPC.AspCore.csproj" />
<ProjectReference Include="..\..\Source\WebSocketRPC.JS\WebSocketRPC.JS.csproj" />
<ProjectReference Include="..\..\Source\WebSocketRPC.JS\WebSocketRPC.Js.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Samples/AspRpc/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class HomeController
/// </summary>
/// <returns>Home page HTML code.</returns>
[HttpGet]
public RedirectResult Get()
public RedirectResult Index()
{
return new RedirectResult("/Site/Index.html");
}
Expand Down
6 changes: 3 additions & 3 deletions Samples/AspRpc/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
}



app.UseMvc();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Site")),
RequestPath = "/Site"
});

//initialize web-sockets and bind the service
reportingService = new ReportingService();
app.UseWebSockets();
app.MapWebSocketRPC("/reportingService", (hc, c) => c.Bind<ReportingService, IClientUpdate>(reportingService));
}
}

public class Program
class Program
{
public static void Main(string[] args)
{
Expand Down
2 changes: 1 addition & 1 deletion Samples/ClientJs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Program
static void Main(string[] args)
{
//generate js code
File.WriteAllText($"../Site/{nameof(LocalAPI)}.js", RPCJs.GenerateCallerWithDoc<LocalAPI>());
File.WriteAllText($"./Site/{nameof(LocalAPI)}.js", RPCJs.GenerateCallerWithDoc<LocalAPI>());

//start server and bind its local and remote API
var cts = new CancellationTokenSource();
Expand Down
7 changes: 5 additions & 2 deletions Samples/ClientJs/ServerClientJs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

<PropertyGroup>
<OutputPath>bin\</OutputPath>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
</PropertyGroup>

<ItemGroup>
<None Remove="Site\LocalAPI.js" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Source\WebSocketRPC.JS\WebSocketRPC.JS.csproj" />
<ProjectReference Include="..\..\Source\WebsocketRPC\WebsocketRPC.csproj" />
<ProjectReference Include="..\..\Source\WebSocketRPC.JS\WebSocketRPC.Js.csproj" />
<ProjectReference Include="..\..\Source\WebsocketRPC.Standalone\WebsocketRPC.Standalone.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
7 changes: 5 additions & 2 deletions Samples/MultiService/MultiService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

<PropertyGroup>
<OutputPath>bin\</OutputPath>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Source\WebSocketRPC.JS\WebSocketRPC.JS.csproj" />
<ProjectReference Include="..\..\Source\WebsocketRPC\WebsocketRPC.csproj" />
<ProjectReference Include="..\..\Source\WebSocketRPC.JS\WebSocketRPC.Js.csproj" />
<ProjectReference Include="..\..\Source\WebsocketRPC.Standalone\WebsocketRPC.Standalone.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions Samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The recommendation is to run samples in the following order (from the more simpl
2. ServerClientJs
3. MultiService
4. Serialization
5. AspRpc

**Remarks**
+ If a sample contains JavaScript client, the additional step is to open the included Index.html.
Expand Down
7 changes: 5 additions & 2 deletions Samples/Serialization/Serialization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
<PropertyGroup>
<OutputPath>bin\</OutputPath>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
</PropertyGroup>

<ItemGroup>
<None Remove="Site\LocalAPI.js" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Source\WebSocketRPC.JS\WebSocketRPC.JS.csproj" />
<ProjectReference Include="..\..\Source\WebsocketRPC\WebsocketRPC.csproj" />
<ProjectReference Include="..\..\Source\WebSocketRPC.JS\WebSocketRPC.Js.csproj" />
<ProjectReference Include="..\..\Source\WebsocketRPC.Standalone\WebsocketRPC.Standalone.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 4 additions & 1 deletion Samples/ServerClientSample/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

<PropertyGroup>
<OutputPath>bin\</OutputPath>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Source\WebsocketRPC\WebsocketRPC.csproj" />
<ProjectReference Include="..\..\..\Source\WebsocketRPC.Standalone\WebsocketRPC.Standalone.csproj" />
</ItemGroup>
</Project>
5 changes: 4 additions & 1 deletion Samples/ServerClientSample/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

<PropertyGroup>
<OutputPath>bin\</OutputPath>
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Source\WebsocketRPC\WebsocketRPC.csproj" />
<ProjectReference Include="..\..\..\Source\WebsocketRPC.Standalone\WebsocketRPC.Standalone.csproj" />
</ItemGroup>
</Project>
24 changes: 18 additions & 6 deletions Source/WebSocketRPC.AspCore/WebSokcetRPCMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Net;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -11,18 +12,18 @@ namespace WebSocketRPC
public class WebSocketRPCMiddleware
{
private readonly RequestDelegate next;
private Action<HttpContext, Connection> onConnection;
private Action<HttpContext, Connection> onConnect;

/// <summary>
/// Creates new web-socket RPC middle-ware.
/// </summary>
/// <param name="next">Next middle-ware in the pipeline.</param>
/// <param name="onConnection">Action triggered when a new connection is received.</param>
/// <param name="onConnect">Action triggered when a new connection is received.</param>
public WebSocketRPCMiddleware(RequestDelegate next,
Action<HttpContext, Connection> onConnection)
Action<HttpContext, Connection> onConnect)
{
this.next = next;
this.onConnection = onConnection;
this.onConnect = onConnect;
}

/// <summary>
Expand All @@ -39,17 +40,28 @@ public async Task Invoke(HttpContext context)
}

var socket = await context.WebSockets.AcceptWebSocketAsync();
var connection = new Connection { Socket = socket, Cookies = getCookies(context.Request.Cookies) };

var connection = new Connection { Socket = socket, Cookies = null /*context.Request.Cookies*/ };
try
{
onConnection(context, connection);
onConnect(context, connection);
await Connection.ListenReceiveAsync(connection, CancellationToken.None);
}
finally
{
socket?.Dispose();
}
}

private static CookieCollection getCookies(IRequestCookieCollection cookieCollection)
{
var cc = new CookieCollection();
foreach (var k in cookieCollection.Keys)
{
cc.Add(new Cookie(k, cookieCollection[k]));
}

return cc;
}
}
}
16 changes: 9 additions & 7 deletions Source/WebSocketRPC.AspCore/WebSokcetRPCMiddlewareExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,29 @@ public static class WebSokcetRPCMiddlewareExtensions
{
/// <summary>
/// Branches the request pipeline based on matches of the given request path. If the request path starts with the given path, the branch is executed.
/// <para>Make sure the 'UseWebSockets()' from Microsoft.AspNetCore.WebSockets is called before.</para>
/// </summary>
/// <param name="app">Application builder.</param>
/// <param name="path">The request path to match.</param>
/// <param name="onConnection">Action triggered when a new connection is established.</param>
/// <param name="onConnect">Action triggered when a new connection is established.</param>
/// <returns>Application builder.</returns>
public static IApplicationBuilder MapWebSocketRPC(this IApplicationBuilder app,
PathString path,
Action<HttpContext, Connection> onConnection)
Action<HttpContext, Connection> onConnect)
{
return app.Map(path, (_app) => _app.UseMiddleware<WebSocketRPCMiddleware>(onConnection));
return app.Map(path, (_app) => _app.UseMiddleware<WebSocketRPCMiddleware>(onConnect));
}

/// <summary>
/// Adds a WebSocketRPC middleware to the application's request pipeline.
/// <para>Make sure the 'UseWebSockets()' from Microsoft.AspNetCore.WebSockets is called before.</para>
/// </summary>
/// <param name="app"></param>
/// <param name="onConnection"></param>
/// <param name="app">Application builder.</param>
/// <param name="onConnect">Action triggered when a new connection is established.</param>
/// <returns>Application builder.</returns>
public static IApplicationBuilder UseWebSocketRPC(this IApplicationBuilder app, Action<HttpContext, Connection> onConnection)
public static IApplicationBuilder UseWebSocketRPC(this IApplicationBuilder app, Action<HttpContext, Connection> onConnect)
{
return app.UseMiddleware<WebSocketRPCMiddleware>(onConnection);
return app.UseMiddleware<WebSocketRPCMiddleware>(onConnect);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public static class Client
/// </summary>
/// <param name="uri">The target uri of the format: "ws://(address)/[path]".</param>
/// <param name="token">Cancellation token.</param>
/// <param name="onConnection">Action executed when connection is established.</param>
/// <param name="onConnect">Action executed when connection is established.</param>
/// <param name="setOptions">Websocket option set method.</param>
/// <param name="reconnectOnError">True to reconnect on error, false otherwise.</param>
/// <param name="reconnectOnClose">True to reconnect on normal close request, false otherwise.</param>
/// <param name="secondsBetweenReconnect">The number of seconds between two reconnect attempts.</param>
/// <returns>Client task.</returns>
/// <exception cref="Exception">Socket connection exception thrown in case when <paramref name="reconnectOnError"/> and <paramref name="reconnectOnClose"/> is set to false.</exception>
public static async Task ConnectAsync(string uri, CancellationToken token, Action<Connection> onConnection, Action<ClientWebSocketOptions> setOptions = null,
public static async Task ConnectAsync(string uri, CancellationToken token, Action<Connection> onConnect, Action<ClientWebSocketOptions> setOptions = null,
bool reconnectOnError = true, bool reconnectOnClose = false, int secondsBetweenReconnect = 0)
{
var isClosedSuccessfully = true;
Expand All @@ -57,7 +57,7 @@ public static async Task ConnectAsync(string uri, CancellationToken token, Actio
{
try
{
isClosedSuccessfully = await connectAsync(uri, token, onConnection, setOptions);
isClosedSuccessfully = await connectAsync(uri, token, onConnect, setOptions);
}
catch (Exception ex)
{
Expand Down
1 change: 1 addition & 0 deletions Source/WebSocketRPC.Base/WebSocketRPC.Base.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)ClientServer\ArraySegmentExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ClientServer\Client.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ClientServer\Connection.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ConnectionBinders\Base\Binder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ConnectionBinders\Base\IBinder.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public static class Server
/// </summary>
/// <param name="httpListenerPrefix">The http/https URI listening prefix.</param>
/// <param name="token">Cancellation token.</param>
/// <param name="onConnection">Action executed when connection is created.</param>
/// <param name="onConnect">Action executed when connection is created.</param>
/// <returns>Server task.</returns>
public static async Task ListenAsync(string httpListenerPrefix, CancellationToken token, Action<Connection, WebSocketContext> onConnection)
public static async Task ListenAsync(string httpListenerPrefix, CancellationToken token, Action<Connection, WebSocketContext> onConnect)
{
var listener = new HttpListener();
listener.Prefixes.Add(httpListenerPrefix);
Expand All @@ -55,7 +55,7 @@ public static async Task ListenAsync(string httpListenerPrefix, CancellationToke

if (listenerContext.Request.IsWebSocketRequest)
{
listenAsync(listenerContext, token, onConnection).Wait(0);
listenAsync(listenerContext, token, onConnect).Wait(0);
}
else
{
Expand All @@ -70,7 +70,7 @@ public static async Task ListenAsync(string httpListenerPrefix, CancellationToke
listener.Stop();
}

static async Task listenAsync(HttpListenerContext listenerContext, CancellationToken token, Action<Connection, WebSocketContext> onConnection)
static async Task listenAsync(HttpListenerContext listenerContext, CancellationToken token, Action<Connection, WebSocketContext> onConnect)
{
if (!listenerContext.Request.IsWebSocketRequest)
return;
Expand All @@ -92,7 +92,7 @@ static async Task listenAsync(HttpListenerContext listenerContext, CancellationT
var connection = new Connection { Socket = webSocket, Cookies = webSocketContext.CookieCollection };
try
{
onConnection(connection, webSocketContext);
onConnect(connection, webSocketContext);
await Connection.ListenReceiveAsync(connection, token);
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
</PropertyGroup>

<PropertyGroup>
<RootNamespace>WebsocketRPC</RootNamespace>
<OutputPath>bin\</OutputPath>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions WebsocketRPC.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{E550
Samples\README.md = Samples\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebsocketRPC", "Source\WebsocketRPC\WebsocketRPC.csproj", "{AB4A5DDF-1F91-4AF8-9E9C-242832576C5E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebsocketRPC.Standalone", "Source\WebsocketRPC.Standalone\WebsocketRPC.Standalone.csproj", "{AB4A5DDF-1F91-4AF8-9E9C-242832576C5E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebSocketRPC.JS", "Source\WebSocketRPC.JS\WebSocketRPC.JS.csproj", "{965791BF-8F77-4A69-97E2-8B6B66CF9863}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebSocketRPC.Js", "Source\WebSocketRPC.JS\WebSocketRPC.Js.csproj", "{965791BF-8F77-4A69-97E2-8B6B66CF9863}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ServerClientSample", "ServerClientSample", "{E4A64C0A-0127-42AD-A439-2063A145F536}"
ProjectSection(SolutionItems) = preProject
Expand Down

0 comments on commit 15c936c

Please sign in to comment.