2014-06-22 12:04:07 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('app').service('feed', ['$http', '$window', function($http, $window) {
|
|
|
|
|
2014-10-15 18:46:37 +03:00
|
|
|
var token = localStorage.getItem('access_token');
|
2014-06-22 12:04:07 +03:00
|
|
|
var proto = ($window.location.protocol == 'https:' ? 'wss' : 'ws');
|
2014-10-15 18:46:37 +03:00
|
|
|
var route = [proto, "://", $window.location.host, '/api/stream/user?access_token=', token].join('');
|
2014-06-22 12:04:07 +03:00
|
|
|
|
|
|
|
var wsCallback = undefined;
|
|
|
|
var ws = new WebSocket(route);
|
|
|
|
ws.onmessage = function(event) {
|
|
|
|
var data = angular.fromJson(event.data);
|
|
|
|
if (wsCallback != undefined) {
|
|
|
|
wsCallback(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.subscribe = function(callback) {
|
|
|
|
wsCallback = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.unsubscribe = function() {
|
2014-07-12 01:29:31 +03:00
|
|
|
wsCallback = undefined;
|
2014-06-22 12:04:07 +03:00
|
|
|
};
|
|
|
|
}]);
|