mirror of https://github.com/apache/activemq.git
https://issues.apache.org/jira/browse/AMQ-4200 Update the websocket example to use the latest stomp.js which supports STOMP 1.1 (including heart-beating).
Also added a websocket connector to the activemq-demo.xml config file. Thanks Jeff Mensil!! git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1416524 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
cbf2adb343
commit
ff0746c190
|
@ -1,148 +1,269 @@
|
|||
/**
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
// Generated by CoffeeScript 1.3.3
|
||||
(function() {
|
||||
var Client, Stomp, WebSocketStompMock;
|
||||
var __hasProp = Object.prototype.hasOwnProperty, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
Stomp = {
|
||||
frame: function(command, headers, body) {
|
||||
if (headers == null) {
|
||||
headers = [];
|
||||
var Byte, Client, Frame, Stomp,
|
||||
__hasProp = {}.hasOwnProperty;
|
||||
|
||||
Byte = {
|
||||
LF: '\x0A',
|
||||
NULL: '\x00'
|
||||
};
|
||||
|
||||
Frame = (function() {
|
||||
|
||||
function Frame(command, headers, body) {
|
||||
this.command = command;
|
||||
this.headers = headers != null ? headers : {};
|
||||
this.body = body != null ? body : '';
|
||||
}
|
||||
|
||||
Frame.prototype.toString = function() {
|
||||
var lines, name, value, _ref;
|
||||
lines = [this.command];
|
||||
_ref = this.headers;
|
||||
for (name in _ref) {
|
||||
if (!__hasProp.call(_ref, name)) continue;
|
||||
value = _ref[name];
|
||||
lines.push("" + name + ":" + value);
|
||||
}
|
||||
if (body == null) {
|
||||
body = '';
|
||||
if (this.body) {
|
||||
lines.push("content-length:" + ('' + this.body).length);
|
||||
}
|
||||
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');
|
||||
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;
|
||||
divider = data.search(RegExp("" + Byte.LF + Byte.LF));
|
||||
headerLines = data.substring(0, divider).split(Byte.LF);
|
||||
command = headerLines.shift();
|
||||
headers = {};
|
||||
body = '';
|
||||
trim = function(str) {
|
||||
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
|
||||
return str.replace(/^\s+|\s+$/g, '');
|
||||
};
|
||||
line = idx = null;
|
||||
for (i = 0, _ref = headerLines.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
|
||||
for (i = _i = 0, _ref = headerLines.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 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 = '';
|
||||
start = divider + 2;
|
||||
if (headers['content-length']) {
|
||||
len = parseInt(headers['content-length']);
|
||||
body = ('' + data).substring(start, start + len);
|
||||
} else {
|
||||
chr = null;
|
||||
for (i = _j = start, _ref1 = data.length; start <= _ref1 ? _j < _ref1 : _j > _ref1; i = start <= _ref1 ? ++_j : --_j) {
|
||||
chr = data.charAt(i);
|
||||
if (chr === Byte.NULL) {
|
||||
break;
|
||||
}
|
||||
body += chr;
|
||||
}
|
||||
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);
|
||||
}
|
||||
};
|
||||
return new Frame(command, headers, body);
|
||||
};
|
||||
|
||||
Frame.unmarshall = function(datas) {
|
||||
var data;
|
||||
return (function() {
|
||||
var _i, _len, _ref, _results;
|
||||
_ref = datas.split(RegExp("" + Byte.NULL + Byte.LF + "*"));
|
||||
_results = [];
|
||||
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));
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
})();
|
||||
};
|
||||
|
||||
Frame.marshall = function(command, headers, body) {
|
||||
var frame;
|
||||
frame = new Frame(command, headers, body);
|
||||
return frame.toString() + Byte.NULL;
|
||||
};
|
||||
|
||||
return Frame;
|
||||
|
||||
})();
|
||||
|
||||
Client = (function() {
|
||||
function Client(url) {
|
||||
this.url = url;
|
||||
|
||||
function Client(ws) {
|
||||
this.ws = ws;
|
||||
this.ws.binaryType = "arraybuffer";
|
||||
this.counter = 0;
|
||||
this.connected = false;
|
||||
this.heartbeat = {
|
||||
outgoing: 10000,
|
||||
incoming: 10000
|
||||
};
|
||||
this.subscriptions = {};
|
||||
}
|
||||
|
||||
Client.prototype._transmit = function(command, headers, body) {
|
||||
var out;
|
||||
out = Stomp.marshal(command, headers, body);
|
||||
out = Frame.marshall(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;
|
||||
if (typeof this.debug === "function") {
|
||||
this.debug("Opening Web Socket..." + this.url);
|
||||
|
||||
Client.prototype._setupHeartbeat = function(headers) {
|
||||
var serverIncoming, serverOutgoing, ttl, v, _ref, _ref1,
|
||||
_this = this;
|
||||
if ((_ref = headers.version) !== Stomp.VERSIONS.V1_1 && _ref !== Stomp.VERSIONS.V1_2) {
|
||||
return;
|
||||
}
|
||||
klass = WebSocketStompMock || WebSocket;
|
||||
this.ws = new klass(this.url);
|
||||
this.ws.onmessage = __bind(function(evt) {
|
||||
var frame, onreceive;
|
||||
_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") {
|
||||
this.debug('<<< ' + evt.data);
|
||||
this.debug("send PING every " + ttl + "ms");
|
||||
}
|
||||
frame = Stomp.unmarshal(evt.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.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)) {
|
||||
ttl = Math.max(this.heartbeat.incoming, serverOutgoing);
|
||||
if (typeof this.debug === "function") {
|
||||
this.debug("check PONG every " + ttl + "ms");
|
||||
}
|
||||
}, this);
|
||||
this.ws.onclose = __bind(function() {
|
||||
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();
|
||||
}
|
||||
}, ttl) : void 0;
|
||||
}
|
||||
};
|
||||
|
||||
Client.prototype.connect = function(login, passcode, connectCallback, errorCallback, vhost) {
|
||||
var _this = this;
|
||||
this.connectCallback = connectCallback;
|
||||
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;
|
||||
_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 = Date.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 = _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);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
this.ws.onclose = function() {
|
||||
var msg;
|
||||
msg = "Whoops! Lost connection to " + this.url;
|
||||
if (typeof this.debug === "function") {
|
||||
this.debug(msg);
|
||||
msg = "Whoops! Lost connection to " + _this.ws.url;
|
||||
if (typeof _this.debug === "function") {
|
||||
_this.debug(msg);
|
||||
}
|
||||
return typeof errorCallback === "function" ? errorCallback(msg) : void 0;
|
||||
}, this);
|
||||
this.ws.onopen = __bind(function() {
|
||||
if (typeof this.debug === "function") {
|
||||
this.debug('Web Socket Opened...');
|
||||
};
|
||||
return this.ws.onopen = function() {
|
||||
var headers;
|
||||
if (typeof _this.debug === "function") {
|
||||
_this.debug('Web Socket Opened...');
|
||||
}
|
||||
return this._transmit("CONNECT", {
|
||||
login: login_,
|
||||
passcode: passcode_
|
||||
});
|
||||
}, this);
|
||||
return this.connectCallback = connectCallback;
|
||||
headers = {
|
||||
"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);
|
||||
};
|
||||
};
|
||||
|
||||
Client.prototype.disconnect = function(disconnectCallback) {
|
||||
this._transmit("DISCONNECT");
|
||||
this.ws.close();
|
||||
this.connected = false;
|
||||
this.ws.onclose = null;
|
||||
this._cleanUp();
|
||||
return typeof disconnectCallback === "function" ? disconnectCallback() : void 0;
|
||||
};
|
||||
|
||||
Client.prototype._cleanUp = function() {
|
||||
this.ws.close();
|
||||
this.connected = false;
|
||||
if (this.pinger) {
|
||||
if (typeof window !== "undefined" && window !== null) {
|
||||
window.clearInterval(this.pinger);
|
||||
}
|
||||
}
|
||||
if (this.ponger) {
|
||||
return typeof window !== "undefined" && window !== null ? window.clearInterval(this.ponger) : void 0;
|
||||
}
|
||||
};
|
||||
|
||||
Client.prototype.send = function(destination, headers, body) {
|
||||
if (headers == null) {
|
||||
headers = {};
|
||||
|
@ -153,60 +274,97 @@
|
|||
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++;
|
||||
if (!headers.id) {
|
||||
headers.id = "sub-" + this.counter++;
|
||||
}
|
||||
headers.destination = destination;
|
||||
headers.id = id;
|
||||
this.subscriptions[id] = callback;
|
||||
this.subscriptions[headers.id] = callback;
|
||||
this._transmit("SUBSCRIBE", headers);
|
||||
return id;
|
||||
return headers.id;
|
||||
};
|
||||
Client.prototype.unsubscribe = function(id, headers) {
|
||||
if (headers == null) {
|
||||
headers = {};
|
||||
}
|
||||
headers.id = id;
|
||||
|
||||
Client.prototype.unsubscribe = function(id) {
|
||||
delete this.subscriptions[id];
|
||||
return this._transmit("UNSUBSCRIBE", headers);
|
||||
return this._transmit("UNSUBSCRIBE", {
|
||||
id: id
|
||||
});
|
||||
};
|
||||
Client.prototype.begin = function(transaction, headers) {
|
||||
|
||||
Client.prototype.begin = function(transaction) {
|
||||
return this._transmit("BEGIN", {
|
||||
transaction: transaction
|
||||
});
|
||||
};
|
||||
|
||||
Client.prototype.commit = function(transaction) {
|
||||
return this._transmit("COMMIT", {
|
||||
transaction: transaction
|
||||
});
|
||||
};
|
||||
|
||||
Client.prototype.abort = function(transaction) {
|
||||
return this._transmit("ABORT", {
|
||||
transaction: transaction
|
||||
});
|
||||
};
|
||||
|
||||
Client.prototype.ack = function(messageID, subscription, 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;
|
||||
headers["message-id"] = messageID;
|
||||
headers.subscription = subscription;
|
||||
return this._transmit("ACK", headers);
|
||||
};
|
||||
|
||||
Client.prototype.nack = function(messageID, subscription, headers) {
|
||||
if (headers == null) {
|
||||
headers = {};
|
||||
}
|
||||
headers["message-id"] = messageID;
|
||||
headers.subscription = subscription;
|
||||
return this._transmit("NACK", headers);
|
||||
};
|
||||
|
||||
return Client;
|
||||
|
||||
})();
|
||||
|
||||
Stomp = {
|
||||
libVersion: "2.0.0-next",
|
||||
VERSIONS: {
|
||||
V1_0: '1.0',
|
||||
V1_1: '1.1',
|
||||
V1_2: '1.2',
|
||||
supportedVersions: function() {
|
||||
return '1.1,1.0';
|
||||
}
|
||||
},
|
||||
client: function(url, protocols) {
|
||||
var klass, ws;
|
||||
if (protocols == null) {
|
||||
protocols = ['v10.stomp', 'v11.stomp'];
|
||||
}
|
||||
klass = Stomp.WebSocketClass || WebSocket;
|
||||
ws = new klass(url, protocols);
|
||||
return new Client(ws);
|
||||
},
|
||||
over: function(ws) {
|
||||
return new Client(ws);
|
||||
},
|
||||
Frame: Frame
|
||||
};
|
||||
|
||||
if (typeof window !== "undefined" && window !== null) {
|
||||
window.Stomp = Stomp;
|
||||
} else {
|
||||
exports.Stomp = Stomp;
|
||||
WebSocketStompMock = require('./test/server.mock.js').StompServerMock;
|
||||
Stomp.WebSocketClass = require('./test/server.mock.js').StompServerMock;
|
||||
}
|
||||
}).call(this);
|
||||
|
||||
}).call(this);
|
||||
|
|
|
@ -1,148 +1,269 @@
|
|||
/**
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
// Generated by CoffeeScript 1.3.3
|
||||
(function() {
|
||||
var Client, Stomp, WebSocketStompMock;
|
||||
var __hasProp = Object.prototype.hasOwnProperty, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
Stomp = {
|
||||
frame: function(command, headers, body) {
|
||||
if (headers == null) {
|
||||
headers = [];
|
||||
var Byte, Client, Frame, Stomp,
|
||||
__hasProp = {}.hasOwnProperty;
|
||||
|
||||
Byte = {
|
||||
LF: '\x0A',
|
||||
NULL: '\x00'
|
||||
};
|
||||
|
||||
Frame = (function() {
|
||||
|
||||
function Frame(command, headers, body) {
|
||||
this.command = command;
|
||||
this.headers = headers != null ? headers : {};
|
||||
this.body = body != null ? body : '';
|
||||
}
|
||||
|
||||
Frame.prototype.toString = function() {
|
||||
var lines, name, value, _ref;
|
||||
lines = [this.command];
|
||||
_ref = this.headers;
|
||||
for (name in _ref) {
|
||||
if (!__hasProp.call(_ref, name)) continue;
|
||||
value = _ref[name];
|
||||
lines.push("" + name + ":" + value);
|
||||
}
|
||||
if (body == null) {
|
||||
body = '';
|
||||
if (this.body) {
|
||||
lines.push("content-length:" + ('' + this.body).length);
|
||||
}
|
||||
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');
|
||||
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;
|
||||
divider = data.search(RegExp("" + Byte.LF + Byte.LF));
|
||||
headerLines = data.substring(0, divider).split(Byte.LF);
|
||||
command = headerLines.shift();
|
||||
headers = {};
|
||||
body = '';
|
||||
trim = function(str) {
|
||||
return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
|
||||
return str.replace(/^\s+|\s+$/g, '');
|
||||
};
|
||||
line = idx = null;
|
||||
for (i = 0, _ref = headerLines.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
|
||||
for (i = _i = 0, _ref = headerLines.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 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 = '';
|
||||
start = divider + 2;
|
||||
if (headers['content-length']) {
|
||||
len = parseInt(headers['content-length']);
|
||||
body = ('' + data).substring(start, start + len);
|
||||
} else {
|
||||
chr = null;
|
||||
for (i = _j = start, _ref1 = data.length; start <= _ref1 ? _j < _ref1 : _j > _ref1; i = start <= _ref1 ? ++_j : --_j) {
|
||||
chr = data.charAt(i);
|
||||
if (chr === Byte.NULL) {
|
||||
break;
|
||||
}
|
||||
body += chr;
|
||||
}
|
||||
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);
|
||||
}
|
||||
};
|
||||
return new Frame(command, headers, body);
|
||||
};
|
||||
|
||||
Frame.unmarshall = function(datas) {
|
||||
var data;
|
||||
return (function() {
|
||||
var _i, _len, _ref, _results;
|
||||
_ref = datas.split(RegExp("" + Byte.NULL + Byte.LF + "*"));
|
||||
_results = [];
|
||||
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));
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
})();
|
||||
};
|
||||
|
||||
Frame.marshall = function(command, headers, body) {
|
||||
var frame;
|
||||
frame = new Frame(command, headers, body);
|
||||
return frame.toString() + Byte.NULL;
|
||||
};
|
||||
|
||||
return Frame;
|
||||
|
||||
})();
|
||||
|
||||
Client = (function() {
|
||||
function Client(url) {
|
||||
this.url = url;
|
||||
|
||||
function Client(ws) {
|
||||
this.ws = ws;
|
||||
this.ws.binaryType = "arraybuffer";
|
||||
this.counter = 0;
|
||||
this.connected = false;
|
||||
this.heartbeat = {
|
||||
outgoing: 10000,
|
||||
incoming: 10000
|
||||
};
|
||||
this.subscriptions = {};
|
||||
}
|
||||
|
||||
Client.prototype._transmit = function(command, headers, body) {
|
||||
var out;
|
||||
out = Stomp.marshal(command, headers, body);
|
||||
out = Frame.marshall(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;
|
||||
|
||||
Client.prototype._setupHeartbeat = function(headers) {
|
||||
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") {
|
||||
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;
|
||||
}
|
||||
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._cleanUp();
|
||||
}
|
||||
}, ttl) : void 0;
|
||||
}
|
||||
};
|
||||
|
||||
Client.prototype.connect = function(login, passcode, connectCallback, errorCallback, vhost) {
|
||||
var _this = this;
|
||||
this.connectCallback = connectCallback;
|
||||
if (typeof this.debug === "function") {
|
||||
this.debug("Opening Web Socket...");
|
||||
}
|
||||
klass = WebSocketStompMock || WebSocket;
|
||||
this.ws = new klass(this.url);
|
||||
this.ws.onmessage = __bind(function(evt) {
|
||||
var frame, onreceive;
|
||||
if (typeof this.debug === "function") {
|
||||
this.debug('<<< ' + evt.data);
|
||||
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;
|
||||
_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 = Date.now();
|
||||
if (data === Byte.LF) {
|
||||
if (typeof _this.debug === "function") {
|
||||
_this.debug("<<< PONG");
|
||||
}
|
||||
return;
|
||||
}
|
||||
frame = Stomp.unmarshal(evt.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;
|
||||
if (typeof _this.debug === "function") {
|
||||
_this.debug("<<< " + data);
|
||||
}
|
||||
}, this);
|
||||
this.ws.onclose = __bind(function() {
|
||||
_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);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
this.ws.onclose = function() {
|
||||
var msg;
|
||||
msg = "Whoops! Lost connection to " + this.url;
|
||||
if (typeof this.debug === "function") {
|
||||
this.debug(msg);
|
||||
msg = "Whoops! Lost connection to " + _this.ws.url;
|
||||
if (typeof _this.debug === "function") {
|
||||
_this.debug(msg);
|
||||
}
|
||||
return typeof errorCallback === "function" ? errorCallback(msg) : void 0;
|
||||
}, this);
|
||||
this.ws.onopen = __bind(function() {
|
||||
if (typeof this.debug === "function") {
|
||||
this.debug('Web Socket Opened...');
|
||||
};
|
||||
return this.ws.onopen = function() {
|
||||
var headers;
|
||||
if (typeof _this.debug === "function") {
|
||||
_this.debug('Web Socket Opened...');
|
||||
}
|
||||
return this._transmit("CONNECT", {
|
||||
login: login_,
|
||||
passcode: passcode_
|
||||
});
|
||||
}, this);
|
||||
return this.connectCallback = connectCallback;
|
||||
headers = {
|
||||
"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);
|
||||
};
|
||||
};
|
||||
|
||||
Client.prototype.disconnect = function(disconnectCallback) {
|
||||
this._transmit("DISCONNECT");
|
||||
this.ws.close();
|
||||
this.connected = false;
|
||||
this.ws.onclose = null;
|
||||
this._cleanUp();
|
||||
return typeof disconnectCallback === "function" ? disconnectCallback() : void 0;
|
||||
};
|
||||
|
||||
Client.prototype._cleanUp = function() {
|
||||
this.ws.close();
|
||||
this.connected = false;
|
||||
if (this.pinger) {
|
||||
if (typeof window !== "undefined" && window !== null) {
|
||||
window.clearInterval(this.pinger);
|
||||
}
|
||||
}
|
||||
if (this.ponger) {
|
||||
return typeof window !== "undefined" && window !== null ? window.clearInterval(this.ponger) : void 0;
|
||||
}
|
||||
};
|
||||
|
||||
Client.prototype.send = function(destination, headers, body) {
|
||||
if (headers == null) {
|
||||
headers = {};
|
||||
|
@ -153,60 +274,97 @@
|
|||
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++;
|
||||
if (!headers.id) {
|
||||
headers.id = "sub-" + this.counter++;
|
||||
}
|
||||
headers.destination = destination;
|
||||
headers.id = id;
|
||||
this.subscriptions[id] = callback;
|
||||
this.subscriptions[headers.id] = callback;
|
||||
this._transmit("SUBSCRIBE", headers);
|
||||
return id;
|
||||
return headers.id;
|
||||
};
|
||||
Client.prototype.unsubscribe = function(id, headers) {
|
||||
if (headers == null) {
|
||||
headers = {};
|
||||
}
|
||||
headers.id = id;
|
||||
|
||||
Client.prototype.unsubscribe = function(id) {
|
||||
delete this.subscriptions[id];
|
||||
return this._transmit("UNSUBSCRIBE", headers);
|
||||
return this._transmit("UNSUBSCRIBE", {
|
||||
id: id
|
||||
});
|
||||
};
|
||||
Client.prototype.begin = function(transaction, headers) {
|
||||
|
||||
Client.prototype.begin = function(transaction) {
|
||||
return this._transmit("BEGIN", {
|
||||
transaction: transaction
|
||||
});
|
||||
};
|
||||
|
||||
Client.prototype.commit = function(transaction) {
|
||||
return this._transmit("COMMIT", {
|
||||
transaction: transaction
|
||||
});
|
||||
};
|
||||
|
||||
Client.prototype.abort = function(transaction) {
|
||||
return this._transmit("ABORT", {
|
||||
transaction: transaction
|
||||
});
|
||||
};
|
||||
|
||||
Client.prototype.ack = function(messageID, subscription, 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;
|
||||
headers["message-id"] = messageID;
|
||||
headers.subscription = subscription;
|
||||
return this._transmit("ACK", headers);
|
||||
};
|
||||
|
||||
Client.prototype.nack = function(messageID, subscription, headers) {
|
||||
if (headers == null) {
|
||||
headers = {};
|
||||
}
|
||||
headers["message-id"] = messageID;
|
||||
headers.subscription = subscription;
|
||||
return this._transmit("NACK", headers);
|
||||
};
|
||||
|
||||
return Client;
|
||||
|
||||
})();
|
||||
|
||||
Stomp = {
|
||||
libVersion: "2.0.0-next",
|
||||
VERSIONS: {
|
||||
V1_0: '1.0',
|
||||
V1_1: '1.1',
|
||||
V1_2: '1.2',
|
||||
supportedVersions: function() {
|
||||
return '1.1,1.0';
|
||||
}
|
||||
},
|
||||
client: function(url, protocols) {
|
||||
var klass, ws;
|
||||
if (protocols == null) {
|
||||
protocols = ['v10.stomp', 'v11.stomp'];
|
||||
}
|
||||
klass = Stomp.WebSocketClass || WebSocket;
|
||||
ws = new klass(url, protocols);
|
||||
return new Client(ws);
|
||||
},
|
||||
over: function(ws) {
|
||||
return new Client(ws);
|
||||
},
|
||||
Frame: Frame
|
||||
};
|
||||
|
||||
if (typeof window !== "undefined" && window !== null) {
|
||||
window.Stomp = Stomp;
|
||||
} else {
|
||||
exports.Stomp = Stomp;
|
||||
WebSocketStompMock = require('./test/server.mock.js').StompServerMock;
|
||||
Stomp.WebSocketClass = require('./test/server.mock.js').StompServerMock;
|
||||
}
|
||||
}).call(this);
|
||||
|
||||
}).call(this);
|
||||
|
|
|
@ -203,6 +203,8 @@
|
|||
<transportConnector name="stomp" uri="stomp://localhost:61613"/>
|
||||
<!-- Create a XMPP transport for XMPP clients. -->
|
||||
<transportConnector name="xmpp" uri="xmpp://localhost:61222"/>
|
||||
<!-- Create a Websocket transport for the websocket dmeo -->
|
||||
<transportConnector name="ws" uri="ws://localhost:61614/" />
|
||||
</transportConnectors>
|
||||
|
||||
<!-- destroy the spring context on shutdown to stop jetty -->
|
||||
|
|
Loading…
Reference in New Issue