Tag Archive: c-sharp

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!");
        }

The JniorWebSocket Library was started as an example project.  It has not had much use here in-house and therefore the testing has been limited.

Here is a quick application that demonstrates how to instantiate multiple JNIOR WebSocket objects.  Each Object is added to a tree control so that they may be interacted with individually.  So far I have added the ability to add and remove, connect and disconnect and pulse output 1.  This application can be easily extended to manage multiple JNIORs and perform many different tasks.

Here are 2 links.  The first is a link to the Visual Studio project for the library.  The second link is a zip file containing the example described above.

  C# WebSocket Example [ Oct 30 2018, 6.24 MB, MD5: fe1be1e450c2772a16d090a0d9c37f99 ]

  MultipleJniors-1.zip [ Oct 31 2018, 1.05 MB, MD5: 26485d581f00d9c5985db3e7f481e551 ]