1

I'm trying to create small web application which uses WebSocket protocol to exchange the data

Here is my angular service

define(function () {

    return function (module) {
        return module.service("socketService", function($q, $timeout) {

            var service = {},
                listener = $q.defer(),
                socket = {
                    client: null,
                    stomp: null
                };

            service.RECONNECT_TIMEOUT = 30000;
            service.SOCKET_URL = "/service/booking";
            service.CHAT_TOPIC = "/service/booking/updates";
            service.CHAT_BROKER = "/service/booking";

            service.receive = function() {
                return listener.promise;
            };

            ...
            return service;
       });
   };
});

and also controller code

define(function () {
    return function (module) {
            socketService.receive().then(null, null, function(message) { 
                $scope.list.countDownTimer = 0;
                if(message.resourceId === parseInt($routeParams.resourceId)) {
                    $scope.list.bookings = [];
                }   
                ...     
            });

My problem is that promise.then() callback function is invoked many times and the amount of this invocations is not repeatable: it might be 2 times, 27 or even 110.

One more thing that I should add is that I think that is somehow dependent with amount of users who tests and clicks on the application

Could anyone help? Thank you in advance...

gokareless
  • 1,165
  • 1
  • 10
  • 26
  • Use events $on and $emit/$broadcast, promises only work with one return element at a time – Jesús Quintana Jun 15 '15 at 09:54
  • @JesúsQuintana could you please explain how it related to the multiple *promise.then()* callback function invocations? – gokareless Jun 15 '15 at 14:33
  • the .then callback being called multiple times doesn't make much sense, unless your binding to it multiple times. my guess would be you controller is being invoked multiple times, and when it is no longer needed the callbacks aren't cleaned up, so they still trigger. – Kevin B Jun 15 '15 at 14:52

0 Answers0