2015-04-25 00:06:46 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
|
|
|
|
function FeedService($http, $window) {
|
|
|
|
|
2015-04-30 02:57:43 +00:00
|
|
|
var callback,
|
|
|
|
websocket,
|
|
|
|
token = localStorage.getItem('access_token');
|
|
|
|
|
|
|
|
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');
|
|
|
|
};
|
2015-04-25 00:06:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.unsubscribe = function() {
|
2015-04-30 02:57:43 +00:00
|
|
|
callback = undefined;
|
|
|
|
if (websocket !== undefined) {
|
|
|
|
websocket.close();
|
|
|
|
websocket = undefined;
|
|
|
|
}
|
2015-04-25 00:06:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
angular
|
|
|
|
.module('drone')
|
|
|
|
.service('feed', FeedService);
|
|
|
|
})();
|