activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r1239163 [2/2] - in /activemq/activemq-apollo/trunk/apollo-distro/src/main/release: ./ examples/js-websocket/ examples/js-websocket/css/ examples/js-websocket/img/ examples/js-websocket/js/
Date Wed, 01 Feb 2012 15:08:43 GMT
Added: activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/index.html
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/index.html?rev=1239163&view=auto
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/index.html
(added)
+++ activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/index.html
Wed Feb  1 15:08:42 2012
@@ -0,0 +1,149 @@
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8">
+    <title>Chat Example Using STOMP Over WebSockets</title>
+    <!--[if lt IE 9]>
+      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
+    <![endif]-->
+    <link href="css/bootstrap.min.css" rel="stylesheet">
+    <link href="css/bootstrap.min.responsive.css" rel="stylesheet">
+    <style type="text/css">
+      body { padding-top: 40px; }
+    </style>
+  </head>
+
+  <body>
+    
+    <div class="navbar navbar-fixed-top">
+      <div class="navbar-inner">
+        <div class="container">
+          <a class="brand" href="#">Apollo WebSocket Chat Example</a>
+        </div>
+      </div>
+    </div>
+
+    <div class="container-fluid">
+      <div class="row-fluid">
+        <div class="span6">
+          <div id="connect">
+            <div class="page-header">
+              <h2>Server Login</h2>
+            </div>
+            <form class="form-horizontal" id='connect_form'>
+              <fieldset>
+                <div class="control-group">
+                  <label>WebSocket URL</label>
+                  <div class="controls">
+                    <input name=url id='connect_url' value='ws://localhost:61623' type="text">
+                  </div>
+                </div>
+                <div class="control-group">
+                  <label>User</label>
+                  <div class="controls">
+                    <input id='connect_login' placeholder="User Login" value="admin" type="text">
+                  </div>
+                </div>
+                <div class="control-group">
+                  <label>Password</label>
+                  <div class="controls">
+                    <input id='connect_passcode' placeholder="User Password" value="password"
type="password">
+                  </div>
+                </div>
+                <div class="control-group">
+                  <label>Destination</label>
+                  <div class="controls">
+                    <input id='destination' placeholder="Destination" value="/topic/chat.general"
type="text">
+                  </div>
+                </div>
+                <div class="form-actions">
+                  <button id='connect_submit' type="submit" class="btn btn-large btn-primary">Connect</button>
+                </div>
+              </fieldset>
+            </form>
+          </div>
+          <div id="connected" style="display:none">
+            <div class="page-header">
+              <h2>Chat Room</h2>
+            </div>
+            <div id="messages">
+            </div>
+            <form class="well form-search" id='send_form'>
+              <button class="btn" type="button" id='disconnect' style="float:right">Disconnect</button>
+              <input class="input-medium" id='send_form_input' placeholder="Type your
message here" class="span6"/>
+              <button class="btn" type="submit">Send</button>
+            </form>
+          </div>
+        </div>
+        <div class="span4">
+          <div class="page-header">
+            <h2>Debug Log</h2>
+          </div>
+          <pre id="debug"></pre>
+        </div>
+      </div>
+    </div>
+
+    <!-- Scripts placed at the end of the document so the pages load faster -->
+    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'></script>
+    <script src="js/stomp.js"></script>
+    <script>//<![CDATA[
+    $(document).ready(function() {
+      if(window.WebSocket) {
+        var client, destination;
+        $('#connect_form').submit(function() {
+          var url = $("#connect_url").val();
+          var login = $("#connect_login").val();
+          var passcode = $("#connect_passcode").val();
+          destination = $("#destination").val();
+
+          client = Stomp.client(url);
+
+          // this allows to display debug logs directly on the web page
+          client.debug = function(str) {
+            $("#debug").append(str + "\n");
+          };
+          
+          // the client is notified when it is connected to the server.
+          client.connect(login, passcode, function(frame) {
+            client.debug("connected to Stomp");
+            $('#connect').fadeOut({ duration: 'fast' });
+            $('#connected').fadeIn();
+            client.subscribe(destination, function(message) {
+              $("#messages").append("<p>" + message.body + "</p>\n");
+            });
+          });
+          return false;
+        });
+  
+        $('#disconnect').click(function() {
+          client.disconnect(function() {
+            $('#connected').fadeOut({ duration: 'fast' });
+            $('#connect').fadeIn();
+            $("#messages").html("")
+          });
+          return false;
+        });
+   
+        $('#send_form').submit(function() {
+          var text = $('#send_form_input').val();
+          if (text) {
+            client.send(destination, {}, text);
+            $('#send_form_input').val("");
+          }
+          return false;
+        });
+      } else {
+        $("#connect").html("\
+            <h1>Get a new Web Browser!</h1>\
+            <p>\
+            Your browser does not support WebSockets. This example will not work properly.<br>\
+            Please use a Web Browser with WebSockets support (WebKit or Google Chrome).\
+            </p>\
+        ");
+      }
+    });
+    //]]></script>
+
+  </body>
+</html>

Added: activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/js/stomp.js
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/js/stomp.js?rev=1239163&view=auto
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/js/stomp.js
(added)
+++ activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/js/stomp.js
Wed Feb  1 15:08:42 2012
@@ -0,0 +1,205 @@
+
+/*
+Copyright (C) 2010 Jeff Mesnil -- http://jmesnil.net/
+Copyright (C) 2012 FuseSource, Inc. -- http://fusesource.com
+*/
+
+(function() {
+  var Client, Stomp, WebSocketStompMock,
+    __hasProp = Object.prototype.hasOwnProperty;
+
+  Stomp = {
+    frame: function(command, headers, body) {
+      if (headers == null) headers = [];
+      if (body == null) body = '';
+      return {
+        command: command,
+        headers: headers,
+        body: body,
+        id: headers.id,
+        receipt: headers.receipt,
+        transaction: headers.transaction,
+        destination: headers.destination,
+        subscription: headers.subscription,
+        error: null,
+        toString: function() {
+          var lines, name, value;
+          lines = [command];
+          for (name in headers) {
+            if (!__hasProp.call(headers, name)) continue;
+            value = headers[name];
+            lines.push("" + name + ":" + value);
+          }
+          lines.push('\n' + body);
+          return lines.join('\n');
+        }
+      };
+    },
+    unmarshal: function(data) {
+      var body, chr, command, divider, headerLines, headers, i, idx, line, trim, _ref, _ref2,
_ref3;
+      divider = data.search(/\n\n/);
+      headerLines = data.substring(0, divider).split('\n');
+      command = headerLines.shift();
+      headers = {};
+      body = '';
+      trim = function(str) {
+        return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
+      };
+      line = idx = null;
+      for (i = 0, _ref = headerLines.length; 0 <= _ref ? i < _ref : i > _ref; 0
<= _ref ? i++ : i--) {
+        line = headerLines[i];
+        idx = line.indexOf(':');
+        headers[trim(line.substring(0, idx))] = trim(line.substring(idx + 1));
+      }
+      chr = null;
+      for (i = _ref2 = divider + 2, _ref3 = data.length; _ref2 <= _ref3 ? i < _ref3
: i > _ref3; _ref2 <= _ref3 ? i++ : i--) {
+        chr = data.charAt(i);
+        if (chr === '\0') break;
+        body += chr;
+      }
+      return Stomp.frame(command, headers, body);
+    },
+    marshal: function(command, headers, body) {
+      return Stomp.frame(command, headers, body).toString() + '\0';
+    },
+    client: function(url) {
+      return new Client(url);
+    }
+  };
+
+  Client = (function() {
+
+    function Client(url) {
+      this.url = url;
+      this.counter = 0;
+      this.connected = false;
+      this.subscriptions = {};
+    }
+
+    Client.prototype._transmit = function(command, headers, body) {
+      var out;
+      out = Stomp.marshal(command, headers, body);
+      if (typeof this.debug === "function") this.debug(">>> " + out);
+      return this.ws.send(out);
+    };
+
+    Client.prototype.connect = function(login_, passcode_, connectCallback, errorCallback)
{
+      var klass,
+        _this = this;
+      if (typeof this.debug === "function") this.debug("Opening Web Socket...");
+      klass = WebSocketStompMock || WebSocket;
+      this.ws = new klass(this.url);
+      this.ws.binaryType = "arraybuffer";
+      this.ws.onmessage = function(evt) {
+        var data, frame, i, onreceive, view;
+        data = (function() {
+          var _i, _len;
+          if (evt.data instanceof ArrayBuffer) {
+            view = new Uint8Array(evt.data);
+            if (typeof this.debug === "function") {
+              this.debug('--- got data length: ' + view.length);
+            }
+            data = "";
+            for (_i = 0, _len = view.length; _i < _len; _i++) {
+              i = view[_i];
+              data += String.fromCharCode(i);
+            }
+            return data;
+          } else {
+            return evt.data;
+          }
+        }).call(_this);
+        if (typeof _this.debug === "function") _this.debug('<<< ' + data);
+        frame = Stomp.unmarshal(data);
+        if (frame.command === "CONNECTED" && connectCallback) {
+          _this.connected = true;
+          return connectCallback(frame);
+        } else if (frame.command === "MESSAGE") {
+          onreceive = _this.subscriptions[frame.headers.subscription];
+          return typeof onreceive === "function" ? onreceive(frame) : void 0;
+        }
+      };
+      this.ws.onclose = function() {
+        var msg;
+        msg = "Whoops! Lost connection to " + _this.url;
+        if (typeof _this.debug === "function") _this.debug(msg);
+        return typeof errorCallback === "function" ? errorCallback(msg) : void 0;
+      };
+      this.ws.onopen = function() {
+        if (typeof _this.debug === "function") _this.debug('Web Socket Opened...');
+        return _this._transmit("CONNECT", {
+          login: login_,
+          passcode: passcode_
+        });
+      };
+      return this.connectCallback = connectCallback;
+    };
+
+    Client.prototype.disconnect = function(disconnectCallback) {
+      this._transmit("DISCONNECT");
+      this.ws.close();
+      this.connected = false;
+      return typeof disconnectCallback === "function" ? disconnectCallback() : void 0;
+    };
+
+    Client.prototype.send = function(destination, headers, body) {
+      if (headers == null) headers = {};
+      if (body == null) body = '';
+      headers.destination = destination;
+      return this._transmit("SEND", headers, body);
+    };
+
+    Client.prototype.subscribe = function(destination, callback, headers) {
+      var id;
+      if (headers == null) headers = {};
+      id = "sub-" + this.counter++;
+      headers.destination = destination;
+      headers.id = id;
+      this.subscriptions[id] = callback;
+      this._transmit("SUBSCRIBE", headers);
+      return id;
+    };
+
+    Client.prototype.unsubscribe = function(id, headers) {
+      if (headers == null) headers = {};
+      headers.id = id;
+      delete this.subscriptions[id];
+      return this._transmit("UNSUBSCRIBE", headers);
+    };
+
+    Client.prototype.begin = function(transaction, headers) {
+      if (headers == null) headers = {};
+      headers.transaction = transaction;
+      return this._transmit("BEGIN", headers);
+    };
+
+    Client.prototype.commit = function(transaction, headers) {
+      if (headers == null) headers = {};
+      headers.transaction = transaction;
+      return this._transmit("COMMIT", headers);
+    };
+
+    Client.prototype.abort = function(transaction, headers) {
+      if (headers == null) headers = {};
+      headers.transaction = transaction;
+      return this._transmit("ABORT", headers);
+    };
+
+    Client.prototype.ack = function(message_id, headers) {
+      if (headers == null) headers = {};
+      headers["message-id"] = message_id;
+      return this._transmit("ACK", headers);
+    };
+
+    return Client;
+
+  })();
+
+  if (typeof window !== "undefined" && window !== null) {
+    window.Stomp = Stomp;
+  } else {
+    exports.Stomp = Stomp;
+    WebSocketStompMock = require('./test/server.mock.js').StompServerMock;
+  }
+
+}).call(this);

Added: activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/readme.md
URL: http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/readme.md?rev=1239163&view=auto
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/readme.md
(added)
+++ activemq/activemq-apollo/trunk/apollo-distro/src/main/release/examples/js-websocket/readme.md
Wed Feb  1 15:08:42 2012
@@ -0,0 +1,18 @@
+## Overview
+
+This is an example of how use the 
+['stomp-websocket'](https://github.com/jmesnil/stomp-websocket) 
+JavaScript APIs with the Apollo message broker.
+
+## Prereqs
+
+- Use a modern browser with WebSocket support like Chrome or WebKit
+
+## Running
+
+Run the Apollo sever locally and then open the `index.html`
+in the current directory using a web browser.
+
+## Credits
+
+This example is a modified version of Jeff Mesnil's chat example.
\ No newline at end of file



Mime
View raw message