-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
98 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters