|
@@ -1,7 +1,16 @@
|
|
|
-// Generated by CoffeeScript 1.4.0
|
|
|
+// Generated by CoffeeScript 1.7.1
|
|
|
+
|
|
|
+/*
|
|
|
+ Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0
|
|
|
+
|
|
|
+ Copyright (C) 2010-2013 [Jeff Mesnil](http://jmesnil.net/)
|
|
|
+ Copyright (C) 2012 [FuseSource, Inc.](http://fusesource.com)
|
|
|
+ */
|
|
|
+
|
|
|
(function() {
|
|
|
var Byte, Client, Frame, Stomp,
|
|
|
- __hasProp = {}.hasOwnProperty;
|
|
|
+ __hasProp = {}.hasOwnProperty,
|
|
|
+ __slice = [].slice;
|
|
|
|
|
|
Byte = {
|
|
|
LF: '\x0A',
|
|
@@ -9,6 +18,7 @@
|
|
|
};
|
|
|
|
|
|
Frame = (function() {
|
|
|
+ var unmarshallSingle;
|
|
|
|
|
|
function Frame(command, headers, body) {
|
|
|
this.command = command;
|
|
@@ -26,14 +36,22 @@
|
|
|
lines.push("" + name + ":" + value);
|
|
|
}
|
|
|
if (this.body) {
|
|
|
- lines.push("content-length:" + ('' + this.body).length);
|
|
|
+ lines.push("content-length:" + (Frame.sizeOfUTF8(this.body)));
|
|
|
}
|
|
|
lines.push(Byte.LF + this.body);
|
|
|
return lines.join(Byte.LF);
|
|
|
};
|
|
|
|
|
|
- Frame._unmarshallSingle = function(data) {
|
|
|
- var body, chr, command, divider, headerLines, headers, i, idx, len, line, start, trim, _i, _j, _ref, _ref1;
|
|
|
+ Frame.sizeOfUTF8 = function(s) {
|
|
|
+ if (s) {
|
|
|
+ return encodeURI(s).split(/%..|./).length - 1;
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ unmarshallSingle = function(data) {
|
|
|
+ var body, chr, command, divider, headerLines, headers, i, idx, len, line, start, trim, _i, _j, _len, _ref, _ref1;
|
|
|
divider = data.search(RegExp("" + Byte.LF + Byte.LF));
|
|
|
headerLines = data.substring(0, divider).split(Byte.LF);
|
|
|
command = headerLines.shift();
|
|
@@ -41,9 +59,9 @@
|
|
|
trim = function(str) {
|
|
|
return str.replace(/^\s+|\s+$/g, '');
|
|
|
};
|
|
|
- line = idx = null;
|
|
|
- for (i = _i = 0, _ref = headerLines.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
|
|
|
- line = headerLines[i];
|
|
|
+ _ref = headerLines.reverse();
|
|
|
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
+ line = _ref[_i];
|
|
|
idx = line.indexOf(':');
|
|
|
headers[trim(line.substring(0, idx))] = trim(line.substring(idx + 1));
|
|
|
}
|
|
@@ -74,7 +92,7 @@
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
data = _ref[_i];
|
|
|
if ((data != null ? data.length : void 0) > 0) {
|
|
|
- _results.push(Frame._unmarshallSingle(data));
|
|
|
+ _results.push(unmarshallSingle(data));
|
|
|
}
|
|
|
}
|
|
|
return _results;
|
|
@@ -92,6 +110,7 @@
|
|
|
})();
|
|
|
|
|
|
Client = (function() {
|
|
|
+ var now;
|
|
|
|
|
|
function Client(ws) {
|
|
|
this.ws = ws;
|
|
@@ -102,21 +121,40 @@
|
|
|
outgoing: 10000,
|
|
|
incoming: 10000
|
|
|
};
|
|
|
+ this.maxWebSocketFrameSize = 16 * 1024;
|
|
|
this.subscriptions = {};
|
|
|
}
|
|
|
|
|
|
+ Client.prototype.debug = function(message) {
|
|
|
+ var _ref;
|
|
|
+ return typeof window !== "undefined" && window !== null ? (_ref = window.console) != null ? _ref.log(message) : void 0 : void 0;
|
|
|
+ };
|
|
|
+
|
|
|
+ now = function() {
|
|
|
+ return Date.now || new Date().valueOf;
|
|
|
+ };
|
|
|
+
|
|
|
Client.prototype._transmit = function(command, headers, body) {
|
|
|
var out;
|
|
|
out = Frame.marshall(command, headers, body);
|
|
|
if (typeof this.debug === "function") {
|
|
|
this.debug(">>> " + out);
|
|
|
}
|
|
|
- return this.ws.send(out);
|
|
|
+ while (true) {
|
|
|
+ if (out.length > this.maxWebSocketFrameSize) {
|
|
|
+ this.ws.send(out.substring(0, this.maxWebSocketFrameSize));
|
|
|
+ out = out.substring(this.maxWebSocketFrameSize);
|
|
|
+ if (typeof this.debug === "function") {
|
|
|
+ this.debug("remaining = " + out.length);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return this.ws.send(out);
|
|
|
+ }
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
Client.prototype._setupHeartbeat = function(headers) {
|
|
|
- var serverIncoming, serverOutgoing, ttl, v, _ref, _ref1,
|
|
|
- _this = this;
|
|
|
+ var serverIncoming, serverOutgoing, ttl, v, _ref, _ref1;
|
|
|
if ((_ref = headers.version) !== Stomp.VERSIONS.V1_1 && _ref !== Stomp.VERSIONS.V1_2) {
|
|
|
return;
|
|
|
}
|
|
@@ -135,114 +173,157 @@
|
|
|
if (typeof this.debug === "function") {
|
|
|
this.debug("send PING every " + ttl + "ms");
|
|
|
}
|
|
|
- this.pinger = typeof window !== "undefined" && window !== null ? window.setInterval(function() {
|
|
|
- _this.ws.send(Byte.LF);
|
|
|
- return typeof _this.debug === "function" ? _this.debug(">>> PING") : void 0;
|
|
|
- }, ttl) : void 0;
|
|
|
+ this.pinger = Stomp.setInterval(ttl, (function(_this) {
|
|
|
+ return function() {
|
|
|
+ _this.ws.send(Byte.LF);
|
|
|
+ return typeof _this.debug === "function" ? _this.debug(">>> PING") : void 0;
|
|
|
+ };
|
|
|
+ })(this));
|
|
|
}
|
|
|
if (!(this.heartbeat.incoming === 0 || serverOutgoing === 0)) {
|
|
|
ttl = Math.max(this.heartbeat.incoming, serverOutgoing);
|
|
|
if (typeof this.debug === "function") {
|
|
|
this.debug("check PONG every " + ttl + "ms");
|
|
|
}
|
|
|
- return this.ponger = typeof window !== "undefined" && window !== null ? window.setInterval(function() {
|
|
|
- var delta;
|
|
|
- delta = Date.now() - _this.serverActivity;
|
|
|
- if (delta > ttl * 2) {
|
|
|
- if (typeof _this.debug === "function") {
|
|
|
- _this.debug("did not receive server activity for the last " + delta + "ms");
|
|
|
+ return this.ponger = Stomp.setInterval(ttl, (function(_this) {
|
|
|
+ return function() {
|
|
|
+ var delta;
|
|
|
+ delta = now() - _this.serverActivity;
|
|
|
+ if (delta > ttl * 2) {
|
|
|
+ if (typeof _this.debug === "function") {
|
|
|
+ _this.debug("did not receive server activity for the last " + delta + "ms");
|
|
|
+ }
|
|
|
+ return _this.ws.close();
|
|
|
}
|
|
|
- return _this.ws.close();
|
|
|
+ };
|
|
|
+ })(this));
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ Client.prototype._parseConnect = function() {
|
|
|
+ var args, connectCallback, errorCallback, headers;
|
|
|
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
|
|
+ headers = {};
|
|
|
+ switch (args.length) {
|
|
|
+ case 2:
|
|
|
+ headers = args[0], connectCallback = args[1];
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ if (args[1] instanceof Function) {
|
|
|
+ headers = args[0], connectCallback = args[1], errorCallback = args[2];
|
|
|
+ } else {
|
|
|
+ headers.login = args[0], headers.passcode = args[1], connectCallback = args[2];
|
|
|
}
|
|
|
- }, ttl) : void 0;
|
|
|
+ break;
|
|
|
+ case 4:
|
|
|
+ headers.login = args[0], headers.passcode = args[1], connectCallback = args[2], errorCallback = args[3];
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ headers.login = args[0], headers.passcode = args[1], connectCallback = args[2], errorCallback = args[3], headers.host = args[4];
|
|
|
}
|
|
|
+ return [headers, connectCallback, errorCallback];
|
|
|
};
|
|
|
|
|
|
- Client.prototype.connect = function(login, passcode, connectCallback, errorCallback, vhost) {
|
|
|
- var _this = this;
|
|
|
- this.connectCallback = connectCallback;
|
|
|
+ Client.prototype.connect = function() {
|
|
|
+ var args, errorCallback, headers, out;
|
|
|
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
|
|
|
+ out = this._parseConnect.apply(this, args);
|
|
|
+ headers = out[0], this.connectCallback = out[1], errorCallback = out[2];
|
|
|
if (typeof this.debug === "function") {
|
|
|
this.debug("Opening Web Socket...");
|
|
|
}
|
|
|
- this.ws.onmessage = function(evt) {
|
|
|
- var arr, c, data, frame, onreceive, _i, _len, _ref, _results;
|
|
|
- data = typeof ArrayBuffer !== 'undefined' && evt.data instanceof ArrayBuffer ? (arr = new Uint8Array(evt.data), typeof _this.debug === "function" ? _this.debug("--- got data length: " + arr.length) : void 0, ((function() {
|
|
|
- var _i, _len, _results;
|
|
|
+ this.ws.onmessage = (function(_this) {
|
|
|
+ return function(evt) {
|
|
|
+ var arr, c, client, data, frame, messageID, onreceive, subscription, _i, _len, _ref, _results;
|
|
|
+ data = typeof ArrayBuffer !== 'undefined' && evt.data instanceof ArrayBuffer ? (arr = new Uint8Array(evt.data), typeof _this.debug === "function" ? _this.debug("--- got data length: " + arr.length) : void 0, ((function() {
|
|
|
+ var _i, _len, _results;
|
|
|
+ _results = [];
|
|
|
+ for (_i = 0, _len = arr.length; _i < _len; _i++) {
|
|
|
+ c = arr[_i];
|
|
|
+ _results.push(String.fromCharCode(c));
|
|
|
+ }
|
|
|
+ return _results;
|
|
|
+ })()).join('')) : evt.data;
|
|
|
+ _this.serverActivity = now();
|
|
|
+ if (data === Byte.LF) {
|
|
|
+ if (typeof _this.debug === "function") {
|
|
|
+ _this.debug("<<< PONG");
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (typeof _this.debug === "function") {
|
|
|
+ _this.debug("<<< " + data);
|
|
|
+ }
|
|
|
+ _ref = Frame.unmarshall(data);
|
|
|
_results = [];
|
|
|
- for (_i = 0, _len = arr.length; _i < _len; _i++) {
|
|
|
- c = arr[_i];
|
|
|
- _results.push(String.fromCharCode(c));
|
|
|
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
+ frame = _ref[_i];
|
|
|
+ switch (frame.command) {
|
|
|
+ case "CONNECTED":
|
|
|
+ if (typeof _this.debug === "function") {
|
|
|
+ _this.debug("connected to server " + frame.headers.server);
|
|
|
+ }
|
|
|
+ _this.connected = true;
|
|
|
+ _this._setupHeartbeat(frame.headers);
|
|
|
+ _results.push(typeof _this.connectCallback === "function" ? _this.connectCallback(frame) : void 0);
|
|
|
+ break;
|
|
|
+ case "MESSAGE":
|
|
|
+ subscription = frame.headers.subscription;
|
|
|
+ onreceive = _this.subscriptions[subscription] || _this.onreceive;
|
|
|
+ if (onreceive) {
|
|
|
+ client = _this;
|
|
|
+ messageID = frame.headers["message-id"];
|
|
|
+ frame.ack = function(headers) {
|
|
|
+ if (headers == null) {
|
|
|
+ headers = {};
|
|
|
+ }
|
|
|
+ return client.ack(messageID, subscription, headers);
|
|
|
+ };
|
|
|
+ frame.nack = function(headers) {
|
|
|
+ if (headers == null) {
|
|
|
+ headers = {};
|
|
|
+ }
|
|
|
+ return client.nack(messageID, subscription, headers);
|
|
|
+ };
|
|
|
+ _results.push(onreceive(frame));
|
|
|
+ } else {
|
|
|
+ _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled received MESSAGE: " + frame) : void 0);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "RECEIPT":
|
|
|
+ _results.push(typeof _this.onreceipt === "function" ? _this.onreceipt(frame) : void 0);
|
|
|
+ break;
|
|
|
+ case "ERROR":
|
|
|
+ _results.push(typeof errorCallback === "function" ? errorCallback(frame) : void 0);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled frame: " + frame) : void 0);
|
|
|
+ }
|
|
|
}
|
|
|
return _results;
|
|
|
- })()).join('')) : evt.data;
|
|
|
- _this.serverActivity = Date.now();
|
|
|
- if (data === Byte.LF) {
|
|
|
+ };
|
|
|
+ })(this);
|
|
|
+ this.ws.onclose = (function(_this) {
|
|
|
+ return function() {
|
|
|
+ var msg;
|
|
|
+ msg = "Whoops! Lost connection to " + _this.ws.url;
|
|
|
if (typeof _this.debug === "function") {
|
|
|
- _this.debug("<<< PONG");
|
|
|
+ _this.debug(msg);
|
|
|
}
|
|
|
- return;
|
|
|
- }
|
|
|
- if (typeof _this.debug === "function") {
|
|
|
- _this.debug("<<< " + data);
|
|
|
- }
|
|
|
- _ref = Frame.unmarshall(data);
|
|
|
- _results = [];
|
|
|
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
- frame = _ref[_i];
|
|
|
- switch (frame.command) {
|
|
|
- case "CONNECTED":
|
|
|
- if (typeof _this.debug === "function") {
|
|
|
- _this.debug("connected to server " + frame.headers.server);
|
|
|
- }
|
|
|
- _this.connected = true;
|
|
|
- _this._setupHeartbeat(frame.headers);
|
|
|
- _results.push(typeof _this.connectCallback === "function" ? _this.connectCallback(frame) : void 0);
|
|
|
- break;
|
|
|
- case "MESSAGE":
|
|
|
- onreceive = _this.subscriptions[frame.headers.subscription];
|
|
|
- _results.push(typeof onreceive === "function" ? onreceive(frame) : void 0);
|
|
|
- break;
|
|
|
- case "RECEIPT":
|
|
|
- _results.push(typeof _this.onreceipt === "function" ? _this.onreceipt(frame) : void 0);
|
|
|
- break;
|
|
|
- case "ERROR":
|
|
|
- _results.push(typeof errorCallback === "function" ? errorCallback(frame) : void 0);
|
|
|
- break;
|
|
|
- default:
|
|
|
- _results.push(typeof _this.debug === "function" ? _this.debug("Unhandled frame: " + frame) : void 0);
|
|
|
+ _this._cleanUp();
|
|
|
+ return typeof errorCallback === "function" ? errorCallback(msg) : void 0;
|
|
|
+ };
|
|
|
+ })(this);
|
|
|
+ return this.ws.onopen = (function(_this) {
|
|
|
+ return function() {
|
|
|
+ if (typeof _this.debug === "function") {
|
|
|
+ _this.debug('Web Socket Opened...');
|
|
|
}
|
|
|
- }
|
|
|
- return _results;
|
|
|
- };
|
|
|
- this.ws.onclose = function() {
|
|
|
- var msg;
|
|
|
- msg = "Whoops! Lost connection to " + _this.ws.url;
|
|
|
- if (typeof _this.debug === "function") {
|
|
|
- _this.debug(msg);
|
|
|
- }
|
|
|
- _this._cleanUp();
|
|
|
- return typeof errorCallback === "function" ? errorCallback(msg) : void 0;
|
|
|
- };
|
|
|
- return this.ws.onopen = function() {
|
|
|
- var headers;
|
|
|
- if (typeof _this.debug === "function") {
|
|
|
- _this.debug('Web Socket Opened...');
|
|
|
- }
|
|
|
- headers = {
|
|
|
- "accept-version": Stomp.VERSIONS.supportedVersions(),
|
|
|
- "heart-beat": [_this.heartbeat.outgoing, _this.heartbeat.incoming].join(',')
|
|
|
+ headers["accept-version"] = Stomp.VERSIONS.supportedVersions();
|
|
|
+ headers["heart-beat"] = [_this.heartbeat.outgoing, _this.heartbeat.incoming].join(',');
|
|
|
+ return _this._transmit("CONNECT", headers);
|
|
|
};
|
|
|
- if (vhost) {
|
|
|
- headers.host = vhost;
|
|
|
- }
|
|
|
- if (login) {
|
|
|
- headers.login = login;
|
|
|
- }
|
|
|
- if (passcode) {
|
|
|
- headers.passcode = passcode;
|
|
|
- }
|
|
|
- return _this._transmit("CONNECT", headers);
|
|
|
- };
|
|
|
+ })(this);
|
|
|
};
|
|
|
|
|
|
Client.prototype.disconnect = function(disconnectCallback) {
|
|
@@ -256,12 +337,10 @@
|
|
|
Client.prototype._cleanUp = function() {
|
|
|
this.connected = false;
|
|
|
if (this.pinger) {
|
|
|
- if (typeof window !== "undefined" && window !== null) {
|
|
|
- window.clearInterval(this.pinger);
|
|
|
- }
|
|
|
+ Stomp.clearInterval(this.pinger);
|
|
|
}
|
|
|
if (this.ponger) {
|
|
|
- return typeof window !== "undefined" && window !== null ? window.clearInterval(this.ponger) : void 0;
|
|
|
+ return Stomp.clearInterval(this.ponger);
|
|
|
}
|
|
|
};
|
|
|
|
|
@@ -277,6 +356,7 @@
|
|
|
};
|
|
|
|
|
|
Client.prototype.subscribe = function(destination, callback, headers) {
|
|
|
+ var client;
|
|
|
if (headers == null) {
|
|
|
headers = {};
|
|
|
}
|
|
@@ -286,7 +366,13 @@
|
|
|
headers.destination = destination;
|
|
|
this.subscriptions[headers.id] = callback;
|
|
|
this._transmit("SUBSCRIBE", headers);
|
|
|
- return headers.id;
|
|
|
+ client = this;
|
|
|
+ return {
|
|
|
+ id: headers.id,
|
|
|
+ unsubscribe: function() {
|
|
|
+ return client.unsubscribe(headers.id);
|
|
|
+ }
|
|
|
+ };
|
|
|
};
|
|
|
|
|
|
Client.prototype.unsubscribe = function(id) {
|
|
@@ -297,9 +383,21 @@
|
|
|
};
|
|
|
|
|
|
Client.prototype.begin = function(transaction) {
|
|
|
- return this._transmit("BEGIN", {
|
|
|
- transaction: transaction
|
|
|
+ var client, txid;
|
|
|
+ txid = transaction || "tx-" + this.counter++;
|
|
|
+ this._transmit("BEGIN", {
|
|
|
+ transaction: txid
|
|
|
});
|
|
|
+ client = this;
|
|
|
+ return {
|
|
|
+ id: txid,
|
|
|
+ commit: function() {
|
|
|
+ return client.commit(txid);
|
|
|
+ },
|
|
|
+ abort: function() {
|
|
|
+ return client.abort(txid);
|
|
|
+ }
|
|
|
+ };
|
|
|
};
|
|
|
|
|
|
Client.prototype.commit = function(transaction) {
|
|
@@ -337,7 +435,6 @@
|
|
|
})();
|
|
|
|
|
|
Stomp = {
|
|
|
- libVersion: "2.0.0-next",
|
|
|
VERSIONS: {
|
|
|
V1_0: '1.0',
|
|
|
V1_1: '1.1',
|
|
@@ -362,10 +459,15 @@
|
|
|
};
|
|
|
|
|
|
if (typeof window !== "undefined" && window !== null) {
|
|
|
+ Stomp.setInterval = function(interval, f) {
|
|
|
+ return window.setInterval(f, interval);
|
|
|
+ };
|
|
|
+ Stomp.clearInterval = function(id) {
|
|
|
+ return window.clearInterval(id);
|
|
|
+ };
|
|
|
window.Stomp = Stomp;
|
|
|
} else if (typeof exports !== "undefined" && exports !== null) {
|
|
|
exports.Stomp = Stomp;
|
|
|
- Stomp.WebSocketClass = require('./test/server.mock.js').StompServerMock;
|
|
|
} else {
|
|
|
self.Stomp = Stomp;
|
|
|
}
|