2014-10-28 15:13:23 +02:00
|
|
|
/*global angular, WebSocket, localStorage, console */
|
|
|
|
angular.module('app').service('stdout', ['$window', function ($window) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var callback,
|
|
|
|
websocket,
|
|
|
|
token = localStorage.getItem('access_token');
|
2014-06-22 12:04:07 +03:00
|
|
|
|
2014-10-28 15:13:23 +02:00
|
|
|
this.subscribe = function (path, _callback) {
|
2014-06-22 12:04:07 +03:00
|
|
|
callback = _callback;
|
|
|
|
|
2014-10-28 15:13:23 +02:00
|
|
|
var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'),
|
|
|
|
route = [proto, "://", $window.location.host, '/api/stream/stdout/', path, '?access_token=', token].join('');
|
2014-06-22 12:04:07 +03:00
|
|
|
|
|
|
|
websocket = new WebSocket(route);
|
2014-10-28 15:13:23 +02:00
|
|
|
websocket.onmessage = function (event) {
|
|
|
|
if (callback !== undefined) {
|
2014-06-22 12:04:07 +03:00
|
|
|
callback(event.data);
|
|
|
|
}
|
|
|
|
};
|
2014-10-28 15:13:23 +02:00
|
|
|
websocket.onclose = function (event) {
|
|
|
|
console.log('websocket closed at ' + path);
|
2014-07-12 02:52:22 +03:00
|
|
|
};
|
2014-06-22 12:04:07 +03:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:13:23 +02:00
|
|
|
this.unsubscribe = function () {
|
2014-06-22 12:04:07 +03:00
|
|
|
callback = undefined;
|
2014-10-28 15:13:23 +02:00
|
|
|
if (websocket !== undefined) {
|
|
|
|
console.log('unsubscribing websocket at ' + websocket.url);
|
2014-06-22 12:04:07 +03:00
|
|
|
websocket.close();
|
2014-10-28 15:13:23 +02:00
|
|
|
websocket = undefined;
|
2014-06-22 12:04:07 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}]);
|