ScClient.Unity 0.1.1

dotnet add package ScClient.Unity --version 0.1.1                
NuGet\Install-Package ScClient.Unity -Version 0.1.1                
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="ScClient.Unity" Version="0.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ScClient.Unity --version 0.1.1                
#r "nuget: ScClient.Unity, 0.1.1"                
#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 ScClient.Unity as a Cake Addin
#addin nuget:?package=ScClient.Unity&version=0.1.1

// Install ScClient.Unity as a Cake Tool
#tool nuget:?package=ScClient.Unity&version=0.1.1                

socketcluster-client-unity

An extension to original C# client implementation.

Overview

This client provides following functionality

  • Support for emitting and listening to remote events
  • Automatic reconnection
  • Pub/sub
  • Authentication (JWT)

Client supports following platforms

  • Unity Desktop/Android/iOS
  • .Net 2.0
  • .Net 3.5
  • .Net 4.0
  • .Net standard 1.5 onwards
  • .Net Core 1.0 onwards

License

Apache License, Version 2.0

Usage via Nuget

    Install-Package ScClient.Unity

Nuget Gallery link : https://www.nuget.org/packages/ScClient.Unity/

Usage using source files

Library is built on top of Websocket4Net and Newtonsoft.Json. Install those packages via nuget and add source files into project

Description

Create instance of Socket class by passing url of socketcluster-server end-point

    //Create a socket instance
    string url = "ws://localhost:8000/socketcluster/";
    var socket = new Socket(url);

Important Note : Default url to socketcluster end-point is always ws://somedomainname.com/socketcluster/.

Registering basic listeners

Create a class implementing BasicListener interface and pass it's instance to socket setListener method


        internal class MyListener : IBasicListener
        {
            public void OnConnected(Socket socket)
            {
                Console.WriteLine("connected got called");
            }

            public void OnDisconnected(Socket socket)
            {
                Console.WriteLine("disconnected got called");
            }

            public void OnConnectError(Socket socket, ErrorEventArgs e)
            {
                Console.WriteLine("on connect error got called");
            }

            public void OnAuthentication(Socket socket, bool status)
            {
                Console.WriteLine(status ? "Socket is authenticated" : "Socket is not authenticated");
            }

            public void OnSetAuthToken(string token, Socket socket)
            {
                socket.setAuthToken(token);
                Console.WriteLine("on set auth token got called");
            }

        }

        internal class Program
        {
            public static void Main(string[] args)
            {
                var socket = new Socket("ws://localhost:8000/socketcluster/");
                socket.SetListerner(new MyListener());
            }
        }


Connecting to server
  • For connecting to server:
    //This will send websocket handshake request to socketcluster-server
    socket.Connect();
  • By default reconnection to server is not enabled , to enable it :
    //This will set automatic-reconnection to server with delay of 3 seconds and repeating it for 30 times
    socket.SetReconnectStrategy(new ReconnectStrategy().SetMaxAttempts(30));
    socket.Connect()
  • To disable reconnection :
   socket.SetReconnectStrategy(null);

Emitting and listening to events

Event emitter
  • eventname is name of event and message can be String, boolean, Long or JSON-object
    socket.Emit(eventname,message);

    //socket.Emit("chat","Hi");
  • To send event with acknowledgement

    socket.Emit("chat", "Hi", (eventName, error, data) =>
    {
       //If error and data is String
       Console.WriteLine("Got message for :"+eventName+" error is :"+error+" data is :"+data);
    });

Event Listener
  • For listening to events :

The object received can be String, Boolean, Long or JSONObject.


    socket.On("chat", (eventName, data) =>
    {
        Console.WriteLine("got message "+ data+ " from event "+eventName);
    });

  • To send acknowledgement back to server

    socket.On("chat", (eventName, data, ack) =>
    {
        Console.WriteLine("got message "+ data+ " from event "+eventName);
        ack(name, "No error", "Hi there buddy");
    });

Implementing Pub-Sub via channels

Creating channel
  • For creating and subscribing to channels:
    var channel=socket.CreateChannel(channelName);
   //var channel=socket.CreateChannel("yolo");


    /**
     * without acknowledgement
     */
     channel.Subscribe();

    /**
     * with acknowledgement
     */

    channel.Subscribe((channelName, error, data) =>
    {
       if (error == null)
       {
             Console.WriteLine("Subscribed to channel "+channelName+" successfully");
       }
    });
  • For getting list of created channels :
    List<Socket.Channel> channels = socket.GetChannels();
  • To get channel by name :
    var channel=socket.GetChannelByName("yell");
    //Returns null if channel of given name is not present

Publishing event on channel
  • For publishing event :
       // message can have any data type
    /**
     * without acknowledgement
     */
     channel.Publish(message);

    /**
     * with acknowledgement
     */
       channel.Publish(message, (channelName, error, data) =>
       {
            if (error == null) {
               Console.WriteLine("Published message to channel "+channelName+" successfully");
            }
       });

Listening to channel
  • For listening to channel event :

    //If instance of channel exists
    channel.OnMessage((channelName, data) =>
    {
         Console.WriteLine("Got message for channel "+channelName+" data is "+data);
    });

    //or
    socket.OnSubscribe(channelName, (channelName, data) =>
    {
        Console.WriteLine("Got message for channel "+channelName+" data is "+data);
    });

Un-subscribing to channel

    /**
     * without acknowledgement
     */

     channel.Unsubscribe();

    /**
     * with acknowledgement
     */

    channel.Unsubscribe((name, error, data) =>
    {
        if (error == null) {
            Console.WriteLine("channel unsubscribed successfully");
        }
    });

Handling SSL connection with server

To enable or disable SSL certficate verification use


socket.SetSSLCertVerification(true/false);

Setting HTTP proxy with server

//args, string : host , int : port
socket.SetProxy(host,port);

Star the repo. if you love the client 😃.
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 netcoreapp1.0 is compatible.  netcoreapp1.1 is compatible.  netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.5 is compatible.  netstandard1.6 is compatible.  netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net20 is compatible.  net35 is compatible.  net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 is compatible.  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 tizen30 was computed.  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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.1 617 3/10/2020
0.1.0 922 7/30/2018

Build target defined for dotnetcore, dotnet4.* and dotnetstandard