Search code examples
cometdojocometd

dojo cometd is never ready


I am trying to setup a Bayeux server and client using Jetty, Dojo and maven.

My issue is that dojo seems to never be ready. The callback in require is never called.

This is the code for the HTML page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <title></title>
   <script data-dojo-config="parseOnLoad:true" src="dojo/dojo.js.uncompressed.js"></script>
   <script type="text/javascript">

      function initFormListener( onNewForm ) {

         require(["dojox/cometd", "dojo/io/script", "dojox/cometd/callbackPollTransport", "dojo/domReady!" ],
                     function ( cometd, io, callback ) {
                        console.log(" entered CometD function ");

                        // Function that manages the connection status with the Bayeux server
                        var _connected = false;

                        var _metaConnect = function ( message ) {
                           if ( cometd.isDisconnected() ) {
                              _connected = false;
                              console.log( "disconnected from server " + message );
                              return;
                           }

                           var wasConnected = _connected;
                           _connected = message.successful === true;
                           if ( !wasConnected && _connected ) {
                              console.log( "connected to server " );
                           }
                           else if ( wasConnected && !_connected ) {
                              console.log( "connection broken from server " + message );
                           }
                        }

                        // Function invoked when first contacting the server and
                        // when the server has lost the state of this client
                        var _metaHandshake = function ( handshake ) {
                           if ( handshake.successful === true ) {
                              cometd.batch( function () {
                                 cometd.subscribe( '/newFormData', function ( message ) {
                                    console.log( "new data for form " + message.formId + " in formData " + message.formDataId );
                                 } );
                              } );
                           }
                        }

                        // Disconnect when the page unloads
                        dojo.addOnUnload( function () {
                           cometd.disconnect( true );
                        } );

                        var cometURL = location.protocol + "//" + location.host + "/VisionWeb/cometd";
                        cometd.configure( {
                           url:cometURL,
                           logLevel:'debug'
                        } );

                        cometd.addListener( '/meta/handshake', _metaHandshake );
                        cometd.addListener( '/meta/connect', _metaConnect );

                        cometd.handshake();
                     } );
      }

      initFormListener( function() {console.log("cometd success")});
   </script>
</head>
<body>
   just some content
</body>
</html>

It's not like dojo does not work on the server. It does. My app is written in dojo 1.7.2

Is there a known issue that I don't know about or am I doing something wrong?

Thank you for any tip on how to find out why the callback is never called.


Solution

  • You are trying to use cometD.

    According to cometD's Reference Manual, you need to replace some js files of the standard Dojo toolkit, with the files provided in cometD's Primer download.

    Use cometD's documentation and you'll get your app up and running nicely and in a reasonable time.