Skip to content

Commit

Permalink
* update readme
Browse files Browse the repository at this point in the history
* simplified configuration creation
  • Loading branch information
dguttman-jacada committed Oct 13, 2020
1 parent b284ed8 commit 25a9422
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 43 deletions.
109 changes: 78 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,104 @@
SockJS.NET
==========
A .NET implementation of the SockJS client
An asynchronous .NET implementation of the SockJS client

Componenets
Components
-----------
### [syp.biz.SockJS.NET.Common](https://github.com/sypbiz/SockJS.NET/tree/master/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Common)
### [`syp.biz.SockJS.NET.Common`](https://github.com/sypbiz/SockJS.NET/tree/master/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Common)
Includes all interfaces, extensions, enums and utils which are common to the client, user of the client and for extending the client.

### [syp.biz.SockJS.NET.Client](https://github.com/sypbiz/SockJS.NET/tree/master/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client)
### [`syp.biz.SockJS.NET.Client`](https://github.com/sypbiz/SockJS.NET/tree/master/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client)
The client library containing the actual SockJS client to be used by consuming applications.

### [syp.biz.SockJS.NET.Test](https://github.com/sypbiz/SockJS.NET/tree/master/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test)
### [`syp.biz.SockJS.NET.Test`](https://github.com/sypbiz/SockJS.NET/tree/master/syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test)
A test application which consumes the client library.

### [server](https://github.com/sypbiz/SockJS.NET/tree/master/server)
### [`server`](https://github.com/sypbiz/SockJS.NET/tree/master/server)
A node.js SockJS server to be used in conjunction with the test application.

Basic Usage
-----
-----------
```csharp
SockJS.SetLogger(new ConsoleLogger()); // sets the global logger
var sockJs = new SockJS("http://localhost:9999/echo"); // creates a client and points it to the local node.js server
sockJs.AddEventListener("open", (sender, e) =>
var sockJs = new SockJS("http://localhost:9999/echo");
sockJs.Connected += async (sender, e) =>
{
Console.WriteLine("Connection opened");
sockJs.Send("foo");
});
sockJs.AddEventListener("message", (sender, e) =>
// this event is triggered once the connection is established
try
{
Console.WriteLine("Connected...");
await sockJs.Send(JsonConvert.SerializeObject(new { foo = "bar" }));
}
catch (Exception ex)
{
Console.WriteLine($"Error: {e}");
}
};

sockJs.Message += async (sender, msg) =>
{
var stringifiedArgs = string.Join(",", e.Select(o => o?.ToString() ?? null));
Console.WriteLine($"Message received: {stringifiedArgs}");
if (e[0] is TransportMessageEvent msg)
// this event is triggered every time a message is received
try
{
var dataString = msg.Data?.ToString();
Console.WriteLine($"Message received: Data = {dataString}");

if (dataString == "foo")
{
Console.WriteLine("Echo successful -> closing connection");
sockJs.Close();
}
Console.WriteLine($"Message: {msg}");
await sockJs.Disconnect(); // disconnect after first received message
}
});
sockJs.AddEventListener("close", (sender, e) =>
catch (Exception ex)
{
Console.WriteLine($"Error: {e}");
}
};

sockJs.Disconnected += (sender, e) =>
{
Console.WriteLine("Connection closed");
});
// this event is triggered when the connection is disconnected (for any reason)
Console.WriteLine("Disconnected");
};

await sockJs.Connect(); // connect to the server
```

Advanced Usage
--------------
### Customize Configuration
```csharp
// create a default configuration file (default values)
var config = Configuration.Factory.BuildDefault("http://localhost:9999/echo");
var sockJs = new SockJs(config);
```
### Customize Logger
```csharp
config.Logger = new ConsoleLogger();
```

### Customize Default Request Headers
```csharp
config.DefaultHeaders = new WebHeaderCollection
{
{HttpRequestHeader.UserAgent, "Custom User Agent"},
{"application-key", "foo-bar"}
};
```

### Customize Transports
```csharp
// add custom transport implementations
config.TransportFactories.Add(new CustomTransportFactory());

// remove custom/built-in transport implementation
config.TransportFactories.Remove(config.TransportFactories.First(t => t.Name == "websocket-system"));

// disable transport
config.TransportFactories.First(t => t.Name == "websocket-system").Enabled = false;
```

Note
----
WebSocket connection is implemented via [System.Net.WebSockets.ClientWebSocket](https://docs.microsoft.com/en-us/dotnet/api/system.net.websockets.clientwebsocket?view=netstandard-2.0) and as such, is not supported on Windows 7, Windows Vista SP2, and Windows Server 2008. See [Remarks section](https://docs.microsoft.com/en-us/dotnet/api/system.net.websockets.clientwebsocket?view=netstandard-2.0#remarks).
Built-in WebSocket connection (`websocket-system`) is implemented via [`System.Net.WebSockets.ClientWebSocket`](https://docs.microsoft.com/en-us/dotnet/api/system.net.websockets.clientwebsocket?view=netstandard-2.0) and as such, is not supported on Windows 7, Windows Vista SP2, and Windows Server 2008. See [Remarks section](https://docs.microsoft.com/en-us/dotnet/api/system.net.websockets.clientwebsocket?view=netstandard-2.0#remarks).

References
----------
This library is a .NET port of the [SockJS Client](https://github.com/sockjs/sockjs-client) JavaScript library and is subject to the same license published [here](https://raw.githubusercontent.com/sockjs/sockjs-client/cc6ae9531bda2d4ee80e52fab246933558790163/LICENSE), via commit **cc6ae9531bda2d4ee80e52fab246933558790163**
This library is based on the [`SockJS-client`](https://github.com/sockjs/sockjs-client) JavaScript library ([license](https://github.com/sockjs/sockjs-client/blob/master/LICENSE)).

Dependencies
------------
- [`Newtonsoft.Json`](https://www.nuget.org/packages/Newtonsoft.Json) ([license](https://github.com/JamesNK/Newtonsoft.Json/blob/master/LICENSE.md))
11 changes: 9 additions & 2 deletions syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ public class Configuration

public static class Factory
{
public static Configuration BuildDefault()
public static Configuration BuildDefault(string baseEndpoint)
{
return BuildDefault(new Uri(baseEndpoint ?? throw new ArgumentNullException(nameof(baseEndpoint))));
}

public static Configuration BuildDefault(Uri baseEndpoint)
{
if (baseEndpoint is null) throw new ArgumentNullException(nameof(baseEndpoint));

return new Configuration
{
BaseEndpoint = baseEndpoint,
TransportFactories = ReflectTransportFactories(),
BaseEndpoint = null,
DefaultHeaders = new WebHeaderCollection(),
Logger = new Implementations.NullLogger(),
InfoReceiverTimeout = TimeSpan.FromSeconds(8),
Expand Down
10 changes: 8 additions & 2 deletions syp.biz/SockJS.NET/syp.biz.SockJS.NET.Client/SockJS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ public class SockJS : IClient
private ITransport? _transport;
private ConnectionState _state = ConnectionState.Initial;

public SockJS(Configuration? config = default)
[SuppressMessage("ReSharper", "UnusedMember.Global")]
public SockJS(string baseEndpoint) : this(Configuration.Factory.BuildDefault(baseEndpoint)) {}

[SuppressMessage("ReSharper", "UnusedMember.Global")]
public SockJS(Uri baseEndpoint) : this(Configuration.Factory.BuildDefault(baseEndpoint)) {}

public SockJS(Configuration config)
{
this._config = (config ?? Configuration.Factory.BuildDefault()).AsReadonly();
this._config = config?.AsReadonly() ?? throw new ArgumentNullException(nameof(config));
this._log = this._config.Logger;
}

Expand Down
11 changes: 3 additions & 8 deletions syp.biz/SockJS.NET/syp.biz.SockJS.NET.Test/ClientTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public async Task Execute()
var tcs = new TaskCompletionSource<bool>();
try
{
var config = Configuration.Factory.BuildDefault();
var config = Configuration.Factory.BuildDefault("http://localhost:9999/echo");
config.Logger = new ConsoleLogger();
config.BaseEndpoint = new Uri("http://localhost:9999/echo");
config.DefaultHeaders = new WebHeaderCollection
{
{HttpRequestHeader.UserAgent, "Test"},
{HttpRequestHeader.UserAgent, "Custom User Agent"},
{"application-key", "foo-bar"}
};

var sockJs = (IClient)new Client.SockJS(config);

sockJs.Connected += async (sender, e) =>
Expand All @@ -46,11 +46,6 @@ public async Task Execute()
Console.WriteLine($"****************** Main: Message: {msg}");
if (msg != "test") return;
Console.WriteLine("****************** Main: Got back echo -> sending shutdown");
// sockJs.Send("shutdown");
// }
// else if (dataString == "ok")
// {
// Console.WriteLine($"****************** Main: Got back shutdown confirmation");
await sockJs.Disconnect();
}
catch (Exception ex)
Expand Down

0 comments on commit 25a9422

Please sign in to comment.