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

Commit

Permalink
Enhancing samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajuric committed Dec 29, 2017
1 parent 2cbca7d commit 507361a
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 41 deletions.
4 changes: 3 additions & 1 deletion Samples/ClientJs/ClientJs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<OutputType>Exe</OutputType>
<StartupObject />

<DocumentationFile>bin\$(TargetFramework)\ServerClientJs.xml</DocumentationFile>
<DocumentationFile>bin\$(TargetFramework)\ClientJs.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -26,4 +26,6 @@
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

<Import Project="..\SampleBase\SampleBase.projitems" Label="Shared" />
</Project>
13 changes: 6 additions & 7 deletions Samples/ClientJs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using SampleBase;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using WebSocketRPC;

namespace RawMsgJs
namespace ClientJs
{
/// <summary>
/// Remote API.
Expand Down Expand Up @@ -52,18 +53,16 @@ static void Main(string[] args)

//start server and bind its local and remote API
var cts = new CancellationTokenSource();
var s = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) =>
var t = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) =>
{
c.Bind<LocalAPI, IRemoteAPI>(new LocalAPI());
//c.BindTimeout(TimeSpan.FromSeconds(1)); //close connection if there is no incommming message after X seconds

c.OnOpen += async () => await c.SendAsync("Hello from server using WebSocketRPC", RPCSettings.Encoding);
});

Console.Write("Running: '{0}'. Press [Enter] to exit.", nameof(RawMsgJs));
Console.ReadLine();
cts.Cancel();
s.Wait();
Console.Write("{0} ", nameof(ClientJs));
AppExit.WaitFor(cts, t);
}
}
}
2 changes: 2 additions & 0 deletions Samples/MultiService/MultiService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

<Import Project="..\SampleBase\SampleBase.projitems" Label="Shared" />
</Project>
13 changes: 6 additions & 7 deletions Samples/MultiService/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using SampleBase;
using System;
using System.IO;
using System.Threading;
using WebSocketRPC;
Expand Down Expand Up @@ -51,7 +52,7 @@ static void Main(string[] args)

//start server and bind its local and remote APIs
var cts = new CancellationTokenSource();
Server.ListenAsync("http://localhost:8001/", cts.Token, (c, wc) =>
var t = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, wc) =>
{
var path = wc.RequestUri.AbsolutePath;
if (path == "/numericService")
Expand All @@ -62,12 +63,10 @@ static void Main(string[] args)
{
c.Bind(new TextService());
}
})
.Wait(0);
});

Console.Write("Running: '{0}'. Press [Enter] to exit.", nameof(MultiService));
Console.ReadLine();
cts.Cancel();
Console.Write("{0} ", nameof(MultiService));
AppExit.WaitFor(cts, t);
}
}
}
11 changes: 5 additions & 6 deletions Samples/RawMsgJs/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using SampleBase;
using System;
using System.IO;
using System.Threading;
using WebSocketRPC;
Expand All @@ -23,7 +24,7 @@ static void Main(string[] args)

//start server
var cts = new CancellationTokenSource();
var s = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) =>
var t = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) =>
{
//set idle timeout
c.BindTimeout(TimeSpan.FromSeconds(30));
Expand All @@ -43,10 +44,8 @@ static void Main(string[] args)
};
});

Console.WriteLine("Running: '{0}'. Press [Enter] to exit.", nameof(RawMsgJs));
Console.ReadLine();
cts.Cancel();
s.Wait();
Console.Write("{0} ", nameof(RawMsgJs));
AppExit.WaitFor(cts, t);
}
}
}
4 changes: 2 additions & 2 deletions Samples/RawMsgJs/RawMsgJs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
<ApplicationIcon />
<OutputType>Exe</OutputType>
<StartupObject />

<DocumentationFile>bin\$(TargetFramework)\ServerClientJs.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -26,4 +24,6 @@
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

<Import Project="..\SampleBase\SampleBase.projitems" Label="Shared" />
</Project>
61 changes: 61 additions & 0 deletions Samples/SampleBase/AppExit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace SampleBase
{
static class AppExit
{
public static void WaitFor(CancellationTokenSource cts, params Task[] tasks)
{
if (cts == null)
throw new ArgumentNullException(nameof(cts));

if (tasks == null)
throw new ArgumentNullException(nameof(tasks));

Task.Run(() =>
{
Console.WriteLine("------Press [Enter] or [Ctrl+C] to stop------");
Console.ReadLine();

cancelTasks(cts);
});

waitTasks(tasks);
}

static void cancelTasks(CancellationTokenSource cts)
{
Console.WriteLine("\nWaiting for the tasks to complete...");
cts.Cancel();
}

static void waitTasks(Task[] tasks)
{
try
{
//wait for the competition
foreach (var t in tasks) //enables exception handling
t.Wait();
}
catch (Exception ex)
{
writeError(ex);
}
}

static void writeError(Exception ex)
{
if (ex == null)
return;

if (ex is AggregateException)
ex = ex.InnerException;

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Error: " + ex.Message);
Console.ResetColor();
}
}
}
14 changes: 14 additions & 0 deletions Samples/SampleBase/SampleBase.projitems
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<HasSharedItems>true</HasSharedItems>
<SharedGUID>4c3c0292-f1f5-4997-8a0e-b4c7d05bb22f</SharedGUID>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace>SampleBase</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)AppExit.cs" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions Samples/SampleBase/SampleBase.shproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<ProjectGuid>4c3c0292-f1f5-4997-8a0e-b4c7d05bb22f</ProjectGuid>
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
<PropertyGroup />
<Import Project="SampleBase.projitems" Label="Shared" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
</Project>
9 changes: 4 additions & 5 deletions Samples/Serialization/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using WebSocketRPC;
using System.Runtime.CompilerServices;
using SampleBase;

namespace Serialization
{
Expand Down Expand Up @@ -85,12 +86,10 @@ static void Main(string[] args)

//start server and bind its local and remote API
var cts = new CancellationTokenSource();
Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) => c.Bind(new ImageProcessingAPI())).Wait();
var t = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) => c.Bind(new ImageProcessingAPI()));


Console.Write("Running: '{0}'. Press [Enter] to exit.", nameof(Serialization));
Console.ReadLine();
cts.Cancel();
Console.Write("{0} ", nameof(Serialization));
AppExit.WaitFor(cts, t);
}
}
}
2 changes: 2 additions & 0 deletions Samples/Serialization/Serialization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

<Import Project="..\SampleBase\SampleBase.projitems" Label="Shared" />
</Project>
2 changes: 2 additions & 0 deletions Samples/ServerClientSample/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
<ItemGroup>
<ProjectReference Include="..\..\..\Source\WebsocketRPC.Standalone\WebsocketRPC.Standalone.csproj" />
</ItemGroup>

<Import Project="..\..\SampleBase\SampleBase.projitems" Label="Shared" />
</Project>
13 changes: 6 additions & 7 deletions Samples/ServerClientSample/Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using SampleBase;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -31,20 +32,18 @@ static void Main(string[] args)

//start client and bind to its local API
var cts = new CancellationTokenSource();
Client.ConnectAsync("ws://localhost:8001/", cts.Token, c =>
var t = Client.ConnectAsync("ws://localhost:8001/", cts.Token, c =>
{
c.Bind<RemoteAPI, ILocalAPI>(new RemoteAPI());
c.OnOpen += async () =>
{
var r = await RPC.For<ILocalAPI>().CallAsync(x => x.LongRunningTask(5, 3));
Console.WriteLine("\nResult: " + r.First());
};
})
.Wait(0);
});

Console.Write("Running: '{0}'. Press [Enter] to exit.\n", nameof(TestClient));
Console.ReadLine();
cts.Cancel();
Console.Write("{0} ", nameof(TestClient));
AppExit.WaitFor(cts, t);
}
}
}
10 changes: 5 additions & 5 deletions Samples/ServerClientSample/Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using SampleBase;
using System;
using System.Threading;
using System.Threading.Tasks;
using WebSocketRPC;
Expand Down Expand Up @@ -34,11 +35,10 @@ static void Main(string[] args)

//start server and bind to its local and remote API
var cts = new CancellationTokenSource();
Server.ListenAsync("http://localhost:8001/", cts.Token, (c, wc) => c.Bind<LocalAPI, IRemoteAPI>(new LocalAPI())).Wait(0);
var t = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, wc) => c.Bind<LocalAPI, IRemoteAPI>(new LocalAPI()));

Console.Write("Running: '{0}'. Press [Enter] to exit.", nameof(TestServer));
Console.ReadLine();
cts.Cancel();
Console.Write("{0} ", nameof(TestServer));
AppExit.WaitFor(cts, t);
}
}
}
2 changes: 2 additions & 0 deletions Samples/ServerClientSample/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
<ItemGroup>
<ProjectReference Include="..\..\..\Source\WebsocketRPC.Standalone\WebsocketRPC.Standalone.csproj" />
</ItemGroup>

<Import Project="..\..\SampleBase\SampleBase.projitems" Label="Shared" />
</Project>
4 changes: 3 additions & 1 deletion Source/WebsocketRPC.Standalone/ClientServer/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ public static async Task ListenAsync(string httpListenerPrefix, CancellationToke
catch (Exception ex) when ((ex as HttpListenerException)?.ErrorCode == 5)
{
throw new UnauthorizedAccessException($"The HTTP server can not be started, as the namespace reservation does not exist.\n" +
$"Please run (elevated): 'netsh http add urlacl url={httpListenerPrefix} user=\"Everyone\"'.", ex);
$"Please run (elevated): 'netsh http add urlacl url={httpListenerPrefix} user=\"Everyone\"'." +
$"\nRemarks:\n" +
$" If using 'localhost', put 'delete' instead of 'add' and type the http prefix instead of 'localhost'.", ex);
}

//helpful: https://stackoverflow.com/questions/11167183/multi-threaded-httplistener-with-await-async-and-tasks
Expand Down
4 changes: 4 additions & 0 deletions WebsocketRPC.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{01049849
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RawMsgJs", "Samples\RawMsgJs\RawMsgJs.csproj", "{76FDC1DC-D80A-42E8-8A5D-2FE3462B9E3F}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "SampleBase", "Samples\SampleBase\SampleBase.shproj", "{4C3C0292-F1F5-4997-8A0E-B4C7D05BB22F}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
Samples\SampleBase\SampleBase.projitems*{4c3c0292-f1f5-4997-8a0e-b4c7d05bb22f}*SharedItemsImports = 13
Source\WebSocketRPC.Base\WebSocketRPC.Base.projitems*{eaebaa06-d5bb-4106-8694-c022832175d6}*SharedItemsImports = 13
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -131,6 +134,7 @@ Global
{CE4BAD88-2358-4B3D-8A34-90A74C73E521} = {3CE373E0-6D9A-48C4-808D-510E6FB69D5C}
{CFC334A5-2B52-42A0-87AE-4B3F84B09530} = {E5509F73-1E5E-45B4-AED7-4A38F8DF1DDE}
{76FDC1DC-D80A-42E8-8A5D-2FE3462B9E3F} = {E5509F73-1E5E-45B4-AED7-4A38F8DF1DDE}
{4C3C0292-F1F5-4997-8A0E-B4C7D05BB22F} = {E5509F73-1E5E-45B4-AED7-4A38F8DF1DDE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {591A6475-8DF2-42DA-AFF1-8EF88BCF6EE4}
Expand Down

0 comments on commit 507361a

Please sign in to comment.