sacOO7 / SocketclusterClientDotNet

C# client for socketcluster framework in node.js
http://socketcluster.io/#!/
Apache License 2.0
69 stars 16 forks source link
c-sharp client mono pub-sub silverlight socketcluster windows-phone xamarin

.Net Socketcluster Client

Overview

This client provides following functionality

Client supports following platforms

License

Apache License, Version 2.0

Usage via Nuget

    Install-Package ScClient.Official

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

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

    //This will send websocket handshake request to socketcluster-server
    socket.Connect();
    //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()
   socket.SetReconnectStrategy(null);

Emitting and listening to events

Event emitter

    socket.Emit(eventname,message);

    //socket.Emit("chat","Hi");

    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

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


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

    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

    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");
       }
    });
    List<Socket.Channel> channels = socket.GetChannels();
    var channel=socket.GetChannelByName("yell");
    //Returns null if channel of given name is not present

Publishing event on channel

       // 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


    //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 :).