Upgrade to StompJs v5
This commit is contained in:
parent
8415ec39a2
commit
37ad4c0c99
@ -168,16 +168,16 @@ var vm = new Vue({
|
|||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
hostGame: function(e) {
|
hostGame: function(e) {
|
||||||
client.send('/app/hostGame');
|
client.publish({destination: '/app/hostGame'})
|
||||||
},
|
},
|
||||||
joinGame: function(e) {
|
joinGame: function(e) {
|
||||||
// Discard 'game-' prefix
|
// Discard 'game-' prefix
|
||||||
var buttonId = e.target.id;
|
var buttonId = e.target.id;
|
||||||
var gameId = buttonId.substr(5);
|
var gameId = buttonId.substr(5);
|
||||||
client.send('/app/joinGame', {}, gameId);
|
client.publish({destination: '/app/joinGame', body: gameId})
|
||||||
},
|
},
|
||||||
leaveGame: function(e) {
|
leaveGame: function(e) {
|
||||||
client.send('/app/leaveGame');
|
client.publish({destination: '/app/leaveGame'})
|
||||||
},
|
},
|
||||||
removeGame: function(gameId) {
|
removeGame: function(gameId) {
|
||||||
var indexToRemove = null;
|
var indexToRemove = null;
|
||||||
@ -197,7 +197,7 @@ var vm = new Vue({
|
|||||||
}
|
}
|
||||||
else if (e.which === KEYCODE_RETURN) {
|
else if (e.which === KEYCODE_RETURN) {
|
||||||
if (this.myGuess.length === 5) {
|
if (this.myGuess.length === 5) {
|
||||||
client.send("/app/guess", {}, this.myGuess);
|
client.publish({destination: '/app/guess', body: this.myGuess})
|
||||||
this.myGuess = '';
|
this.myGuess = '';
|
||||||
this.repaint();
|
this.repaint();
|
||||||
}
|
}
|
||||||
@ -227,7 +227,7 @@ var vm = new Vue({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
messageInput.value = '';
|
messageInput.value = '';
|
||||||
client.send('/app/chat', {}, text);
|
client.publish({destination: '/app/chat', body: text})
|
||||||
addChatMessage(this.username, text);
|
addChatMessage(this.username, text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -279,13 +279,45 @@ function main() {
|
|||||||
webSocketProtocol = 'wss';
|
webSocketProtocol = 'wss';
|
||||||
}
|
}
|
||||||
|
|
||||||
client = Stomp.client(`${webSocketProtocol}://${location.host}/stomp`);
|
client = new StompJs.Client({
|
||||||
client.heartbeat.outgoing = 25000; // send PING every 25 seconds
|
brokerURL: `${webSocketProtocol}://${location.host}/stomp`,
|
||||||
client.heartbeat.incoming = 0; // do NOT want PONGs from server
|
connectHeaders: {},
|
||||||
client.connect({}, afterConnected, function(errorMessage) {
|
//reconnectDelay: 5000, // TODO: implement reconnect functionality
|
||||||
|
reconnectDelay: 0, // Disable automatic reconnects
|
||||||
|
heartbeatIncoming: 25000, // send PING every 25 seconds
|
||||||
|
heartbeatOutgoing: 25000, // receive PONG every 25 seconds
|
||||||
|
//debug: function (str) { console.log(str); } // Print debug logs
|
||||||
|
});
|
||||||
|
|
||||||
|
client.onConnect = function(frame) {
|
||||||
|
afterConnected(frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Will be invoked in case of error encountered at broker.
|
||||||
|
// Bad login/passcode typically will cause an error.
|
||||||
|
// Complaint brokers will set `message` header with a brief message. Body may contain details.
|
||||||
|
// Compliant brokers will terminate the connection after any error.
|
||||||
|
client.onStompError = function(frame) {
|
||||||
|
console.log('Broker reported error: ' + frame.headers['message']);
|
||||||
|
console.log('Additional details: ' + frame.body);
|
||||||
|
var errorMessage = frame.headers['message'];
|
||||||
addChatAnnouncement(errorMessage);
|
addChatAnnouncement(errorMessage);
|
||||||
addChatAnnouncement('Please reload the page!');
|
addChatAnnouncement('Please reload the page!');
|
||||||
});
|
}
|
||||||
|
|
||||||
|
client.onWebSocketClose = function(wsCloseEvent) {
|
||||||
|
console.log('WebSocket close event: ' + JSON.stringify(wsCloseEvent));
|
||||||
|
addChatAnnouncement('WebSocket closed!');
|
||||||
|
addChatAnnouncement('Please reload the page!');
|
||||||
|
}
|
||||||
|
|
||||||
|
client.onWebSocketError = function(wsErrorEvent) {
|
||||||
|
console.log('WebSocket error event: ' + JSON.stringify(wsErrorEvent));
|
||||||
|
addChatAnnouncement('WebSocket error!');
|
||||||
|
addChatAnnouncement('Please reload the page!');
|
||||||
|
}
|
||||||
|
|
||||||
|
client.activate();
|
||||||
|
|
||||||
var usernameInput = document.getElementById('nicknameInput');
|
var usernameInput = document.getElementById('nicknameInput');
|
||||||
|
|
||||||
@ -318,7 +350,7 @@ function main() {
|
|||||||
usernameSubscription = client.subscribe(usernameTopic, usernameHandler);
|
usernameSubscription = client.subscribe(usernameTopic, usernameHandler);
|
||||||
client.subscribe('/topic/userJoined', onUserJoined);
|
client.subscribe('/topic/userJoined', onUserJoined);
|
||||||
}
|
}
|
||||||
client.send('/app/setUsername', {}, usernameValue);
|
client.publish({destination: '/app/setUsername', body: usernameValue})
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
usernameInput.addEventListener('keyup', function(e) {
|
usernameInput.addEventListener('keyup', function(e) {
|
||||||
@ -414,15 +446,6 @@ function isCharacterUppercase(charCode) {
|
|||||||
return charCode >= 65 && charCode <= 90;
|
return charCode >= 65 && charCode <= 90;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValidResult(result) {
|
|
||||||
for (var i = 0; i < 5; i++) {
|
|
||||||
if (result[i] !== 0 && result[i] !== 1 && result[i] !== 2) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
function onChat(message) {
|
function onChat(message) {
|
||||||
var chatMessage = JSON.parse(message.body);
|
var chatMessage = JSON.parse(message.body);
|
||||||
var messageSender = chatMessage.username;
|
var messageSender = chatMessage.username;
|
||||||
@ -432,7 +455,7 @@ function onChat(message) {
|
|||||||
} else if (messageSender === vm.username) {
|
} else if (messageSender === vm.username) {
|
||||||
// Ignore messages sent by yourself
|
// Ignore messages sent by yourself
|
||||||
} else {
|
} else {
|
||||||
console.log('Message from ' + messageSender + ": " + messageBody);
|
console.log('Message from ' + messageSender + ': ' + messageBody);
|
||||||
addChatMessage(messageSender, messageBody);
|
addChatMessage(messageSender, messageBody);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -524,30 +547,28 @@ function onOpponentJoined(message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onOpponentReport(message) {
|
function onOpponentReport(message) {
|
||||||
|
console.log('Opponent report: ' + message.body);
|
||||||
var report = JSON.parse(message.body);
|
var report = JSON.parse(message.body);
|
||||||
if (report.correct === true) {
|
if (report.correct === true) {
|
||||||
var guess = report.guess;
|
var guess = report.guess;
|
||||||
var firstLetter = report.firstLetter;
|
var firstLetter = report.firstLetter;
|
||||||
console.log('Opponent guessed correctly! ' + guess);
|
|
||||||
vm.opponentScore = vm.opponentScore + 100;
|
vm.opponentScore = vm.opponentScore + 100;
|
||||||
vm.lastWord = guess;
|
vm.lastWord = guess;
|
||||||
vm.reset(firstLetter, false);
|
vm.reset(firstLetter, false);
|
||||||
vm.repaint();
|
vm.repaint();
|
||||||
} else {
|
} else {
|
||||||
var result = report.result;
|
var result = report.result;
|
||||||
console.log('Opponent result: ' + result);
|
|
||||||
vm.opponentResults.push(result);
|
vm.opponentResults.push(result);
|
||||||
vm.repaint();
|
vm.repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onPlayerReport(message) {
|
function onPlayerReport(message) {
|
||||||
|
console.log('My report: ' + message.body);
|
||||||
var report = JSON.parse(message.body);
|
var report = JSON.parse(message.body);
|
||||||
console.log('My report: ' + report);
|
|
||||||
if (report.correct === true) {
|
if (report.correct === true) {
|
||||||
var guess = report.guess;
|
var guess = report.guess;
|
||||||
var firstLetter = report.firstLetter;
|
var firstLetter = report.firstLetter;
|
||||||
console.log('I guessed correctly!');
|
|
||||||
vm.myScore = vm.myScore + 100;
|
vm.myScore = vm.myScore + 100;
|
||||||
vm.lastWord = guess;
|
vm.lastWord = guess;
|
||||||
vm.reset(firstLetter, false);
|
vm.reset(firstLetter, false);
|
||||||
@ -555,8 +576,6 @@ function onPlayerReport(message) {
|
|||||||
} else {
|
} else {
|
||||||
var guess = report.guess;
|
var guess = report.guess;
|
||||||
var result = report.result;
|
var result = report.result;
|
||||||
console.log('My result: ' + result);
|
|
||||||
// TODO: use isValidResult function
|
|
||||||
if (result[0] === 9) {
|
if (result[0] === 9) {
|
||||||
vm.myGuesses.push('-----');
|
vm.myGuesses.push('-----');
|
||||||
} else {
|
} else {
|
||||||
|
@ -5,7 +5,8 @@
|
|||||||
<title>Lingo</title>
|
<title>Lingo</title>
|
||||||
<link rel="stylesheet" href="layout.css">
|
<link rel="stylesheet" href="layout.css">
|
||||||
<link rel="stylesheet" href="style.css">
|
<link rel="stylesheet" href="style.css">
|
||||||
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.js"></script>
|
<script src="//cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js"></script>
|
||||||
|
<script src="//cdn.jsdelivr.net/npm/@stomp/stompjs@5.2.0/bundles/stomp.umd.min.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="vue-app" v-cloak>
|
<div id="vue-app" v-cloak>
|
||||||
@ -64,8 +65,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- https://github.com/stomp-js/stomp-websocket -->
|
|
||||||
<script src="stomp-3.1.1.min.js"></script>
|
|
||||||
<script src="client.js"></script>
|
<script src="client.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -11,8 +11,7 @@
|
|||||||
<button id="skipButton" type="button">Skip Word</button>
|
<button id="skipButton" type="button">Skip Word</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="//cdnjs.cloudflare.com/ajax/libs/sockjs-client/1.1.1/sockjs.min.js"></script>
|
<script src="//cdn.jsdelivr.net/npm/@stomp/stompjs@5.2.0/bundles/stomp.umd.min.js"></script>
|
||||||
<script src="//cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>
|
|
||||||
<script src="practice.js"></script>
|
<script src="practice.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -37,14 +37,24 @@ function start() {
|
|||||||
webSocketProtocol = 'wss';
|
webSocketProtocol = 'wss';
|
||||||
}
|
}
|
||||||
|
|
||||||
client = Stomp.client(`${webSocketProtocol}://${location.host}/stomp`);
|
client = new StompJs.Client({
|
||||||
|
brokerURL: `${webSocketProtocol}://${location.host}/stomp`,
|
||||||
|
connectHeaders: {},
|
||||||
|
//reconnectDelay: 5000, // TODO: implement reconnect functionality
|
||||||
|
reconnectDelay: 0, // Disable automatic reconnects
|
||||||
|
heartbeatIncoming: 25000, // send PING every 25 seconds
|
||||||
|
heartbeatOutgoing: 25000, // receive PONG every 25 seconds
|
||||||
|
//debug: function (str) { console.log(str); } // Print debug logs
|
||||||
|
});
|
||||||
|
|
||||||
client.connect({}, function(frame) {
|
client.onConnect = function(frame) {
|
||||||
subscribeToPracticeGame();
|
subscribeToPracticeGame();
|
||||||
subscribeToPracticeReports();
|
subscribeToPracticeReports();
|
||||||
subscribeToPracticeWordSkipped();
|
subscribeToPracticeWordSkipped();
|
||||||
client.send('/app/practiceGame');
|
client.publish({destination: '/app/practiceGame'});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
client.activate();
|
||||||
}
|
}
|
||||||
|
|
||||||
// special keys
|
// special keys
|
||||||
@ -59,7 +69,7 @@ function addKeydownListener() {
|
|||||||
// return
|
// return
|
||||||
else if (e.which === 13) {
|
else if (e.which === 13) {
|
||||||
if (myGuess.length === 5) {
|
if (myGuess.length === 5) {
|
||||||
client.send("/app/practiceGuess", {}, myGuess);
|
client.publish({destination: '/app/practiceGuess', body: myGuess});
|
||||||
myGuess = '';
|
myGuess = '';
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
@ -87,7 +97,7 @@ function addKeypressListener() {
|
|||||||
// skip button
|
// skip button
|
||||||
function addSkipButtonListener() {
|
function addSkipButtonListener() {
|
||||||
document.getElementById('skipButton').addEventListener('click', function(e) {
|
document.getElementById('skipButton').addEventListener('click', function(e) {
|
||||||
client.send("/app/practiceSkip");
|
client.publish({destination: '/app/practiceSkip'});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,15 +215,6 @@ function isCharacterUppercase(charCode) {
|
|||||||
return charCode >= 65 && charCode <= 90;
|
return charCode >= 65 && charCode <= 90;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isValidResult(result) {
|
|
||||||
for (var i = 0; i < 5; i++) {
|
|
||||||
if (result[i] !== 0 && result[i] !== 1 && result[i] !== 2) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
function repaint() {
|
function repaint() {
|
||||||
// clear the canvas
|
// clear the canvas
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
@ -247,12 +248,11 @@ function subscribeToPracticeGame() {
|
|||||||
|
|
||||||
function subscribeToPracticeReports() {
|
function subscribeToPracticeReports() {
|
||||||
client.subscribe('/user/topic/practiceReports', function(message) {
|
client.subscribe('/user/topic/practiceReports', function(message) {
|
||||||
|
console.log('My report: ' + message.body);
|
||||||
var report = JSON.parse(message.body);
|
var report = JSON.parse(message.body);
|
||||||
console.log('My report: ' + report);
|
|
||||||
if (report.correct === true) {
|
if (report.correct === true) {
|
||||||
var guess = report.guess;
|
var guess = report.guess;
|
||||||
var firstLetter = report.firstLetter;
|
var firstLetter = report.firstLetter;
|
||||||
console.log('I guessed correctly!');
|
|
||||||
myScore = myScore + 100;
|
myScore = myScore + 100;
|
||||||
lastWord = guess;
|
lastWord = guess;
|
||||||
reset(firstLetter, false);
|
reset(firstLetter, false);
|
||||||
@ -260,8 +260,6 @@ function subscribeToPracticeReports() {
|
|||||||
} else {
|
} else {
|
||||||
var guess = report.guess;
|
var guess = report.guess;
|
||||||
var result = report.result;
|
var result = report.result;
|
||||||
console.log('My result: ' + result);
|
|
||||||
// TODO: use isValidResult function
|
|
||||||
if (result[0] === 9) {
|
if (result[0] === 9) {
|
||||||
myGuesses.push('-----');
|
myGuesses.push('-----');
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,569 +0,0 @@
|
|||||||
// Generated by CoffeeScript 1.12.6
|
|
||||||
|
|
||||||
/*
|
|
||||||
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)
|
|
||||||
Copyright (C) 2017 [Deepak Kumar](https://www.kreatio.com)
|
|
||||||
*/
|
|
||||||
|
|
||||||
(function() {
|
|
||||||
var Byte, Client, Frame, Stomp,
|
|
||||||
hasProp = {}.hasOwnProperty,
|
|
||||||
slice = [].slice;
|
|
||||||
|
|
||||||
Byte = {
|
|
||||||
LF: '\x0A',
|
|
||||||
NULL: '\x00'
|
|
||||||
};
|
|
||||||
|
|
||||||
Frame = (function() {
|
|
||||||
var unmarshallSingle;
|
|
||||||
|
|
||||||
function Frame(command1, headers1, body1) {
|
|
||||||
this.command = command1;
|
|
||||||
this.headers = headers1 != null ? headers1 : {};
|
|
||||||
this.body = body1 != null ? body1 : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
Frame.prototype.toString = function() {
|
|
||||||
var lines, name, ref, skipContentLength, value;
|
|
||||||
lines = [this.command];
|
|
||||||
skipContentLength = (this.headers['content-length'] === false) ? true : false;
|
|
||||||
if (skipContentLength) {
|
|
||||||
delete this.headers['content-length'];
|
|
||||||
}
|
|
||||||
ref = this.headers;
|
|
||||||
for (name in ref) {
|
|
||||||
if (!hasProp.call(ref, name)) continue;
|
|
||||||
value = ref[name];
|
|
||||||
lines.push(name + ":" + value);
|
|
||||||
}
|
|
||||||
if (this.body && !skipContentLength) {
|
|
||||||
lines.push("content-length:" + (Frame.sizeOfUTF8(this.body)));
|
|
||||||
}
|
|
||||||
lines.push(Byte.LF + this.body);
|
|
||||||
return lines.join(Byte.LF);
|
|
||||||
};
|
|
||||||
|
|
||||||
Frame.sizeOfUTF8 = function(s) {
|
|
||||||
if (s) {
|
|
||||||
return encodeURI(s).match(/%..|./g).length;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
unmarshallSingle = function(data) {
|
|
||||||
var body, chr, command, divider, headerLines, headers, i, idx, j, k, len, len1, line, ref, ref1, ref2, start, trim;
|
|
||||||
divider = data.search(RegExp("" + Byte.LF + Byte.LF));
|
|
||||||
headerLines = data.substring(0, divider).split(Byte.LF);
|
|
||||||
command = headerLines.shift();
|
|
||||||
headers = {};
|
|
||||||
trim = function(str) {
|
|
||||||
return str.replace(/^\s+|\s+$/g, '');
|
|
||||||
};
|
|
||||||
ref = headerLines.reverse();
|
|
||||||
for (j = 0, len1 = ref.length; j < len1; j++) {
|
|
||||||
line = ref[j];
|
|
||||||
idx = line.indexOf(':');
|
|
||||||
headers[trim(line.substring(0, idx))] = trim(line.substring(idx + 1));
|
|
||||||
}
|
|
||||||
body = '';
|
|
||||||
start = divider + 2;
|
|
||||||
if (headers['content-length']) {
|
|
||||||
len = parseInt(headers['content-length']);
|
|
||||||
body = ('' + data).substring(start, start + len);
|
|
||||||
} else {
|
|
||||||
chr = null;
|
|
||||||
for (i = k = ref1 = start, ref2 = data.length; ref1 <= ref2 ? k < ref2 : k > ref2; i = ref1 <= ref2 ? ++k : --k) {
|
|
||||||
chr = data.charAt(i);
|
|
||||||
if (chr === Byte.NULL) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
body += chr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new Frame(command, headers, body);
|
|
||||||
};
|
|
||||||
|
|
||||||
Frame.unmarshall = function(datas) {
|
|
||||||
var frame, frames, last_frame, r;
|
|
||||||
frames = datas.split(RegExp("" + Byte.NULL + Byte.LF + "*"));
|
|
||||||
r = {
|
|
||||||
frames: [],
|
|
||||||
partial: ''
|
|
||||||
};
|
|
||||||
r.frames = (function() {
|
|
||||||
var j, len1, ref, results;
|
|
||||||
ref = frames.slice(0, -1);
|
|
||||||
results = [];
|
|
||||||
for (j = 0, len1 = ref.length; j < len1; j++) {
|
|
||||||
frame = ref[j];
|
|
||||||
results.push(unmarshallSingle(frame));
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
var frame;
|
|
||||||
frame = new Frame(command, headers, body);
|
|
||||||
return frame.toString() + Byte.NULL;
|
|
||||||
};
|
|
||||||
|
|
||||||
return Frame;
|
|
||||||
|
|
||||||
})();
|
|
||||||
|
|
||||||
Client = (function() {
|
|
||||||
var now;
|
|
||||||
|
|
||||||
function Client(ws_fn) {
|
|
||||||
this.ws_fn = function() {
|
|
||||||
var ws;
|
|
||||||
ws = ws_fn();
|
|
||||||
ws.binaryType = "arraybuffer";
|
|
||||||
return ws;
|
|
||||||
};
|
|
||||||
this.reconnect_delay = 0;
|
|
||||||
this.counter = 0;
|
|
||||||
this.connected = false;
|
|
||||||
this.heartbeat = {
|
|
||||||
outgoing: 10000,
|
|
||||||
incoming: 10000
|
|
||||||
};
|
|
||||||
this.maxWebSocketFrameSize = 16 * 1024;
|
|
||||||
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) {
|
|
||||||
var out;
|
|
||||||
out = Frame.marshall(command, headers, body);
|
|
||||||
if (typeof this.debug === "function") {
|
|
||||||
this.debug(">>> " + 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 ref, ref1, serverIncoming, serverOutgoing, ttl, v;
|
|
||||||
if ((ref = headers.version) !== Stomp.VERSIONS.V1_1 && ref !== Stomp.VERSIONS.V1_2) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ref1 = (function() {
|
|
||||||
var j, len1, ref1, results;
|
|
||||||
ref1 = headers['heart-beat'].split(",");
|
|
||||||
results = [];
|
|
||||||
for (j = 0, len1 = ref1.length; j < len1; j++) {
|
|
||||||
v = ref1[j];
|
|
||||||
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 = 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 = 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();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
})(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];
|
|
||||||
}
|
|
||||||
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() {
|
|
||||||
var args, out;
|
|
||||||
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
|
|
||||||
out = this._parseConnect.apply(this, args);
|
|
||||||
this.headers = out[0], this.connectCallback = out[1], this.errorCallback = out[2];
|
|
||||||
return this._connect();
|
|
||||||
};
|
|
||||||
|
|
||||||
Client.prototype._connect = function() {
|
|
||||||
var errorCallback, headers;
|
|
||||||
headers = this.headers;
|
|
||||||
errorCallback = this.errorCallback;
|
|
||||||
if (typeof this.debug === "function") {
|
|
||||||
this.debug("Opening Web Socket...");
|
|
||||||
}
|
|
||||||
this.ws = this.ws_fn();
|
|
||||||
this.ws.onmessage = (function(_this) {
|
|
||||||
return function(evt) {
|
|
||||||
var arr, c, client, data, frame, j, len1, messageID, onreceive, ref, results, subscription, unmarshalledData;
|
|
||||||
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 j, len1, results;
|
|
||||||
results = [];
|
|
||||||
for (j = 0, len1 = arr.length; j < len1; j++) {
|
|
||||||
c = arr[j];
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
unmarshalledData = Frame.unmarshall(_this.partialData + data);
|
|
||||||
_this.partialData = unmarshalledData.partial;
|
|
||||||
ref = unmarshalledData.frames;
|
|
||||||
results = [];
|
|
||||||
for (j = 0, len1 = ref.length; j < len1; j++) {
|
|
||||||
frame = ref[j];
|
|
||||||
switch (frame.command) {
|
|
||||||
case "CONNECTED":
|
|
||||||
if (typeof _this.debug === "function") {
|
|
||||||
_this.debug("connected to server " + frame.headers.server);
|
|
||||||
}
|
|
||||||
_this.connected = true;
|
|
||||||
_this.version = frame.headers.version;
|
|
||||||
_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;
|
|
||||||
if (_this.version === Stomp.VERSIONS.V1_2) {
|
|
||||||
messageID = frame.headers["ack"];
|
|
||||||
} else {
|
|
||||||
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":
|
|
||||||
if (frame.headers["receipt-id"] === _this.closeReceipt) {
|
|
||||||
_this.ws.onclose = null;
|
|
||||||
_this.ws.close();
|
|
||||||
results.push(_this._cleanUp());
|
|
||||||
} else {
|
|
||||||
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);
|
|
||||||
this.ws.onclose = (function(_this) {
|
|
||||||
return function() {
|
|
||||||
var msg;
|
|
||||||
msg = "Whoops! Lost connection to " + _this.ws.url;
|
|
||||||
if (typeof _this.debug === "function") {
|
|
||||||
_this.debug(msg);
|
|
||||||
}
|
|
||||||
_this._cleanUp();
|
|
||||||
if (typeof errorCallback === "function") {
|
|
||||||
errorCallback(msg);
|
|
||||||
}
|
|
||||||
return _this._schedule_reconnect();
|
|
||||||
};
|
|
||||||
})(this);
|
|
||||||
return this.ws.onopen = (function(_this) {
|
|
||||||
return function() {
|
|
||||||
if (typeof _this.debug === "function") {
|
|
||||||
_this.debug('Web Socket Opened...');
|
|
||||||
}
|
|
||||||
headers["accept-version"] = Stomp.VERSIONS.supportedVersions();
|
|
||||||
headers["heart-beat"] = [_this.heartbeat.outgoing, _this.heartbeat.incoming].join(',');
|
|
||||||
return _this._transmit("CONNECT", headers);
|
|
||||||
};
|
|
||||||
})(this);
|
|
||||||
};
|
|
||||||
|
|
||||||
Client.prototype._schedule_reconnect = function() {
|
|
||||||
if (this.reconnect_delay > 0) {
|
|
||||||
this.debug("STOMP: scheduling reconnection in " + this.reconnect_delay + "ms");
|
|
||||||
return setTimeout((function(_this) {
|
|
||||||
return function() {
|
|
||||||
if (_this.connected) {
|
|
||||||
return typeof _this.debug === "function" ? _this.debug('STOMP: already connected') : void 0;
|
|
||||||
} else {
|
|
||||||
if (typeof _this.debug === "function") {
|
|
||||||
_this.debug('STOMP: attempting to reconnect');
|
|
||||||
}
|
|
||||||
return _this._connect();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
})(this), this.reconnect_delay);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Client.prototype.disconnect = function(disconnectCallback, headers) {
|
|
||||||
if (headers == null) {
|
|
||||||
headers = {};
|
|
||||||
}
|
|
||||||
if (!headers.receipt) {
|
|
||||||
headers.receipt = "close-" + this.counter++;
|
|
||||||
}
|
|
||||||
this.closeReceipt = headers.receipt;
|
|
||||||
this._transmit("DISCONNECT", headers);
|
|
||||||
return typeof disconnectCallback === "function" ? disconnectCallback() : void 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
Client.prototype._cleanUp = function() {
|
|
||||||
this.connected = false;
|
|
||||||
this.subscriptions = {};
|
|
||||||
this.partial = '';
|
|
||||||
if (this.pinger) {
|
|
||||||
Stomp.clearInterval(this.pinger);
|
|
||||||
}
|
|
||||||
if (this.ponger) {
|
|
||||||
return Stomp.clearInterval(this.ponger);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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 client;
|
|
||||||
if (headers == null) {
|
|
||||||
headers = {};
|
|
||||||
}
|
|
||||||
if (!headers.id) {
|
|
||||||
headers.id = "sub-" + this.counter++;
|
|
||||||
}
|
|
||||||
headers.destination = destination;
|
|
||||||
this.subscriptions[headers.id] = callback;
|
|
||||||
this._transmit("SUBSCRIBE", headers);
|
|
||||||
client = this;
|
|
||||||
return {
|
|
||||||
id: headers.id,
|
|
||||||
unsubscribe: function(hdrs) {
|
|
||||||
return client.unsubscribe(headers.id, hdrs);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
Client.prototype.unsubscribe = function(id, headers) {
|
|
||||||
if (headers == null) {
|
|
||||||
headers = {};
|
|
||||||
}
|
|
||||||
delete this.subscriptions[id];
|
|
||||||
headers.id = id;
|
|
||||||
return this._transmit("UNSUBSCRIBE", headers);
|
|
||||||
};
|
|
||||||
|
|
||||||
Client.prototype.begin = function(transaction_id) {
|
|
||||||
var client, txid;
|
|
||||||
txid = transaction_id || "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_id) {
|
|
||||||
return this._transmit("COMMIT", {
|
|
||||||
transaction: transaction_id
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Client.prototype.abort = function(transaction_id) {
|
|
||||||
return this._transmit("ABORT", {
|
|
||||||
transaction: transaction_id
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Client.prototype.ack = function(messageID, subscription, headers) {
|
|
||||||
if (headers == null) {
|
|
||||||
headers = {};
|
|
||||||
}
|
|
||||||
if (this.version === Stomp.VERSIONS.V1_2) {
|
|
||||||
headers["id"] = messageID;
|
|
||||||
} else {
|
|
||||||
headers["message-id"] = messageID;
|
|
||||||
}
|
|
||||||
headers.subscription = subscription;
|
|
||||||
return this._transmit("ACK", headers);
|
|
||||||
};
|
|
||||||
|
|
||||||
Client.prototype.nack = function(messageID, subscription, headers) {
|
|
||||||
if (headers == null) {
|
|
||||||
headers = {};
|
|
||||||
}
|
|
||||||
if (this.version === Stomp.VERSIONS.V1_2) {
|
|
||||||
headers["id"] = messageID;
|
|
||||||
} else {
|
|
||||||
headers["message-id"] = messageID;
|
|
||||||
}
|
|
||||||
headers.subscription = subscription;
|
|
||||||
return this._transmit("NACK", headers);
|
|
||||||
};
|
|
||||||
|
|
||||||
return Client;
|
|
||||||
|
|
||||||
})();
|
|
||||||
|
|
||||||
Stomp = {
|
|
||||||
VERSIONS: {
|
|
||||||
V1_0: '1.0',
|
|
||||||
V1_1: '1.1',
|
|
||||||
V1_2: '1.2',
|
|
||||||
supportedVersions: function() {
|
|
||||||
return '1.2,1.1,1.0';
|
|
||||||
}
|
|
||||||
},
|
|
||||||
client: function(url, protocols) {
|
|
||||||
var ws_fn;
|
|
||||||
if (protocols == null) {
|
|
||||||
protocols = ['v10.stomp', 'v11.stomp', 'v12.stomp'];
|
|
||||||
}
|
|
||||||
ws_fn = function() {
|
|
||||||
var klass;
|
|
||||||
klass = Stomp.WebSocketClass || WebSocket;
|
|
||||||
return new klass(url, protocols);
|
|
||||||
};
|
|
||||||
return new Client(ws_fn);
|
|
||||||
},
|
|
||||||
over: function(ws) {
|
|
||||||
var ws_fn;
|
|
||||||
ws_fn = typeof ws === "function" ? ws : function() {
|
|
||||||
return ws;
|
|
||||||
};
|
|
||||||
return new Client(ws_fn);
|
|
||||||
},
|
|
||||||
Frame: Frame
|
|
||||||
};
|
|
||||||
|
|
||||||
Stomp.setInterval = function(interval, f) {
|
|
||||||
return setInterval(f, interval);
|
|
||||||
};
|
|
||||||
|
|
||||||
Stomp.clearInterval = function(id) {
|
|
||||||
return clearInterval(id);
|
|
||||||
};
|
|
||||||
|
|
||||||
if (typeof exports !== "undefined" && exports !== null) {
|
|
||||||
exports.Stomp = Stomp;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof window !== "undefined" && window !== null) {
|
|
||||||
window.Stomp = Stomp;
|
|
||||||
} else if (!exports) {
|
|
||||||
self.Stomp = Stomp;
|
|
||||||
}
|
|
||||||
|
|
||||||
}).call(this);
|
|
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user