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

Commit

Permalink
JS doc generation enhancements , ClientJsAPI fixes, samples enhanceme…
Browse files Browse the repository at this point in the history
…nts.
  • Loading branch information
dajuric committed Jan 2, 2018
1 parent cc91ac5 commit c88be10
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 35 deletions.
11 changes: 1 addition & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
**WebSokcetRPC** - RPC over websocket for .NET
Lightweight .NET framework for making RPC over websockets. Supports full duplex connections; .NET or Javascript clients.

<!--
> **Tutorial:** <a href="https://www.codeproject.com/Articles/0000/Lightweight-WebSocket-RPC-library" target="_blank">CodeProject article</a>
-->
> **Tutorial:** <a href="https://www.codeproject.com/Articles/1210957/Introducing-Lightweight-WebSocket-RPC-Library-for" target="_blank">CodeProject article</a>

## Why WebSocketRPC ?
Expand Down Expand Up @@ -193,13 +191,6 @@ api.connect(async () =>

{empty} .


## Getting started
+ Samples
<!--
+ <a href="https://www.codeproject.com/Articles/0000/Lightweight-WebSocket-RPC-library" target="_blank">CodeProject article</a>
-->

## How to Engage, Contribute and Provide Feedback
Remember: Your opinion is important and will define the future roadmap.
+ questions, comments - Github
Expand Down
14 changes: 7 additions & 7 deletions Samples/ClientJs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
namespace ClientJs
{
/// <summary>
/// Remote API.
/// Progress API.
/// </summary>
interface IRemoteAPI
interface IProgressAPI
{
/// <summary>
/// Writes progress.
Expand All @@ -21,9 +21,9 @@ interface IRemoteAPI
}

/// <summary>
/// Local API.
/// Task API.
/// </summary>
class LocalAPI
class TaskAPI
{
/// <summary>
/// Executes long running addition task.
Expand All @@ -36,7 +36,7 @@ public async Task<int> LongRunningTask(int a, int b)
for (var p = 0; p <= 100; p += 5)
{
await Task.Delay(250);
await RPC.For<IRemoteAPI>(this).CallAsync(x => x.WriteProgress((float)p / 100));
await RPC.For<IProgressAPI>(this).CallAsync(x => x.WriteProgress((float)p / 100));
}

return a + b;
Expand All @@ -50,13 +50,13 @@ class Program
static void Main(string[] args)
{
//generate js code
File.WriteAllText($"./Site/{nameof(LocalAPI)}.js", RPCJs.GenerateCallerWithDoc<LocalAPI>());
File.WriteAllText($"./Site/{nameof(TaskAPI)}.js", RPCJs.GenerateCallerWithDoc<TaskAPI>());

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

Expand Down
4 changes: 2 additions & 2 deletions Samples/ClientJs/Site/Index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<html>
<head>
<title>Client JS</title>
<script src="LocalAPI.js"></script>
<script src="TaskAPI.js"></script>
</head>

<body>
Expand All @@ -21,7 +21,7 @@
}

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

//implement the interface
api.writeProgress = p =>
Expand Down
8 changes: 4 additions & 4 deletions Samples/ServerClientSample/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace TestClient
{
interface ILocalAPI
interface ITaskAPI
{
Task<int> LongRunningTask(int a, int b);
}

class RemoteAPI //:IRemoteAPI
class ProgressAPI //:ITaskAPI
{
public void WriteProgress(float progress)
{
Expand All @@ -34,10 +34,10 @@ static void Main(string[] args)
var cts = new CancellationTokenSource();
var t = Client.ConnectAsync("ws://localhost:8001/", cts.Token, c =>
{
c.Bind<RemoteAPI, ILocalAPI>(new RemoteAPI());
c.Bind<ProgressAPI, ITaskAPI>(new ProgressAPI());
c.OnOpen += async () =>
{
var r = await RPC.For<ILocalAPI>().CallAsync(x => x.LongRunningTask(5, 3));
var r = await RPC.For<ITaskAPI>().CallAsync(x => x.LongRunningTask(5, 3));
Console.WriteLine("\nResult: " + r.First());
};
},
Expand Down
10 changes: 5 additions & 5 deletions Samples/ServerClientSample/Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

namespace TestServer
{
interface IRemoteAPI
interface IProgressAPI
{
void WriteProgress(float progress);
}

class LocalAPI
class TaskAPI
{
public async Task<int> LongRunningTask(int a, int b)
{
for (var p = 0; p <= 100; p += 5)
{
await Task.Delay(250);
await RPC.For<IRemoteAPI>(this).CallAsync(x => x.WriteProgress((float)p / 100));
await RPC.For<IProgressAPI>(this).CallAsync(x => x.WriteProgress((float)p / 100));
}

return a + b;
Expand All @@ -38,7 +38,7 @@ static void Main(string[] args)
var cts = new CancellationTokenSource();
var t = Server.ListenAsync("http://localhost:8001/", cts.Token, (c, wc) =>
{
c.Bind<LocalAPI, IRemoteAPI>(new LocalAPI());
c.Bind<TaskAPI, IProgressAPI>(new TaskAPI());

c.OnOpen += () => Task.Run((Action)writeClientCount);
c.OnClose += (s, d) => Task.Run((Action)writeClientCount);
Expand All @@ -50,7 +50,7 @@ static void Main(string[] args)

static void writeClientCount()
{
var cc = RPC.For<IRemoteAPI>().Count();
var cc = RPC.For<IProgressAPI>().Count();
Console.WriteLine("Client count: " + cc);
}
}
Expand Down
2 changes: 0 additions & 2 deletions Source/WebSocketRPC.JS/ClassDiagram1.cd

This file was deleted.

12 changes: 11 additions & 1 deletion Source/WebSocketRPC.JS/Components/JsDocGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace WebSocketRPC
Expand Down Expand Up @@ -97,7 +99,7 @@ public static string GetMethodDoc(XmlNodeList mmebers, string methodName,
jsDoc.AppendLine(String.Format("{0} * @param {{{1}}} - {2}", linePrefix, pTypes[i].Name, p[pNames[i]]));
}

jsDoc.AppendLine(String.Format("{0} * @returns {{{1}}} - {2}", linePrefix, returnType.Name, r));
jsDoc.AppendLine(String.Format("{0} * @returns {{{1}}} - {2}", linePrefix, getTypeName(returnType), r));
}
jsDoc.AppendLine(String.Format("{0}*/", linePrefix));

Expand Down Expand Up @@ -171,5 +173,13 @@ static string getReturn(XmlNode node)

return s;
}

static string getTypeName(Type type)
{
if (type.GetGenericTypeDefinition() != typeof(Task<>))
return type.Name;

return type.GenericTypeArguments.First().Name + " (Task)";
}
}
}
4 changes: 2 additions & 2 deletions Source/WebSocketRPC.JS/Resources/ClientAPIBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ this.onOtherMessage = null;
* Send the message using the underlying websocket connection.
* @param {Message} - message.
*/
this.send = (msg) => ws.send(msg);
this.send = function (message) { ws.send(message); }

/*
* Closes the underlying websocket connection.
* @param {number} - Status code (see https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes for details).
* @param {string} - Human readable close reason (the max length is 123 bytes / ASCII characters).
*/
this.close = (code, closeReason) =>
this.close = function (code, closeReason)
{
code = (code === undefined) ? 1000 : code;
closeReason = closeReason || "";
Expand Down
4 changes: 2 additions & 2 deletions Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ static void Main(string[] args)
//RunTest(TestConnectionException);

//RunTest(TestRpcInitializeException);
//RunTest(TestRpcUnhandledException);
RunTest(TestRpcUnhandledException);
//RunTest(TestRpcHandledException);

//RunTest(TestMaxMessageSize);

//RunTest(TestTimeout);
RunTest(TestRpcTimeout);
//RunTest(TestRpcTimeout);

//RunTest(TestMultiClient);

Expand Down

0 comments on commit c88be10

Please sign in to comment.