Skip to content

Latest commit

History

380 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

LanguageLicenseRequirementsRequirementsBuild Status

High-performance event-based .NET Socket.IO library with a convenient interface,
aimed at writing the smallest possible code on the user side.

Features:

  • Supports the latest version of Socket.IO server
  • Supports namespaces
  • The library is null-free and does not contain NRE
  • Event-based
  • Completely asynchronous

Nuget

NuGetNuGetNuGet

Install-Package H.Socket.IO

Usage

usingSystem;usingSystem.Threading.Tasks;usingH.Socket.IO;
#nullable enable
publicclassChatMessage{publicstring?Username{get;set;}publicstring?Message{get;set;}publiclongNumUsers{get;set;}}publicasyncTaskConnectToChatNowShTest(){awaitusingvarclient=newSocketIoClient();client.Connected+=(sender,args)=>Console.WriteLine($"Connected: {args.Namespace}");client.Disconnected+=(sender,args)=>Console.WriteLine($"Disconnected. Reason: {args.Reason}, Status: {args.Status:G}");client.EventReceived+=(sender,args)=>Console.WriteLine($"EventReceived: Namespace: {args.Namespace}, Value: {args.Value}, IsHandled: {args.IsHandled}");client.HandledEventReceived+=(sender,args)=>Console.WriteLine($"HandledEventReceived: Namespace: {args.Namespace}, Value: {args.Value}");client.UnhandledEventReceived+=(sender,args)=>Console.WriteLine($"UnhandledEventReceived: Namespace: {args.Namespace}, Value: {args.Value}");client.ErrorReceived+=(sender,args)=>Console.WriteLine($"ErrorReceived: Namespace: {args.Namespace}, Value: {args.Value}");client.ExceptionOccurred+=(sender,args)=>Console.WriteLine($"ExceptionOccurred: {args.Value}");client.On("login",()=>{Console.WriteLine("You are logged in.");});client.On("login", json =>{Console.WriteLine($"You are logged in. Json: \"{json}\"");});client.On<ChatMessage>("login", message =>{Console.WriteLine($"You are logged in. Total number of users: {message.NumUsers}");});client.On<ChatMessage>("user joined", message =>{Console.WriteLine($"User joined: {message.Username}. Total number of users: {message.NumUsers}");});client.On<ChatMessage>("user left", message =>{Console.WriteLine($"User left: {message.Username}. Total number of users: {message.NumUsers}");});client.On<ChatMessage>("typing", message =>{Console.WriteLine($"User typing: {message.Username}");});client.On<ChatMessage>("stop typing", message =>{Console.WriteLine($"User stop typing: {message.Username}");});client.On<ChatMessage>("new message", message =>{Console.WriteLine($"New message from user \"{message.Username}\": {message.Message}");});awaitclient.ConnectAsync(newUri("wss://socketio-chat-h9jt.herokuapp.com/"));awaitclient.Emit("add user","C# H.Socket.IO Test User");awaitTask.Delay(TimeSpan.FromMilliseconds(200));awaitclient.Emit("typing");awaitTask.Delay(TimeSpan.FromMilliseconds(200));awaitclient.Emit("new message","hello");awaitTask.Delay(TimeSpan.FromMilliseconds(200));awaitclient.Emit("stop typing");awaitTask.Delay(TimeSpan.FromSeconds(2));awaitclient.DisconnectAsync();}

Namespaces

// Will be sent with all messages(Unless otherwise stated).// Also automatically connects to it.client.DefaultNamespace="my";// or// Connects to "my" namespace.awaitclient.ConnectAsync(newUri(LocalCharServerUrl),namespaces:"my");// Sends message to "my" namespace.awaitclient.Emit("message","hello","my");

Custom arguments

awaitclient.ConnectAsync(newUri($"wss://socketio-chat-h9jt.herokuapp.com/?access_token={mAccessToken}"));

Live Example

C# .NET Fiddle - https://dotnetfiddle.net/FWMpQ3
VB.NET .NET Fiddle - https://dotnetfiddle.net/WzIdnG
Http client of the tested Socket.IO server - https://socket-io-chat.now.sh/

Used documentation

Socket.IO Protocol - https://github.com/socketio/socket.io-protocol
Engine.IO Protocol - https://github.com/socketio/engine.io-protocol

Python implementation of Socket.IO - https://github.com/miguelgrinberg/python-socketio/blob/master/socketio/
Python implementation of Engine.IO - https://github.com/miguelgrinberg/python-engineio/blob/master/engineio/

Contacts

About

This is the Socket.IO client for .NET, which is based on ClientWebSocket, provide a simple way to connect to the Socket.IO server. The target framework is .NET Standard 2.0

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages