https://docs.microsoft.com/en-us/dotnet/standard/net-standard Since my current project is built based on .Net Framework and they said that NET Framework doesn't support .NET Standard 2.1, I decided to tweak this project a little bit to make it worked in NET Framework 4.8
To distinguish with the existing project without dispose the original author,
I decided to name project namespace with syp.biz.SockJS.NET.Framework
While the original namespace was syp.biz.SockJS.NET
An asynchronous .NET implementation of the SockJS client
Includes all interfaces, extensions, enums and utils which are common to the client, user of the client and for extending the client.
The client library containing the actual SockJS client to be used by consuming applications.
A test application which consumes the client library.
A node.js SockJS server to be used in conjunction with the test application.
var sockJs = new SockJS("http://localhost:9999/echo");
sockJs.Connected += async (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) =>
{
// this event is triggered every time a message is received
try
{
Console.WriteLine($"Message: {msg}");
await sockJs.Disconnect(); // disconnect after first received message
}
catch (Exception ex)
{
Console.WriteLine($"Error: {e}");
}
};
sockJs.Disconnected += (sender, e) =>
{
// this event is triggered when the connection is disconnected (for any reason)
Console.WriteLine("Disconnected");
};
await sockJs.Connect(); // connect to the server
// create a default configuration file (default values)
var config = Configuration.Factory.BuildDefault("http://localhost:9999/echo");
var sockJs = new SockJs(config);
config.Logger = new ConsoleLogger();
config.DefaultHeaders = new WebHeaderCollection
{
{HttpRequestHeader.UserAgent, "Custom User Agent"},
{"application-key", "foo-bar"}
};
// 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;
Built-in WebSocket connection (websocket-system
) is implemented via System.Net.WebSockets.ClientWebSocket
and as such, is not supported on Windows 7, Windows Vista SP2, and Windows Server 2008. See Remarks section.
This library is based on the SockJS-client
JavaScript library (license).