Getting Started

The JNIOR Web-socket JavaScript Library depends on the INTEG comm.js file. The comm.js file also relies on 2 other JavaScript files. The md5.js file is required for calculating the authentication hash and the base64.js file is required for encoding and decoding file contents.

Your <head> section must include the following lines. The example shows putting the integ files in an integ folder.

<html>
    <head>
        ...

        <script src="integ/base64.js" type="text/javascript"></script>
        <script src="integ/md5.js" type="text/javascript"></script>
        <script src="integ/comm.js" type="text/javascript"></script>
        <script src="integ/jnrwebsocket.js" type="text/javascript"></script>

...

To use the JnrWebsocket we must instantiate an JnrWebsocket object and call connect().

    var jnrWebsocket = new JnrWebsocket();
    jnrWebsocket.connect();

By default the connect() call will build the ws:// URI with the hostname from the server where the code is being hosted. If you want to connect to other JNIORs and not just the one hosting the web app then you can pass in the IP Address of the target JNIOR.

    var jnrWebsocket = new JnrWebsocket();
    jnrWebsocket.connect(ipAddress);

Now What?

Okay? Great, I’m connected to the JNIOR WebSocket Server. Now what can I do?

The short answer: Probably Nothing.

Not what you wanted to hear but it’s only because most likely you need to log in. We find that out by adding an onLoggedIn listener.

There are two ways of doing this which are identical. You can call addEventListener() and supply the type of listener and the callback.

    jnrWebsocket.addEventListener('onLoggedIn', function (json) {
        alert("we're logged in!");
    });

Callback? What is a callback?

You just saw the use of the OnLoggedInListener callback.  So what is a callback?

A callback is, as Mozilla MDN Web Docs says, “A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.”

In simple terms, it is a piece of code that gets alerted of an event.  In the case above, the code was alerted when the login was successful.  There are other callback events such as when the login fails, when there was an update to a registry key, when I/O changes, when any message was received from the Web Server, and when there was a reply message from a Java application.

Above you saw the use of the addEventListener() where the type was supplied as well as the callback that will get called.  Note that this is an “add” method and not a “set”.  Multiple callbacks can be defined per event.  So the syntax of the addEventListener is addEventListener( type, callback ).

The addEventListener uses the type to call the correct add*Listener function.  Those are available to you as well.  Here are the available types and the associated add*Listener function that is called.

  • onLoggedIn calls addOnLoggedInListener(callback)
  • onIOChange calls addOnIOChangeListener(callback)
  • onMessage calls addOnMessageListener(listener)
  • onReplyMessage calls addOnReplyMessageListener(listener)

You’ll notice the absence of the registry listener that was mentioned above.  This is because you add the callback to get called specifically for the registry key that was requested.

On this page