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

Commit

Permalink
Adding sample, minor bug fixing.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajuric committed Jan 15, 2018
1 parent 18c3568 commit 764b4e5
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 13 deletions.
31 changes: 31 additions & 0 deletions Samples/ClientInfoJs/ClientInfoJs.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;net47</TargetFrameworks>
</PropertyGroup>

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

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

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

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

<ItemGroup>
<None Update="Site\Index.html">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

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

namespace ClientInfoJs
{
class PlatformInfo
{
internal async Task InitializeAsync()
{
var browserInfo = await RPC.For<IBrowserInfo>(this)
.CallAsync(x => x.GetBrowserInfo());

Console.WriteLine("\nBrowser info: " + browserInfo.First());
}

/* There are no public methods so the API is 'empty' from a client's perspective */
}

interface IBrowserInfo
{
string GetBrowserInfo();
}

class Program
{
//if access denied execute: "netsh http delete urlacl url=http://+:8001/" (delete for 'localhost', add for public address)
//open Index.html to run the client
static void Main(string[] args)
{
//generate js code (the API is empty)
File.WriteAllText($"./Site/{nameof(PlatformInfo)}.js", RPCJs.GenerateCaller<PlatformInfo>());

//start server and bind its local and remote API
var cts = new CancellationTokenSource();
var t = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, ws) =>
{
var pInfo = new PlatformInfo();
c.Bind<PlatformInfo, IBrowserInfo>(pInfo);
c.OnOpen += pInfo.InitializeAsync;
});

Console.Write("{0} ", nameof(ClientInfoJs));
Process.Start(new ProcessStartInfo(Path.GetFullPath("./Site/Index.html")) { UseShellExecute= true });
AppExit.WaitFor(cts, t);
}
}
}
33 changes: 33 additions & 0 deletions Samples/ClientInfoJs/Site/Index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<html>
<head>
<title>Client JS</title>
<script src="PlatformInfo.js"></script>
</head>

<body>
<p id="msg"></p>

<script>
function writeMsg(msg, color)
{
color = color || 'black';

var p = document.getElementById("msg");
p.innerHTML = JSON.stringify(msg, null, "\t");
p.style.color = color;
}

//init API
var api = new PlatformInfo("ws://localhost:8001");

//implement the interface
api.getBrowserInfo = () => navigator.userAgent;

//connect
api.connect(() => writeMsg("Connecton established.", 'green'),
err => writeMsg(err, 'red'),
msg => writeMsg(msg));
</script>

</body>
</html>
16 changes: 11 additions & 5 deletions Samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,29 @@ The recommendation is to run samples in the following order (from the more simpl
Two way RPC: .NET <-> .NET.
*Server is executing long running task on the client's request and sending progress notifications to to client.*

3. **ClientJs**
3. **ClientInfoJs**
Javascript client and C# server.
One way RPC: .NET -> Javascript.
*Server is calling a remote browser info function implemented on a JavaScript client.
Client's code is autogenerated.*

4. **ClientJs**
Javascript client and C# server.
Two way RPC: .NET <-> Javascript.
*Server is executing long running task on the client's request and sending progress notifications to to client.
*Server is executing long running task on the client's request and sending progress notifications to to client.
Client's code is autogenerated.*

4. **MultiService**
5. **MultiService**
Javascript client(s) and C# server.
Introduction to multi-services RPC.
*Server has two services: numeric and text services / classes. Two Javascript files for each of the services are autogenerated.*

5. **Serialization**
6. **Serialization**
Javascript client and C# server.
Image serialization example.
*Server has image-processing service which receives an image url and gives processed image back to a Javascript client.*

6. **AspRpc**
7. **AspRpc**
Javascript client and C# server.
ASP.NET library integration.
*Server has reporting service initiatted and stopped on the client's request.*
Expand Down
2 changes: 1 addition & 1 deletion Samples/SampleBase/AppExit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static void WaitFor(CancellationTokenSource cts, params Task[] tasks)

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

cancelTasks(cts);
Expand Down
2 changes: 1 addition & 1 deletion Source/WebSocketRPC.AspCore/WebSocketRPC.AspCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<PackageTags>websocket; ASP.NET; RPC; C#; .NET</PackageTags>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>

<Version>1.0.5</Version>
<Version>1.0.6</Version>
<PackageOutputPath>../../Deploy/Nuget/bin/</PackageOutputPath>
<RootNamespace>WebSocketRPC</RootNamespace>

Expand Down
5 changes: 3 additions & 2 deletions Source/WebSocketRPC.JS/RPCJs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

Expand All @@ -42,14 +43,14 @@ public static class RPCJs
/// <typeparam name="T">Class or interface type.</typeparam>
/// <param name="settings">RPC-Js settings used for Javascript code generation.</param>
/// <returns>Javascript API.</returns>
public static string GenerateCaller<T>(RPCJsSettings<T> settings = null)
public static string GenerateCaller<T>(RPCJsSettings<T> settings = null)
{
settings = settings ?? new RPCJsSettings<T>();
var (tName, mInfos) = JsCallerGenerator.GetMethods(settings.OmittedMethods);
tName = settings.NameOverwrite ?? tName;

var sb = new StringBuilder();
if(settings.WithRequireSupport) sb.Append(JsCallerGenerator.GenerateRequireJsHeader(tName));
if (settings.WithRequireSupport) sb.Append(JsCallerGenerator.GenerateRequireJsHeader(tName));
sb.Append(JsCallerGenerator.GenerateHeader(tName));

foreach (var m in mInfos)
Expand Down
2 changes: 1 addition & 1 deletion Source/WebSocketRPC.JS/Resources/ClientAPIBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function onCallRequest(data, onError)
}

var r = null, errMsg = null;
try { obj[jsonFName].apply(obj, data.Arguments); }
try { r = obj[jsonFName].apply(obj, data.Arguments); }
catch (e) { errMsg = e; }

if (r === null || r === undefined) r = true;
Expand Down
2 changes: 1 addition & 1 deletion Source/WebSocketRPC.JS/WebSocketRPC.JS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<PackageTags>websocket; websocket-client; Javascript; RPC; C#; .NET</PackageTags>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>

<Version>1.0.5</Version>
<Version>1.0.6</Version>
<PackageOutputPath>../../Deploy/Nuget/bin/</PackageOutputPath>
<RootNamespace>WebSocketRPC</RootNamespace>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<PackageTags>websocket; websocket-client; websocket-server; RPC; C#; .NET</PackageTags>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>

<Version>1.0.5</Version>
<Version>1.0.6</Version>
<PackageOutputPath>../../Deploy/Nuget/bin/</PackageOutputPath>

<IncludeSource>True</IncludeSource>
Expand Down
9 changes: 8 additions & 1 deletion WebsocketRPC.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
VisualStudioVersion = 15.0.27130.2020
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{3CE373E0-6D9A-48C4-808D-510E6FB69D5C}"
EndProject
Expand Down Expand Up @@ -67,6 +67,8 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "SampleBase", "Samples\Sampl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{96225EA6-4EA7-48AB-91F9-E53B0B5605F0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClientInfoJs", "Samples\ClientInfoJs\ClientInfoJs.csproj", "{4F0482C4-83EE-4C2D-9B74-FE664179C891}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
Samples\SampleBase\SampleBase.projitems*{4c3c0292-f1f5-4997-8a0e-b4c7d05bb22f}*SharedItemsImports = 13
Expand Down Expand Up @@ -121,6 +123,10 @@ Global
{96225EA6-4EA7-48AB-91F9-E53B0B5605F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96225EA6-4EA7-48AB-91F9-E53B0B5605F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96225EA6-4EA7-48AB-91F9-E53B0B5605F0}.Release|Any CPU.Build.0 = Release|Any CPU
{4F0482C4-83EE-4C2D-9B74-FE664179C891}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F0482C4-83EE-4C2D-9B74-FE664179C891}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F0482C4-83EE-4C2D-9B74-FE664179C891}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F0482C4-83EE-4C2D-9B74-FE664179C891}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -142,6 +148,7 @@ Global
{76FDC1DC-D80A-42E8-8A5D-2FE3462B9E3F} = {E5509F73-1E5E-45B4-AED7-4A38F8DF1DDE}
{4C3C0292-F1F5-4997-8A0E-B4C7D05BB22F} = {E5509F73-1E5E-45B4-AED7-4A38F8DF1DDE}
{96225EA6-4EA7-48AB-91F9-E53B0B5605F0} = {01049849-1A9A-4C3A-BD56-EA6B628F9F36}
{4F0482C4-83EE-4C2D-9B74-FE664179C891} = {E5509F73-1E5E-45B4-AED7-4A38F8DF1DDE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {591A6475-8DF2-42DA-AFF1-8EF88BCF6EE4}
Expand Down

0 comments on commit 764b4e5

Please sign in to comment.