SocketIOSharp 1.0.2

There is a newer version of this package available.
See the version list below for details.

Requires NuGet 2.12 or higher.

dotnet add package SocketIOSharp --version 1.0.2                
NuGet\Install-Package SocketIOSharp -Version 1.0.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SocketIOSharp" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SocketIOSharp --version 1.0.2                
#r "nuget: SocketIOSharp, 1.0.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SocketIOSharp as a Cake Addin
#addin nuget:?package=SocketIOSharp&version=1.0.2

// Install SocketIOSharp as a Cake Tool
#tool nuget:?package=SocketIOSharp&version=1.0.2                

SocketIOSharp

SocketIOSharp is a Socket.IO protocol revision 4 client library based on Engine.IO and WebSocket protocol. It depends on WebSocketSharp to use WebSocket protocol. Engine.IO protocol is partly implemented by itself.

Installation

  • Nuget gallery

  • Command Install-Package SocketIOSharp in nuget package manager console.

Usage

Namespace

using SocketIOSharp.Client;

Constructor

SocketIOClient client = new SocketIOClient(SocketIOClient.Scheme.ws, host, port);

SocketIOClient.Scheme

public enum Scheme
{
  ws,
  wss
}

It is used to determine whether SocketIOClient instance will connect to server using WebSocket protocol or WebSocketSecure protocol.

Options in constructor

public SocketIOClient(SocketIOClient.Scheme Scheme, string Host, int Port, bool JsonOnly = false, bool AutoReconnect = false, bool UseAckTimeout = false)
  1. bool JsonOnly is used to determine whether SocketIOClient instance will or will not accept WebSocket binary packet.

  2. bool AutoReconnect is used to determine whether SocketIOClient instnace will or will not reconnect to server when WebSocket.OnClose is called.

  3. bool UseAckTimeout is used to determine whether SocketIOClient instnace will or will not automatically remove timed out ack actions.

  • All options above can be changed after construct like client.JsonOnly = false. Default value of these are false.

  • If JsonOnly is true, SocketIOClient instance will NOT handle WebSocket binary packet.

  • If AutoReconnect is true, SocketIOClient instance will reconnect after WebSocket.OnClose is called.

  • If UseAckTimeout is true, SocketIOClient instance will automatically remove timed out ack actions.

Connect

client.Connect();

Disconnect

client.Close();

or

client.Dispose();

Since SocketIOClient implements IDisposable interface, it will be automatically disconnect when SocketIOClient.Dispose is called.

Handlers

For convenient usage, it is implemented to can be used as Javascript style.

Event handlers

client.On(SocketIOClient.Event.CONNECTION, (JToken[] Data) => // Type of argument is JToken[].
{
  Console.WriteLine("Connected!");
});

client.On(SocketIOClient.Event.DISCONNECT, (Data) => // Argument can be used without type.
{
  Console.WriteLine("Disconnected!");
});

client.On(SocketIOClient.Event.ERROR, (Data) =>
{
  if (Data != null && Data.Length > 0 && Data[0] != null)
  {
    Console.WriteLine("Error : " + Data[0]);
  }
  else
  {
    Console.WrtieLine("Unkown Error");
  }
});

client.On("message", (Data) => 
{
  if (Data != null && Data.Length > 0 && Data[0] != null)
  {
    Console.WriteLine("Message : " + Data[0]);
  }
});

client.On("CustomEvent", CustomEventHandler); // Handler can be method.
client.On(9001, ItsOverNineThousands); // Type of event is JToken. So, it can be a number.

client.Off(9001, ItsOverNineThousands); // Remove 9001 event handler.

ACK handlers

client.On("ACK1", (JToken[] Data, (JToken[] Data) => { Console.WriteLine("ACK : " + Data) } =>
{
  // Type of first argument is JToken[].
  // Type of second arguemtn is Action<JToken[]>.
  
  Console.Write("On event ack1 : " + Data);
});

client.On("ACK2", (Data, AckAction) => // Second argument can be method.
{
  Console.Write("On event ack2 : " + Data);
});

client.On("ACK3", CustomAckHandler); // Handler can be method.
client.On(42, LifeTheUniverseAndTheEverything); // Type of event is JToken. So, it can be a number.

client.Off(42, LifeTheUniverseAndTheEverything); // Remove 42 ack handler.
SocketIOClient.Event
public static class Event
{
  public static readonly string CONNECTION = "connection";
  public static readonly string DISCONNECT = "disconnect";
  public static readonly string ERROR = "error";
}

These are the common basic Socket.IO events.

Emit

client.Emit("Event without data and ack");

client.Emit("Event only with data", "Hello world");

client.Emit("Event only with ack, action as lambda", (Data) => Console.WriteLine("ACK : " + Data));
client.Emit("Event only with ack, action as method", Console.WriteLine);

client.Emit("Event with data and ack, action as lambda", 9001, (Data) => Console.WriteLine("ACK : " + Data));
client.Emit("Event with data and ack, action as method", 42, Console.WriteLine);
// Type of data is JToken. So, it can be a number.

Implemented features

Planned features

  • Fully implemented Engine.IO client (It will implemented as independent project)
  • Transport without WebSocket

Limitations

  • Partly implemented Engine.IO : It implements part of Engine.IO only for processing Socket.IO packet. Therefore, it can NOT be used as Engine.IO client.
  • Only with WebSocket : Since it uses Websocket as transport, it can be used ONLY when WebSocket is avalible at Socket.IO server.
  • Code style : SocketIOSharp is written in 2016 by a college student. It can be looked as old or odd style.

Maintenance

Welcome to report issue or create pull request. I will check it happily.

Dependencies

License

SocketIOSharp is under The MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SocketIOSharp:

Package Downloads
My.DB.Logger

a small company that publish a csharp creeats and publish library

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on SocketIOSharp:

Repository Stars
MatterHackers/MatterControl
3D printing software for Windows, Mac and Linux
Version Downloads Last updated
2.0.3 29,174 11/23/2020
1.0.2 3,204 3/16/2020 1.0.2 is deprecated because it is no longer maintained.

Fix bug from critical misunderstanding of ping timeout.