index.html 2.67 KB
Newer Older
nextime's avatar
nextime committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
<!DOCTYPE html>
<html>
   <head>

      <!-- include AutobahnJS .. that's all you need -->
      <script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>

      <script>
         // WAMP session object
         var sess = null;

         window.onload = function() {

            var wsuri;
            if (window.location.protocol === "file:") {
               wsuri = "ws://localhost:9000";
            } else {
               wsuri = "ws://" + window.location.hostname + ":9000";
            }

            // connect to WAMP server
            ab.connect(wsuri,

               // WAMP session was established
               function (session) {

                  sess = session;

                  console.log("Connected to " + wsuri);
                  test();
               },

               // WAMP session is gone
               function (code, reason) {

                  sess = null;

                  if (code == ab.CONNECTION_UNSUPPORTED) {
                     window.location = "http://autobahn.ws/unsupportedbrowser";
                  } else {
                     console.log(reason);
                  }
               }
            );
         };

         function test() {

            // Call a remote procedure
            //
            sess.call("http://example.com/simple/calc#add", 23, 7).then(
               function (res) {
                  // RPC returned successfully
                  console.log("RPC result: " + res);
               },
               function (error) {
                  // An error occured:
                  //    error.uri      : The URI of the error
                  //    error.desc     :  Human readable, for developers and logging
                  //    error.details  :  Any error details (optional)
                  console.log("RPC error: " + error.desc);
               }
            );

            // Call a remote procedure, and a 2nd one with the result of the first
            //
            sess.call("http://example.com/simple/calc#sub", 15, 5).then(
               function (res) {
                  // call a remote procedure
                  sess.call("http://example.com/simple/calc#mul", res, res).then(
                     function (res) {
                        console.log("RPC result: " + res);
                     }
                  );
               }
            );
         };
      </script>
   </head>
   <body>
      <h1>RPCs with AutobahnJS - Example 1</h1>
      <noscript>
         <span style="color: #f00; font-weight: bold;">
            You need to turn on JavaScript.
         </span>
      </noscript>
      <p>
         Open development console (press F12) to watch.
      </p>
   </body>
 </html>