websocket output working

This commit is contained in:
Brad Rydzewski
2015-04-29 19:57:43 -07:00
parent 7a75a17535
commit ad80facbbd
13 changed files with 150 additions and 36 deletions

View File

@@ -4,7 +4,7 @@
* BuildsCtrl responsible for rendering the repo's
* recent build history.
*/
function BuildsCtrl($scope, $routeParams, builds, repos, users, feed) {
function BuildsCtrl($scope, $routeParams, builds, repos, users, feed, logs) {
var owner = $routeParams.owner;
var name = $routeParams.name;
@@ -83,6 +83,17 @@
var name = $routeParams.name;
var fullName = owner+'/'+name;
// Initiates streaming a build.
var stream = function() {
var convert = new Filter({stream:true,newline:false});
var term = document.getElementById("term");
term.innerHTML = "";
logs.subscribe(fullName, number, step, function(data){
term.innerHTML += convert.toHtml(data)+"\n";
});
}
// Gets the currently authenticated user
users.getCached().then(function(payload){
$scope.user = payload.data;
@@ -104,6 +115,7 @@
// do nothing
} else if ($scope.task.state === 'running') {
// stream the build
stream();
} else {
// fetch the logs for the finished build.
@@ -154,6 +166,11 @@
if (!event.task || event.task.number !== step) {
return; // ignore
}
if (event.task.state === 'running' && $scope.task.state !== 'running') {
stream();
}
// update the task status
$scope.task.state = event.task.state;
$scope.task.started_at = event.task.started_at;
@@ -163,14 +180,7 @@
$scope.$apply();
});
// var convert = new Filter({stream:true,newline:false});
// var term = document.getElementById("term")
// var stdout = document.getElementById("stdout").innerText.split("\n")
// stdout.forEach(function(line, i) {
// setTimeout(function () {
// term.innerHTML += convert.toHtml(line+"\n");
// }, i*i);
// });
}
angular

View File

@@ -119,9 +119,10 @@
}
function RouteChange($rootScope, feed) {
function RouteChange($rootScope, feed, logs) {
$rootScope.$on('$routeChangeStart', function (event, next) {
feed.unsubscribe();
logs.unsubscribe();
});
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {

View File

@@ -3,25 +3,34 @@
(function () {
function FeedService($http, $window) {
var token = localStorage.getItem('access_token');
var proto = ($window.location.protocol == 'https:' ? 'wss' : 'ws');
var route = [proto, "://", $window.location.host, '/api/stream/user?access_token=', token].join('');
var wsCallback = undefined;
var ws = new WebSocket(route);
ws.onmessage = function(event) {
var data = angular.fromJson(event.data);
if (wsCallback != undefined) {
wsCallback(data);
}
};
var callback,
websocket,
token = localStorage.getItem('access_token');
this.subscribe = function(callback) {
wsCallback = callback;
this.subscribe = function(_callback) {
callback = _callback;
var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'),
route = [proto, "://", $window.location.host, '/api/stream/user?access_token=', token].join('');
websocket = new WebSocket(route);
websocket.onmessage = function (event) {
if (callback !== undefined) {
callback(angular.fromJson(event.data));
}
};
websocket.onclose = function (event) {
console.log('user websocket closed');
};
};
this.unsubscribe = function() {
wsCallback = undefined;
callback = undefined;
if (websocket !== undefined) {
websocket.close();
websocket = undefined;
}
};
}

View File

@@ -18,6 +18,35 @@
this.get = function(repoName, number, step) {
return $http.get('/api/repos/'+repoName+'/logs/'+number+'/'+step);
};
var callback,
websocket,
token = localStorage.getItem('access_token');
this.subscribe = function (repoName, number, step, _callback) {
callback = _callback;
var proto = ($window.location.protocol === 'https:' ? 'wss' : 'ws'),
route = [proto, "://", $window.location.host, '/api/stream/logs/', repoName, '/', number, '/', step, '?access_token=', token].join('');
websocket = new WebSocket(route);
websocket.onmessage = function (event) {
if (callback !== undefined) {
callback(event.data);
}
};
websocket.onclose = function (event) {
console.log('logs websocket closed');
};
};
this.unsubscribe = function () {
callback = undefined;
if (websocket !== undefined) {
websocket.close();
websocket = undefined;
}
};
}
angular