V
V
Vitalychez2015-11-15 10:32:04
Angular
Vitalychez, 2015-11-15 10:32:04

How to make an asynchronous Websocket in Angular js?

Hello. There is this code:

.run(["$rootScope", "$cacheFactory", "$websocket", function($rootScope, $cacheFactory, $websocket) {
                $rootScope.cache = $cacheFactory('PJ');
                $rootScope.ws = $websocket('ws://localhost');
            }])
            .controller('profileInfo', ['$scope', '$http', '$cookieStore', 'Websocket', '$rootScope', function($scope, $http, $cookieStore, Websocket, $rootScope) {
                var subject = 'user';
                var param = '"user": {"login": "'+ $cookieStore.get('uname') +'"}';
                var method = 'get_info';

                Websocket.get(param, subject, method)
                    .then(function(data) {
                           alert(data);
                    });

                var subject = 'user';
                var param = '"user": {"login": "'+ $cookieStore.get('uname') +'"}';
                var method = 'get_projects';
                $scope.keys = {};

                Websocket.get(param, subject, method)
                    .then(function(data) {
                         alert(data);
                    });

            }])
            .factory('Websocket',['$rootScope', '$q', function($rootScope, $q) {

                function randomId(){
                    var s ='', abd ='abcdefghijklmnopqrstuvwxyz0123456789', aL = abd.length;
                    while(s.length < '40')
                        s += abd[Math.random() * aL|0];
                    return s;
                }

                var get = function(param, subject, method) {
                    var id = randomId();
                    var deferred = $q.defer();

                    $rootScope.ws.send('{"id": "' + id + '", "type": "request", "version": "v.2.0", "method": "' + method + '", "subject": "' + subject + '", "parameters": {' + param + '}}');
                    $rootScope.ws.onMessage(function (message) {
                        deferred.resolve(message.data);
                    });

                    return deferred.promise;
                }

                return {
                    get: get
                };
            }]);

But the result of the first request is loaded into the 2nd request.
That is, the response data from this block:
var subject = 'user';
                var param = '"user": {"login": "'+ $cookieStore.get('uname') +'"}';
                var method = 'get_info';

                Websocket.get(param, subject, method)
                    .then(function(data) {
                         alert(data);
                    });

Fall into this block:
var subject = 'user';
                var param = '"user": {"login": "'+ $cookieStore.get('uname') +'"}';
                var method = 'get_projects';
                $scope.keys = {};

                Websocket.get(param, subject, method)
                    .then(function(data) {
                          alert(data);
                    });

Using Angular Websocket

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kumanin, 2015-11-16
@jackkum

// тут вешается слушатель на все сообщения из сокета, на сервер передается id, 
// если он возвращается нужно его проверить.
$rootScope.ws.onMessage(function (message) {
  deferred.resolve(message.data);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question