Skip to content

Instantly share code, notes, and snippets.

@pdeva
Created June 19, 2014 07:49
Show Gist options
  • Save pdeva/55df7c60fb35d9c4a422 to your computer and use it in GitHub Desktop.
Save pdeva/55df7c60fb35d9c4a422 to your computer and use it in GitHub Desktop.
var GameSocket = (function () {
function GameSocket() {
this.wsInited = false;
}
GameSocket.onDataReceive = function (msg) {
var data = JSON.parse(msg.body);
var maxHeap = data.m;
var user = data.d.u;
var usedHeap = data.d.h;
if (globalStats.totalBytes != maxHeap) {
globalStats.totalBytes = maxHeap;
ProgressBars.updateGlobalBar();
UIManager.updateGlobalStat();
}
var addition = usedHeap - globalStats.usedBytes;
globalStats.usedBytes = usedHeap;
if (gameSockets.wsInited) {
//update ui if needed
var others = true;
if (networkUser != null && networkUser.userName === user)
others = false;
if (others) {
dripper.updateUI(addition, true, user);
}
} else {
gameSockets.wsInited = true;
ProgressBars.updateGlobalBar();
UIManager.updateGlobalStat();
}
};
GameSocket.init = function () {
gameSockets.wsInited = false;
var socket = new SockJS(WS_SVR + 'wsgame');
var stompClient = Stomp.over(socket);
stompClient.debug = function () {
}; //do nothing
stompClient.connect({}, function (frame) {
stompClient.subscribe('/app/globaldata', function (msg) {
GameSocket.onDataReceive(msg);
});
stompClient.subscribe('/topic/globaldata', function (msg) {
GameSocket.onDataReceive(msg);
});
stompClient.subscribe('/topic/mine', function (msg) {
mine.onUpdate(msg);
});
stompClient.subscribe('/app/mine', function (msg) {
mine.onUpdate(msg);
});
}, function () {
console.log("reconnecting websocket in 5s...");
setTimeout(GameSocket.init, 5000);
});
};
return GameSocket;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment