woodpecker/server/app/scripts/services/stdout.js
Eichin David 50e368c24a refactoring commit controller
fixed bug where include of pr template raised angularjs error
2014-10-28 15:39:14 +01:00

34 lines
939 B
JavaScript

/*global angular, WebSocket, localStorage, console */
angular.module('app').service('stdout', ['$window', function ($window) {
'use strict';
var callback,
websocket,
token = localStorage.getItem('access_token');
this.subscribe = function (path, _callback) {
callback = _callback;
var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'),
route = [proto, "://", $window.location.host, '/api/stream/stdout/', path, '?access_token=', token].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) {
console.log('unsubscribing websocket at ' + websocket.url);
websocket.close();
websocket = undefined;
}
};
}]);