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

Commit

Permalink
Minor fixes; fixing error on close.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajuric committed Oct 14, 2017
1 parent aeb3ba1 commit 016dea3
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Samples/ClientJs/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace TestClientJs
{
interface IRemoteAPI
{
bool WriteProgress(float progress);
void WriteProgress(float progress);
}

class LocalAPI
Expand Down
1 change: 0 additions & 1 deletion Samples/ClientJs/Site/Index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
api.writeProgress = function (p)
{
document.getElementById("progress").innerHTML = "Completed: " + p * 100 + "%";
return true;
}

async function execAPI(api)
Expand Down
1 change: 0 additions & 1 deletion Samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ The recommendation is to run samples in the following order (from the more simpl
2. ServerClientJs
3. MultiService
4. Serialization
5. RelayConnectionSample

**Remarks**
+ If a sample contains JavaScript client, the additional step is to open the included Index.html.
Expand Down
4 changes: 2 additions & 2 deletions Samples/ServerClientSample/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class RemoteAPI //:IRemoteAPI
{
public void WriteProgress(float progress)
{
Console.WriteLine("\rCompleted: {0}%.", progress * 100);
Console.Write("\rCompleted: {0}%.", progress * 100);
}
}

Expand All @@ -37,7 +37,7 @@ static void Main(string[] args)
c.OnOpen += async () =>
{
var r = await RPC.For<ILocalAPI>().CallAsync(x => x.LongRunningTask(5, 3));
Console.WriteLine("Result: " + r.First());
Console.WriteLine("\nResult: " + r.First());
};
})
.Wait(0);
Expand Down
1 change: 1 addition & 0 deletions Source/WebSocketRPC.JS/ClientAPIBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function onCallRequest(data, onError)
}

var r = obj[jsonFName].apply(obj, data.Arguments);
if (r === undefined) r = true;
ws.send(JSON.stringify({ FunctionName: data.FunctionName, ReturnValue: r }));
}

Expand Down
17 changes: 11 additions & 6 deletions Source/WebsocketRPC/ClientServer/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ public static class Client
/// <param name="onConnection">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="secondsBetweenReconnect"/> is set to false.</exception>
/// <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,
bool reconnectOnError = true, int secondsBetweenReconnect = 0)
bool reconnectOnError = true, bool reconnectOnClose = false, int secondsBetweenReconnect = 0)
{
var isClosedSuccessfully = true;
var shouldReconnect = false;

do
{
Expand All @@ -60,13 +62,17 @@ public static async Task ConnectAsync(string uri, CancellationToken token, Actio
catch (Exception ex)
{
isClosedSuccessfully = false;
if (!reconnectOnError) throw ex;
if (!reconnectOnError && !reconnectOnClose) throw ex;
}

if (!isClosedSuccessfully && reconnectOnError)
if (token.IsCancellationRequested)
break;

shouldReconnect = (!isClosedSuccessfully && reconnectOnError) || reconnectOnClose;
if (shouldReconnect)
await Task.Delay(TimeSpan.FromSeconds(secondsBetweenReconnect));
}
while (!isClosedSuccessfully && reconnectOnError);
while (shouldReconnect);
}

static async Task<bool> connectAsync(string uri, CancellationToken token, Action<Connection> onConnection, Action<ClientWebSocketOptions> setOptions = null)
Expand All @@ -90,7 +96,6 @@ static async Task<bool> connectAsync(string uri, CancellationToken token, Action
try
{
onConnection(connection);
connection.InvokeOpenAsync();
await Connection.ListenReceiveAsync(connection, token);
}
finally
Expand Down
11 changes: 2 additions & 9 deletions Source/WebsocketRPC/ClientServer/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public async Task CloseAsync(WebSocketCloseStatus closeStatus = WebSocketCloseSt
if (Socket.State != WebSocketState.Open)
return;

await Socket.CloseAsync(closeStatus, statusDescription, CancellationToken.None);
await Socket.CloseOutputAsync(closeStatus, statusDescription, CancellationToken.None);
OnClose?.Invoke();
clearEvents();
}
Expand All @@ -140,6 +140,7 @@ internal static async Task ListenReceiveAsync(Connection connection, Cancellatio
{
try
{
connection.OnOpen?.Invoke();
byte[] receiveBuffer = new byte[RPCSettings.MaxMessageSize];

while (webSocket.State == WebSocketState.Open)
Expand Down Expand Up @@ -184,14 +185,6 @@ internal static async Task ListenReceiveAsync(Connection connection, Cancellatio
}
}

/// <summary>
/// Invokes the open event.
/// </summary>
internal void InvokeOpenAsync()
{
OnOpen?.Invoke();
}

/// <summary>
/// Invokes the error event.
/// </summary>
Expand Down
4 changes: 1 addition & 3 deletions Source/WebsocketRPC/ClientServer/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,11 @@ public static async Task ListenAsync(HttpListenerContext listenerContext, Cancel
try
{
onConnection(connection, webSocketContext);
connection.InvokeOpenAsync();
await Connection.ListenReceiveAsync(connection, token);
}
finally
{
if (webSocket != null)
webSocket.Dispose();
//webSocket?.Dispose();
}
}
}
Expand Down

0 comments on commit 016dea3

Please sign in to comment.