0

I have some problem with socket factory in AngularJS. I want to create my personal factory for work with sockets by STOMP. My code:

angular.module('SocketApp',['SocketApp.controllers'])
.factory('socketFactory', function($rootScope) {
    return {
        getSocket: function(socketUrl,sendUrl,resultUrl,handler){
            var socket={};
            var socketJS=new SockJS(socketUrl); 
            var stompClient = Stomp.over(socketJS); 

               socket.connect = function(){
                   stompClient.connect({}, function(frame) {
                         stompClient.subscribe(resultUrl, function(calResult){
                            handler(calResult);
                         });
                   });
                };

            socket.disconnect = function(){
                if (stompClient) {
                    stompClient.disconnect();
                }
            };

            socket.send= function(object) {       
                stompClient.send(sendUrl, {}, object);
            }

            return socket;
        }
    };
});

I have problem with connection. I do connect, in JS console I get message “Opening Web Socket...” and that’s all. If I’m working inside controller, all works great. Where can be a problem?

UPD My controller:

angular.module('SocketApp.controllers', [])
.controller('socketCtrl',['$scope','socketFactory',function($scope, socketFactory) {
    $scope.result="";
    $scope.isConnect=false;

    var handler =  function(calResult){
        $scope.result=JSON.parse(calResult.body).result;
        $scope.$apply();
    };

    var socket = socketFactory.getSocket('/getFirstMessage','/socketApp/getFirstMessage', '/topic/msgFromServer', handler);

    $scope.connect = function(){
        socket.connect();
        $scope.isConnect=true;
    };

    $scope.disconnect = function(){
        socket.disconnect();
        $scope.isConnect=false;
        $scope.result="";
    }

    $scope.send= function() {
        socket.send();
    }
}]);
Nata
  • 41
  • 2
  • 8

0 Answers0