Git Product home page Git Product logo

unitypackage-h.websockets's Introduction

Language License Requirements Requirements

High-performance event-based .NET Socket.IO library with a convenient interface aimed at writing the smallest possible code on the user side. The repository is a fork of Konstantin's H.Socket. IO, refactored to work as a Unity plugin.

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
  • Tested in Android, iOS, Linux and Windows 11

Unity 3D

To install this package in your Unity project, please follow the instructions below:

Usage

using System;
using System.Threading.Tasks;
using H.Socket.IO;
using UnityEngine;

#nullable enable

public class WebSocketsExample : MonoBehaviour
{
	private class ChatMessage
	{
		public string? Username { get; set; }
		public string? Message { get; set; }
		public long NumUsers { get; set; }
	}

	void Start()
	{
		Task.Run(async () =>
		{
			var client = new SocketIoClient();

			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<ChatMessage>("login", message =>
			{
				Debug.Log($"You are logged in. Total number of users: {message?.NumUsers}");
			});
			client.On<ChatMessage>("user joined", message =>
			{
				Debug.Log($"User joined: {message?.Username}. Total number of users: {message?.NumUsers}");
			});
			client.On<ChatMessage>("user left", message =>
			{
				Debug.Log($"User left: {message?.Username}. Total number of users: {message?.NumUsers}");
			});
			client.On<ChatMessage>("typing", message =>
			{
				Debug.Log($"User typing: {message?.Username}");
			});
			client.On<ChatMessage>("stop typing", message =>
			{
				Debug.Log($"User stop typing: {message?.Username}");
			});
			client.On<ChatMessage>("new message", message =>
			{
				Debug.Log($"New message from user \"{message?.Username}\": {message?.Message}");
			});

			await client.ConnectAsync(new Uri("wss://socketio-chat-h9jt.herokuapp.com/"));
			await client.Emit("add user", "C# Test User");
			await Task.Delay(TimeSpan.FromMilliseconds(200));
			await client.Emit("typing");
			await Task.Delay(TimeSpan.FromMilliseconds(200));
			await client.Emit("new message", "hello");
			await Task.Delay(TimeSpan.FromMilliseconds(200));
			await client.Emit("stop typing");
			await Task.Delay(TimeSpan.FromSeconds(2));
			await client.DisconnectAsync();

			client.Dispose();
		});
	}
}

Namespaces

// Will be sent with all messages(Unless otherwise stated).
// Also automatically connects to it.
client.DefaultNamespace = "my";

// or

// Connects to "my" namespace.
await  client.ConnectAsync(new  Uri(LocalCharServerUrl), namespaces: "my");

// Sends message to "my" namespace.
await  client.Emit("message", "hello", "my");

Custom arguments

await  client.ConnectAsync(new  Uri($"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

unitypackage-h.websockets's People

Contributors

simoesb avatar

Watchers

James Cloos avatar Bruno Simões avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.