harness-drone/server/app/scripts/services/stdout.js

32 lines
802 B
JavaScript
Raw Normal View History

'use strict';
angular.module('app').service('stdout', ['$window', function($window) {
var callback = undefined;
var websocket = undefined;
this.subscribe = function(path, _callback) {
callback = _callback;
var proto = ($window.location.protocol == 'https:' ? 'wss' : 'ws');
2014-10-12 19:07:34 +00:00
var route = [proto, "://", $window.location.host, '/api/stream/stdout/', path].join('');
websocket = new WebSocket(route);
websocket.onmessage = function(event) {
if (callback != undefined) {
callback(event.data);
}
};
websocket.onclose = function(event) {
console.log('websocket closed at '+path);
};
};
this.unsubscribe = function() {
callback = undefined;
if (websocket != undefined) {
2014-07-12 02:10:18 +00:00
console.log('unsubscribing websocket at '+websocket.url);
websocket.close();
}
};
}]);