Intro to using the C# JniorWebSocket Library

Intro

        /**
         * this is very simple example that uses anonymous inline event handlers.  anonymous inline event handlers 
         * are most likely not the correct arichitecture for your application.  they are only used in this example 
         * to group all of the useful event handlers in one place.
         */

        //
        // instantiate our JniorWebSocket object
        JniorWebSocket jniorWebSocket = new JniorWebSocket("10.0.0.63");

        //
        // handler for logging from our jnior websocket object
        jniorWebSocket.Log += delegate (object sender2, LogEventArgs args)
        {
            Console.WriteLine(args.Message);
        };

        //
        // handler that is called when the connection gets established.  the connection is established when 
        // the socket has been connected.  this does not mean that the connection is authenticated and ready 
        // to use
        jniorWebSocket.Connected += delegate (object sender2, EventArgs args)
        {
            Console.WriteLine("connected!");
        };

        //
        // handler that is called when the connection gets disconnected
        jniorWebSocket.Disconnected += delegate (object sender2, EventArgs args)
        {
            Console.WriteLine("disconnected!");
        };

        //
        // handler that is called when the connection experiences an error
        jniorWebSocket.Error += delegate (object sender2, ExceptionEventArgs args)
        {
            Console.WriteLine($"error: {args.Exception.Message}\n{args.Exception.StackTrace}");
        };

        //
        // handler that gets called when ANY packet is received.  check the Message property for the message type
        jniorWebSocket.MessageReceived += delegate (object sender2, MessageReceivedEventArgs args)
        {
            JObject json = JObject.Parse(args.Message);
            Console.WriteLine($"json: {json}");
        };

        //
        // handler that gets called when the default credentials fail.  this gives the writer of the application a 
        // chance to collect or provide the proper credentials
        jniorWebSocket.Unauthorized += delegate (object sender2, UnauthorizedEventArgs args)
        {
            Console.WriteLine("need proper credentials to log in!");

            //
            // provide the proper credentials.  the example JNIOR has a changed password.
            jniorWebSocket.Login("jnior", "jnior2");
        };

        //
        // handler called after the connection has successfully been authenticated
        jniorWebSocket.Authenticated += delegate (object sender2, EventArgs args)
        {
            Console.WriteLine("authenticated!");
        };

        //
        // the event handlers are set up.  now call to establish the connection
        jniorWebSocket.Connect();

        //
        // the jniorWebSocket object may be connected at this point but it is not fully ready to use.  any 
        // message sent that requires authentication will not be allowed until the login procedure has been 
        // completed.
        Console.WriteLine($"jniorWebSocket opened: {jniorWebSocket.IsOpened}");

        if (jniorWebSocket.IsOpened)
        {
            //
            // to wait for the login to be completed we can loop waiting for the IsAuthenticated flag to be TRUE
            while (!jniorWebSocket.IsAuthenticated)
            {
                Thread.Sleep(1000);
            }

            Console.WriteLine("jniorWebSocket is authenticated!");
        }
By | Updated On July 26, 2024 1:40 pm | No Comments | Categories: | Tags:


INTEG Process Group, inc. © 2023

Real-Time
Mon - Fri, 8am - 4pm EST
P: 724-933-9350
PureChat
Always Available
Contact Form
sales@integpg.com
support@integpg.com

@integpg
@jniordev
13490