Skip to content
Snippets Groups Projects
Commit dd501a7e authored by Constantin Jucovschi's avatar Constantin Jucovschi
Browse files

updated to new version of stomp library. Removed Heartbeat functionality.

parent f39bd3d7
No related branches found
No related tags found
No related merge requests found
// Generated by CoffeeScript 1.3.3 // 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() { (function() {
var Byte, Client, Frame, Stomp, var Byte, Client, Frame, Stomp,
__hasProp = {}.hasOwnProperty; __hasProp = {}.hasOwnProperty,
__slice = [].slice;
Byte = { Byte = {
LF: '\x0A', LF: '\x0A',
...@@ -9,6 +18,7 @@ ...@@ -9,6 +18,7 @@
}; };
Frame = (function() { Frame = (function() {
var unmarshallSingle;
function Frame(command, headers, body) { function Frame(command, headers, body) {
this.command = command; this.command = command;
...@@ -17,23 +27,35 @@ ...@@ -17,23 +27,35 @@
} }
Frame.prototype.toString = function() { Frame.prototype.toString = function() {
var lines, name, value, _ref; var lines, name, skipContentLength, value, _ref;
lines = [this.command]; lines = [this.command];
skipContentLength = this.headers['content-length'] === false ? true : false;
if (skipContentLength) {
delete this.headers['content-length'];
}
_ref = this.headers; _ref = this.headers;
for (name in _ref) { for (name in _ref) {
if (!__hasProp.call(_ref, name)) continue; if (!__hasProp.call(_ref, name)) continue;
value = _ref[name]; value = _ref[name];
lines.push("" + name + ":" + value); lines.push("" + name + ":" + value);
} }
if (this.body) { if (this.body && !skipContentLength) {
lines.push("content-length:" + ('' + this.body).length); lines.push("content-length:" + (Frame.sizeOfUTF8(this.body)));
} }
lines.push(Byte.LF + this.body); lines.push(Byte.LF + this.body);
return lines.join(Byte.LF); return lines.join(Byte.LF);
}; };
Frame._unmarshallSingle = function(data) { Frame.sizeOfUTF8 = function(s) {
var body, chr, command, divider, headerLines, headers, i, idx, len, line, start, trim, _i, _j, _ref, _ref1; if (s) {
return encodeURI(s).match(/%..|./g).length;
} 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)); divider = data.search(RegExp("" + Byte.LF + Byte.LF));
headerLines = data.substring(0, divider).split(Byte.LF); headerLines = data.substring(0, divider).split(Byte.LF);
command = headerLines.shift(); command = headerLines.shift();
...@@ -41,9 +63,9 @@ ...@@ -41,9 +63,9 @@
trim = function(str) { trim = function(str) {
return str.replace(/^\s+|\s+$/g, ''); return str.replace(/^\s+|\s+$/g, '');
}; };
line = idx = null; _ref = headerLines.reverse();
for (i = _i = 0, _ref = headerLines.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
line = headerLines[i]; line = _ref[_i];
idx = line.indexOf(':'); idx = line.indexOf(':');
headers[trim(line.substring(0, idx))] = trim(line.substring(idx + 1)); headers[trim(line.substring(0, idx))] = trim(line.substring(idx + 1));
} }
...@@ -66,19 +88,29 @@ ...@@ -66,19 +88,29 @@
}; };
Frame.unmarshall = function(datas) { Frame.unmarshall = function(datas) {
var data; var frame, frames, last_frame, r;
return (function() { frames = datas.split(RegExp("" + Byte.NULL + Byte.LF + "*"));
r = {
frames: [],
partial: ''
};
r.frames = (function() {
var _i, _len, _ref, _results; var _i, _len, _ref, _results;
_ref = datas.split(RegExp("" + Byte.NULL + Byte.LF + "*")); _ref = frames.slice(0, -1);
_results = []; _results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
data = _ref[_i]; frame = _ref[_i];
if ((data != null ? data.length : void 0) > 0) { _results.push(unmarshallSingle(frame));
_results.push(Frame._unmarshallSingle(data));
}
} }
return _results; return _results;
})(); })();
last_frame = frames.slice(-1)[0];
if (last_frame === Byte.LF || (last_frame.search(RegExp("" + Byte.NULL + Byte.LF + "*$"))) !== -1) {
r.frames.push(unmarshallSingle(last_frame));
} else {
r.partial = last_frame;
}
return r;
}; };
Frame.marshall = function(command, headers, body) { Frame.marshall = function(command, headers, body) {
...@@ -92,6 +124,7 @@ ...@@ -92,6 +124,7 @@
})(); })();
Client = (function() { Client = (function() {
var now;
function Client(ws) { function Client(ws) {
this.ws = ws; this.ws = ws;
...@@ -102,8 +135,23 @@ ...@@ -102,8 +135,23 @@
outgoing: 10000, outgoing: 10000,
incoming: 10000 incoming: 10000
}; };
this.maxWebSocketFrameSize = 16 * 1024;
this.subscriptions = {}; this.subscriptions = {};
this.partialData = '';
}
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() {
if (Date.now) {
return Date.now();
} else {
return new Date().valueOf;
} }
};
Client.prototype._transmit = function(command, headers, body) { Client.prototype._transmit = function(command, headers, body) {
var out; var out;
...@@ -111,61 +159,54 @@ ...@@ -111,61 +159,54 @@
if (typeof this.debug === "function") { if (typeof this.debug === "function") {
this.debug(">>> " + out); this.debug(">>> " + out);
} }
return this.ws.send(out); while (true) {
}; if (out.length > this.maxWebSocketFrameSize) {
this.ws.send(out.substring(0, this.maxWebSocketFrameSize));
Client.prototype._setupHeartbeat = function(headers) { out = out.substring(this.maxWebSocketFrameSize);
var serverIncoming, serverOutgoing, ttl, v, _ref, _ref1,
_this = this;
if ((_ref = headers.version) !== Stomp.VERSIONS.V1_1 && _ref !== Stomp.VERSIONS.V1_2) {
return;
}
_ref1 = (function() {
var _i, _len, _ref1, _results;
_ref1 = headers['heart-beat'].split(",");
_results = [];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
v = _ref1[_i];
_results.push(parseInt(v));
}
return _results;
})(), serverOutgoing = _ref1[0], serverIncoming = _ref1[1];
if (!(this.heartbeat.outgoing === 0 || serverIncoming === 0)) {
ttl = Math.max(this.heartbeat.outgoing, serverIncoming);
if (typeof this.debug === "function") { if (typeof this.debug === "function") {
this.debug("send PING every " + ttl + "ms"); this.debug("remaining = " + out.length);
}
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;
} }
if (!(this.heartbeat.incoming === 0 || serverOutgoing === 0)) { } else {
ttl = Math.max(this.heartbeat.incoming, serverOutgoing); return this.ws.send(out);
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._cleanUp(); };
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) { Client.prototype.connect = function() {
var _this = this; var args, errorCallback, headers, out;
this.connectCallback = connectCallback; 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") { if (typeof this.debug === "function") {
this.debug("Opening Web Socket..."); this.debug("Opening Web Socket...");
} }
this.ws.onmessage = function(evt) { this.ws.onmessage = (function(_this) {
var arr, c, data, frame, onreceive, _i, _len, _ref, _results; return function(evt) {
var arr, c, client, data, frame, messageID, onreceive, subscription, unmarshalledData, _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() { 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; var _i, _len, _results;
_results = []; _results = [];
...@@ -175,7 +216,7 @@ ...@@ -175,7 +216,7 @@
} }
return _results; return _results;
})()).join('')) : evt.data; })()).join('')) : evt.data;
_this.serverActivity = Date.now(); _this.serverActivity = now();
if (data === Byte.LF) { if (data === Byte.LF) {
if (typeof _this.debug === "function") { if (typeof _this.debug === "function") {
_this.debug("<<< PONG"); _this.debug("<<< PONG");
...@@ -185,7 +226,9 @@ ...@@ -185,7 +226,9 @@
if (typeof _this.debug === "function") { if (typeof _this.debug === "function") {
_this.debug("<<< " + data); _this.debug("<<< " + data);
} }
_ref = Frame.unmarshall(data); unmarshalledData = Frame.unmarshall(_this.partialData + data);
_this.partialData = unmarshalledData.partial;
_ref = unmarshalledData.frames;
_results = []; _results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
frame = _ref[_i]; frame = _ref[_i];
...@@ -195,12 +238,30 @@ ...@@ -195,12 +238,30 @@
_this.debug("connected to server " + frame.headers.server); _this.debug("connected to server " + frame.headers.server);
} }
_this.connected = true; _this.connected = true;
_this._setupHeartbeat(frame.headers);
_results.push(typeof _this.connectCallback === "function" ? _this.connectCallback(frame) : void 0); _results.push(typeof _this.connectCallback === "function" ? _this.connectCallback(frame) : void 0);
break; break;
case "MESSAGE": case "MESSAGE":
onreceive = _this.subscriptions[frame.headers.subscription]; subscription = frame.headers.subscription;
_results.push(typeof onreceive === "function" ? onreceive(frame) : void 0); 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; break;
case "RECEIPT": case "RECEIPT":
_results.push(typeof _this.onreceipt === "function" ? _this.onreceipt(frame) : void 0); _results.push(typeof _this.onreceipt === "function" ? _this.onreceipt(frame) : void 0);
...@@ -214,53 +275,47 @@ ...@@ -214,53 +275,47 @@
} }
return _results; return _results;
}; };
this.ws.onclose = function() { })(this);
this.ws.onclose = (function(_this) {
return function() {
var msg; var msg;
msg = "Whoops! Lost connection to " + _this.ws.url; msg = "Whoops! Lost connection to " + _this.ws.url;
if (typeof _this.debug === "function") { if (typeof _this.debug === "function") {
_this.debug(msg); _this.debug(msg);
} }
_this._cleanUp();
return typeof errorCallback === "function" ? errorCallback(msg) : void 0; return typeof errorCallback === "function" ? errorCallback(msg) : void 0;
}; };
return this.ws.onopen = function() { })(this);
var headers; return this.ws.onopen = (function(_this) {
return function() {
if (typeof _this.debug === "function") { if (typeof _this.debug === "function") {
_this.debug('Web Socket Opened...'); _this.debug('Web Socket Opened...');
} }
headers = { headers["accept-version"] = Stomp.VERSIONS.supportedVersions();
"accept-version": Stomp.VERSIONS.supportedVersions(),
"heart-beat": [_this.heartbeat.outgoing, _this.heartbeat.incoming].join(',')
};
if (vhost) {
headers.host = vhost;
}
if (login) {
headers.login = login;
}
if (passcode) {
headers.passcode = passcode;
}
return _this._transmit("CONNECT", headers); return _this._transmit("CONNECT", headers);
}; };
})(this);
}; };
Client.prototype.disconnect = function(disconnectCallback) { Client.prototype.disconnect = function(disconnectCallback, headers) {
this._transmit("DISCONNECT"); if (headers == null) {
headers = {};
}
this._transmit("DISCONNECT", headers);
this.ws.onclose = null; this.ws.onclose = null;
this.ws.close();
this._cleanUp(); this._cleanUp();
return typeof disconnectCallback === "function" ? disconnectCallback() : void 0; return typeof disconnectCallback === "function" ? disconnectCallback() : void 0;
}; };
Client.prototype._cleanUp = function() { Client.prototype._cleanUp = function() {
this.ws.close();
this.connected = false; this.connected = false;
if (this.pinger) { if (this.pinger) {
if (typeof window !== "undefined" && window !== null) { Stomp.clearInterval(this.pinger);
window.clearInterval(this.pinger);
}
} }
if (this.ponger) { if (this.ponger) {
return typeof window !== "undefined" && window !== null ? window.clearInterval(this.ponger) : void 0; return Stomp.clearInterval(this.ponger);
} }
}; };
...@@ -276,6 +331,7 @@ ...@@ -276,6 +331,7 @@
}; };
Client.prototype.subscribe = function(destination, callback, headers) { Client.prototype.subscribe = function(destination, callback, headers) {
var client;
if (headers == null) { if (headers == null) {
headers = {}; headers = {};
} }
...@@ -285,7 +341,13 @@ ...@@ -285,7 +341,13 @@
headers.destination = destination; headers.destination = destination;
this.subscriptions[headers.id] = callback; this.subscriptions[headers.id] = callback;
this._transmit("SUBSCRIBE", headers); 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) { Client.prototype.unsubscribe = function(id) {
...@@ -296,9 +358,21 @@ ...@@ -296,9 +358,21 @@
}; };
Client.prototype.begin = function(transaction) { Client.prototype.begin = function(transaction) {
return this._transmit("BEGIN", { var client, txid;
transaction: transaction 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) { Client.prototype.commit = function(transaction) {
...@@ -336,7 +410,6 @@ ...@@ -336,7 +410,6 @@
})(); })();
Stomp = { Stomp = {
libVersion: "2.0.0-next",
VERSIONS: { VERSIONS: {
V1_0: '1.0', V1_0: '1.0',
V1_1: '1.1', V1_1: '1.1',
...@@ -360,11 +433,20 @@ ...@@ -360,11 +433,20 @@
Frame: Frame Frame: Frame
}; };
if (typeof exports !== "undefined" && exports !== null) {
exports.Stomp = Stomp;
}
if (typeof window !== "undefined" && window !== null) { 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; window.Stomp = Stomp;
} else { } else if (!exports) {
exports.Stomp = Stomp; self.Stomp = Stomp;
Stomp.WebSocketClass = require('./test/server.mock.js').StompServerMock;
} }
}).call(this); }).call(this);
...@@ -17,11 +17,10 @@ require.config( ...@@ -17,11 +17,10 @@ require.config(
} }
}); });
require(["sally_client", "theo", "frames", "repository"], function(SallyClient, Theo, Frames, Repository) { require(["sally_client", "theo", "frames"], function(SallyClient, Theo, Frames) {
var theo = new Theo(); var theo = new Theo();
var frames = new Frames(); var frames = new Frames();
var repository = new Repository("gl.kwarc.info/test/test", "blah.txt");
frames.on("NewDocLevelService", function(msg) { frames.on("NewDocLevelService", function(msg) {
frames.executeDocLevelService(msg.id); frames.executeDocLevelService(msg.id);
...@@ -33,7 +32,7 @@ require(["sally_client", "theo", "frames", "repository"], function(SallyClient, ...@@ -33,7 +32,7 @@ require(["sally_client", "theo", "frames", "repository"], function(SallyClient,
client = new SallyClient({stompUrl: "ws://localhost:61614", stompUser : "webclient", stompPassword : "webclient"}); client = new SallyClient({stompUrl: "ws://localhost:61614", stompUser : "webclient", stompPassword : "webclient"});
client.register([theo, frames, repository], "env"+Math.floor(Math.random()*100000), function() { client.register([theo, frames], "env"+Math.floor(Math.random()*100000), function() {
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment