From 1c43288d088e7d51e570f2be27f2a02155c7d536 Mon Sep 17 00:00:00 2001 From: Steve Holmes Date: Wed, 10 Apr 2013 10:19:03 -0700 Subject: [PATCH] upgraded to vertx 1.3.1 --- index.js | 2 +- lib/vertxBus.js | 185 ---------- lib/vertxbus.js | 935 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 4 files changed, 937 insertions(+), 187 deletions(-) delete mode 100644 lib/vertxBus.js create mode 100644 lib/vertxbus.js diff --git a/index.js b/index.js index 0c1bc31..17fc2aa 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -module.exports = require('./lib/vertxBus.js'); +module.exports = require('./lib/vertxbus.js'); diff --git a/lib/vertxBus.js b/lib/vertxBus.js deleted file mode 100644 index 6fce7cf..0000000 --- a/lib/vertxBus.js +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright 2011-2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -SockJS = require("sockjs-client-ws"); - -var vertx = exports || {}; - -vertx.EventBus = function(url, options) { - - var that = this; - var sockJSConn = new SockJS.create(url, options); - sockJSConn.on("connection", function(){ - sockJSConn.onopen(); - }); - sockJSConn.on("data", function(message){ - sockJSConn.onmessage({data:message}); - }); - sockJSConn.on("close", function(){ - sockJSConn.onclose(); - }); - - var handlerMap = {}; - var replyHandlers = {}; - var state = vertx.EventBus.CONNECTING; - - that.onopen = null; - that.onclose = null; - - that.send = function(address, message, replyHandler) { - sendOrPub("send", address, message, replyHandler) - } - - that.publish = function(address, message, replyHandler) { - sendOrPub("publish", address, message, replyHandler) - } - - that.registerHandler = function(address, handler) { - checkSpecified("address", 'string', address); - checkSpecified("handler", 'function', handler); - checkOpen(); - var handlers = handlerMap[address]; - if (!handlers) { - handlers = [handler]; - handlerMap[address] = handlers; - // First handler for this address so we should register the connection - var msg = { type : "register", - address: address }; - sockJSConn.send(JSON.stringify(msg)); - } else { - handlers[handlers.length] = handler; - } - } - - that.unregisterHandler = function(address, handler) { - checkSpecified("address", 'string', address); - checkSpecified("handler", 'function', handler); - checkOpen(); - var handlers = handlerMap[address]; - if (handlers) { - var idx = handlers.indexOf(handler); - if (idx != -1) handlers.splice(idx, 1); - if (handlers.length == 0) { - // No more local handlers so we should unregister the connection - - var msg = { type : "unregister", - address: address}; - sockJSConn.send(JSON.stringify(msg)); - delete handlerMap[address]; - } - } - } - - that.close = function() { - checkOpen(); - state = vertx.EventBus.CLOSING; - sockJSConn.close(); - } - - that.readyState = function() { - return state; - } - - sockJSConn.onopen = function() { - state = vertx.EventBus.OPEN; - if (that.onopen) { - that.onopen(); - } - }; - - sockJSConn.onclose = function() { - state = vertx.EventBus.CLOSED; - if (that.onclose) { - that.onclose(); - } - }; - - sockJSConn.onmessage = function(e) { - var msg = e.data; - var json = JSON.parse(msg); - var body = json.body; - var replyAddress = json.replyAddress; - var address = json.address; - var replyHandler; - if (replyAddress) { - replyHandler = function(reply, replyHandler) { - // Send back reply - that.send(replyAddress, reply, replyHandler); - }; - } - var handlers = handlerMap[address]; - if (handlers) { - // We make a copy since the handler might get unregistered from within the - // handler itself, which would screw up our iteration - var copy = handlers.slice(0); - for (var i = 0; i < copy.length; i++) { - copy[i](body, replyHandler); - } - } else { - // Might be a reply message - var handler = replyHandlers[address]; - if (handler) { - delete replyHandlers[replyAddress]; - handler(body, replyHandler); - } - } - } - - function sendOrPub(sendOrPub, address, message, replyHandler) { - checkSpecified("address", 'string', address); - checkSpecified("message", 'object', message); - checkSpecified("replyHandler", 'function', replyHandler, true); - checkOpen(); - var envelope = { type : sendOrPub, - address: address, - body: message }; - if (replyHandler) { - var replyAddress = makeUUID(); - envelope.replyAddress = replyAddress; - replyHandlers[replyAddress] = replyHandler; - } - var str = JSON.stringify(envelope); - sockJSConn.send(str); - } - - function checkOpen() { - if (state != vertx.EventBus.OPEN) { - throw new Error('INVALID_STATE_ERR'); - } - } - - function checkSpecified(paramName, paramType, param, optional) { - if (!optional && !param) { - throw new Error("Parameter " + paramName + " must be specified"); - } - if (param && typeof param != paramType) { - throw new Error("Parameter " + paramName + " must be of type " + paramType); - } - } - - function isFunction(obj) { - return !!(obj && obj.constructor && obj.call && obj.apply); - } - - function makeUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" - .replace(/[xy]/g,function(a,b){return b=Math.random()*16,(a=="y"?b&3|8:b|0).toString(16)})} - -} - -vertx.EventBus.CONNECTING = 0; -vertx.EventBus.OPEN = 1; -vertx.EventBus.CLOSING = 2; -vertx.EventBus.CLOSED = 3; \ No newline at end of file diff --git a/lib/vertxbus.js b/lib/vertxbus.js new file mode 100644 index 0000000..3414438 --- /dev/null +++ b/lib/vertxbus.js @@ -0,0 +1,935 @@ + + + + + + + + + vert.x/src/dist/client/vertxbus.js at master · vert-x/vert.x + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+
+ + + + + +
+ + + + + +
+ + +
+
+ + + + + + + + + + + + + + +
+
+ +
+ + + + + + + + + +
+
+ + + + + + + + +
+
+ +
+
+
+ + + +
    + + +
  • +
    + +
    + + + + Watch + + + +
    +
    +
    + Notification status + +
    + +
    + +
    + +
    + +

    Not watching

    + You only receive notifications for discussions in which you participate or are @mentioned. + + + Watch + +
    +
    + +
    + +
    + +

    Watching

    + You receive notifications for all discussions in this repository. + + + Unwatch + +
    +
    + +
    + +
    + +

    Ignoring

    + You do not receive any notifications for discussions in this repository. + + + Stop ignoring + +
    +
    + +
    + +
    +
    +
    + +
    +
  • + +
  • + + + Unstar + + + + Star + + +
  • + +
  • + + + Fork + + +
  • + + +
+ +

+ public + + + / + vert.x +

+
+ + + + +
+ + + + + + +
+ + +
+ + + branch: + master + + +
+ +
+
+ Switch branches/tags + +
+ +
+
+ +
+
+ +
+
+ +
+ +
+ +
+ + amqp_bridge +
+
+ + api_refactor +
+ +
+ + gh-pages +
+
+ + ha +
+
+ + jmx_01 +
+
+ + jmx_04 +
+
+ + jmx_merge_01 +
+ +
+ + master +
+ +
+ + redeploy2 +
+ + +
+ +
Nothing to show
+
+ + +
+
+ +
+ + v1.3.1.final +
+
+ + v1.3.0.final +
+
+ + v1.2.3.final +
+
+ + v1.2.2.final +
+
+ + v1.2.1.final +
+
+ + v1.2.0.final +
+
+ + v1.1.0.final +
+
+ + v1.0.beta1 +
+
+ + v1.0.1.final +
+
+ + v1.0.0.final +
+ + +
+ + v1.0.0.beta9 +
+
+ + v1.0.0.beta8 +
+
+ + v1.0.0.beta7 +
+
+ + v1.0.0.beta6 +
+
+ + v1.0.0.beta5 +
+
+ + v1.0.0.beta4 +
+
+ + v1.0.0.beta3 +
+
+ + v0.2 +
+
+ + v0.1 +
+
+ +
Nothing to show
+ +
+ +
+
+
+ +
+ + + +
+ + + + + + + +
+
+ +
+ + + + + + + +
+
+ + + + + + + + + +
+ + + + + +
+

3 contributors

+ + + + + +
+ +
+ + +
+ +
+
+ +
+
+
+
+ + file + 205 lines (179 sloc) + 6.23 kb +
+
+
+ Edit + Raw + Blame + History +
+
+ +
+
+ + + + + +
+
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
+92
+93
+94
+95
+96
+97
+98
+99
+100
+101
+102
+103
+104
+105
+106
+107
+108
+109
+110
+111
+112
+113
+114
+115
+116
+117
+118
+119
+120
+121
+122
+123
+124
+125
+126
+127
+128
+129
+130
+131
+132
+133
+134
+135
+136
+137
+138
+139
+140
+141
+142
+143
+144
+145
+146
+147
+148
+149
+150
+151
+152
+153
+154
+155
+156
+157
+158
+159
+160
+161
+162
+163
+164
+165
+166
+167
+168
+169
+170
+171
+172
+173
+174
+175
+176
+177
+178
+179
+180
+181
+182
+183
+184
+185
+186
+187
+188
+189
+190
+191
+192
+193
+194
+195
+196
+197
+198
+199
+200
+201
+202
+203
+204
+
+
+
/*
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var vertx = vertx || {};

!function(factory) {
  if (typeof define === "function" && define.amd) {
    // Expose as an AMD module with SockJS dependency.
    // "vertxbus" and "sockjs" names are used because
    // AMD module names are derived from file names.
    define("vertxbus", ["sockjs"], factory);
  } else {
    // No AMD-compliant loader
    factory(SockJS);
  }
}(function(SockJS) {

  vertx.EventBus = function(url, options) {
  
    var that = this;
    var sockJSConn = new SockJS(url, undefined, options);
    var handlerMap = {};
    var replyHandlers = {};
    var state = vertx.EventBus.CONNECTING;
    var sessionID = null;
  
    that.onopen = null;
    that.onclose = null;

    that.login = function(username, password, replyHandler) {
      sendOrPub("send", 'vertx.basicauthmanager.login', {username: username, password: password}, function(reply) {
        if (reply.status === 'ok') {
          that.sessionID = reply.sessionID;
        }
        if (replyHandler) {
          delete reply.sessionID;
          replyHandler(reply)
        }
      });
    }
  
    that.send = function(address, message, replyHandler) {
      sendOrPub("send", address, message, replyHandler)
    }
  
    that.publish = function(address, message, replyHandler) {
      sendOrPub("publish", address, message, replyHandler)
    }
  
    that.registerHandler = function(address, handler) {
      checkSpecified("address", 'string', address);
      checkSpecified("handler", 'function', handler);
      checkOpen();
      var handlers = handlerMap[address];
      if (!handlers) {
        handlers = [handler];
        handlerMap[address] = handlers;
        // First handler for this address so we should register the connection
        var msg = { type : "register",
                    address: address };
        sockJSConn.send(JSON.stringify(msg));
      } else {
        handlers[handlers.length] = handler;
      }
    }
  
    that.unregisterHandler = function(address, handler) {
      checkSpecified("address", 'string', address);
      checkSpecified("handler", 'function', handler);
      checkOpen();
      var handlers = handlerMap[address];
      if (handlers) {
        var idx = handlers.indexOf(handler);
        if (idx != -1) handlers.splice(idx, 1);
        if (handlers.length == 0) {
          // No more local handlers so we should unregister the connection
  
          var msg = { type : "unregister",
                      address: address};
          sockJSConn.send(JSON.stringify(msg));
          delete handlerMap[address];
        }
      }
    }
  
    that.close = function() {
      checkOpen();
      state = vertx.EventBus.CLOSING;
      sockJSConn.close();
    }
  
    that.readyState = function() {
      return state;
    }
  
    sockJSConn.onopen = function() {
      state = vertx.EventBus.OPEN;
      if (that.onopen) {
        that.onopen();
      }
    };
  
    sockJSConn.onclose = function() {
      state = vertx.EventBus.CLOSED;
      if (that.onclose) {
        that.onclose();
      }
    };
  
    sockJSConn.onmessage = function(e) {
      var msg = e.data;
      var json = JSON.parse(msg);
      var body = json.body;
      var replyAddress = json.replyAddress;
      var address = json.address;
      var replyHandler;
      if (replyAddress) {
        replyHandler = function(reply, replyHandler) {
          // Send back reply
          that.send(replyAddress, reply, replyHandler);
        };
      }
      var handlers = handlerMap[address];
      if (handlers) {
        // We make a copy since the handler might get unregistered from within the
        // handler itself, which would screw up our iteration
        var copy = handlers.slice(0);
        for (var i = 0; i < copy.length; i++) {
          copy[i](body, replyHandler);
        }
      } else {
        // Might be a reply message
        var handler = replyHandlers[address];
        if (handler) {
          delete replyHandlers[replyAddress];
          handler(body, replyHandler);
        }
      }
    }
  
    function sendOrPub(sendOrPub, address, message, replyHandler) {
      checkSpecified("address", 'string', address);
      checkSpecified("replyHandler", 'function', replyHandler, true);
      checkOpen();
      var envelope = { type : sendOrPub,
                       address: address,
                       body: message };
      if (that.sessionID) {
        envelope.sessionID = that.sessionID;
      }
      if (replyHandler) {
        var replyAddress = makeUUID();
        envelope.replyAddress = replyAddress;
        replyHandlers[replyAddress] = replyHandler;
      }
      var str = JSON.stringify(envelope);
      sockJSConn.send(str);
    }
  
    function checkOpen() {
      if (state != vertx.EventBus.OPEN) {
        throw new Error('INVALID_STATE_ERR');
      }
    }
  
    function checkSpecified(paramName, paramType, param, optional) {
      if (!optional && !param) {
        throw new Error("Parameter " + paramName + " must be specified");
      }
      if (param && typeof param != paramType) {
        throw new Error("Parameter " + paramName + " must be of type " + paramType);
      }
    }
  
    function isFunction(obj) {
      return !!(obj && obj.constructor && obj.call && obj.apply);
    }
  
    function makeUUID(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
        .replace(/[xy]/g,function(a,b){return b=Math.random()*16,(a=="y"?b&3|8:b|0).toString(16)})}
  
  }
  
  vertx.EventBus.CONNECTING = 0;
  vertx.EventBus.OPEN = 1;
  vertx.EventBus.CLOSING = 2;
  vertx.EventBus.CLOSED = 3;

  return vertx.EventBus;

});
+
+
+ +
+
+ + + + +
+
+
+ + + + +
+
+
+
+ + +
+ + + + + +
+
+
+ +
+
+
+
+
+
+ +
+ + + +
+ + Something went wrong with that request. Please try again. + +
+ + + + + + + + diff --git a/package.json b/package.json index 6678a4a..1d0c8f3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "node-vertxbus", "author": "Surat Teerapittayanon", - "version": "0.1.0", + "version": "1.3.1", "keywords": ["websockets", "websocket", "sockjs"], "repository": { "type": "git",