Files
weave-scope/client/app/scripts/utils/web-api-utils.js
David Kaltschmidt 7d1ee40a2b Fixed lint errors in all js files
- Also added linter configuration, and make linter fail on error
- fixing ES6 errors and added ES6 transformer
- gulp target to try local build
- linted gulpfile
- cant hook into gulp lint yet, because gulp does currently not support
  ES6 which some rules demand, since gulp cant transpile itself, we have a
  chicken and egg problem.
- ES6 transpiler for test runner
- removed old linter config
- adapted editorconfig to reflect linter config
2015-05-28 15:07:13 +00:00

70 lines
1.5 KiB
JavaScript

const reqwest = require('reqwest');
const AppActions = require('../actions/app-actions');
const WS_URL = window.WS_URL || 'ws://' + location.host;
let socket;
let reconnectTimer = 0;
let currentUrl = null;
let updateFrequency = '5s';
let topologyTimer = 0;
function createWebsocket(topologyUrl) {
if (socket) {
socket.onclose = null;
socket.close();
}
socket = new WebSocket(WS_URL + topologyUrl + '/ws?t=' + updateFrequency);
socket.onclose = function() {
clearTimeout(reconnectTimer);
socket = null;
reconnectTimer = setTimeout(function() {
createWebsocket(topologyUrl);
}, 5000);
};
socket.onmessage = function(event) {
const msg = JSON.parse(event.data);
if (msg.add || msg.remove || msg.update) {
AppActions.receiveNodesDelta(msg);
}
};
currentUrl = topologyUrl;
}
function getTopologies() {
clearTimeout(topologyTimer);
reqwest('/api/topology', function(res) {
AppActions.receiveTopologies(res);
topologyTimer = setTimeout(getTopologies, 10000);
});
}
function getNodeDetails(topologyUrl, nodeId) {
if (topologyUrl && nodeId) {
const url = [topologyUrl, nodeId].join('/');
reqwest(url, function(res) {
AppActions.receiveNodeDetails(res.node);
});
}
}
module.exports = {
getNodeDetails: getNodeDetails,
getTopologies: getTopologies,
getNodesDelta: function(topologyUrl) {
if (topologyUrl && topologyUrl !== currentUrl) {
createWebsocket(topologyUrl);
}
}
};