This client provides following functionality
- Support for emitting and listening to remote events
- Automatic reconnection
- Pub/sub
- Authentication (JWT)
Client supports following platforms
- .Net 2.0
- .Net 3.5
- .Net 4.0
- .Net standard 1.3 onwards
- .Net Core 1.0 onwards
- Xamarin.Android
- Xamarin.iOS
- Unity
Apache License, Version 2.0
Install-Package ScClient.Official
Nuget Gallery link : https://www.nuget.org/packages/ScClient.Official/
Library is built on top of Websocket4Net and Newtonsoft.Json. Install those packages via nuget and add source files into project
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/.
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()); } }
- 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);
- 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); });
- 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"); });
- 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
- 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"); } });
- 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); });
/** * without acknowledgement */ channel.Unsubscribe(); /** * with acknowledgement */ channel.Unsubscribe((name, error, data) => { if (error == null) { Console.WriteLine("channel unsubscribed successfully"); } });
To enable or disable SSL certficate verification use
socket.SetSSLCertVerification(true/false);
//args, string : host , int : port socket.SetProxy(host,port);