AngularJS。 STOMP在工厂

时间:2015-06-01 15:11:50

标签: angularjs sockets stomp sockjs

我对AngularJS中的套接字工厂有一些问题。我想创建我的个人工厂,以便STOMP使用插座。 我的代码:

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;
        }
    };
});

我的连接有问题。我连接,在JS控制台中我得到消息“打开Web Socket ......”,就是这样。如果我在控制器内工作,一切都很棒。哪里可能有问题?

UPD 我的控制器:

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();
    }
}]);

0 个答案:

没有答案