2017-01-02 101 views
0

我有角的服務在那裏我得到它正在從服務器調用時用戶從我的應用程序連接或斷開方法角客戶端signalR服務不火控制方法

(function() { 
//'use strict'; 
app.service('PrivateChatService', ['$rootScope', '$location', function PrivateChatService($rootScope, $location){ 
    var online_users = []; 
    var proxy = $.connection.chatHub; 

    return { 
     addOnlineUser: 
      proxy.client.newOnlineUser = function (user) { 
        var newUser = ({ 
         connectionId: user.ConnectionId, 
         UserName: user.UserName 
        }); 
        online_users.push(newUser); 
        $.connection.hub.start() 
      }, 

      removeOfflineUser: proxy.client.onUserDisconnected = function (id, user) { 
       var index = 0; 
       //find out index of user 
       angular.forEach(online_users, function (value, key) { 
        if (value.connectionId == id) { 
         index = key; 
        } 
       }) 
       online_users.splice(index, 1); 
       $.connection.hub.start() 
      }, 

     } 
}])})(); 

在這裏,我得到了我想要成爲控制器的方法當服務器調用newOnlineUser

PrivateChatService.newOnlineUser(function (user) { 
     $scope.online_users.push(newUser); 
     console.log("newOnlineUser finished"); 
    }); 

所以我的問題是。是否有可能使用生成的代理或我不得不使用非生成的代理訪問那些我不太熟悉的方法。

隨着生成的代理,因爲我看到前面它從來沒有達到我的控制器方法來更新我的數據在控制範圍

回答

0

因爲沒有人回答,我感到莫名其妙奇怪。我發現這是行得通的。我不確定這是否是好的方法,因爲沒有人回答,我也沒有找到任何信息,應該如何解決。

app.service('PrivateChatService', ['$rootScope', '$location', function PrivateChatService($rootScope, $location){ 
    var online_users = [];   
    var connection = $.hubConnection(); 
    var proxy = connection.createHubProxy('chatHub'); 

    function signalrCall(eventName, callback) { 
     proxy.on(eventName, function (user) { 
      var args = arguments; 
      $rootScope.$apply(function() { 
       callback.apply(proxy, args) 
      }) 
     }); 
     connection.start(); 
    } 
    return { 
     addOnlineUser: function (eventName, callback) { 
      signalrCall(eventName, callback);    
     }, 

     getActiveUsers: function (eventName, callback) { 
      signalrCall(eventName, callback); 
     }, 

     removeOfflineUser: function (eventName, callback) { 
      signalrCall(eventName, callback); 
     } 
    } 
}]) 

角控制器方法

PrivateChatService.addOnlineUser("newOnlineUser", function (user) { 
     var newUser = ({ 
      connectionId: user.ConnectionId, 
      UserName: user.UserName 
     }); 

     $scope.online_users.push(newUser); 
     console.log("newOnlineUser finished"); 
    }); 

    PrivateChatService.getActiveUsers("getOnlineUsers", function (onlineUsers) {  
     angular.forEach($scope.online_users, function (scopeValue, scopeKey) { 
      //loop through received list of online users from server 
      angular.forEach(onlineUsers, function (serverListValue, serverListKey) { 
       if (!(serverListValue.ConnectionId == scopeValue.connectionId)) { 
        var newUser = ({ 
         connectionId: serverListValue.ConnectionId, 
         UserName: serverListValue.UserName 
        }); 
        $scope.online_users.push(newUser); 
       } 
      }) 
     }) 
     console.log("getOnlineUsers finished"); 
    }); 

    PrivateChatService.removeOfflineUser("onUserDisconnected", function (user) { 
      var index = 0; 
      //find out index of user 
      angular.forEach($scope.online_users, function (value, key) { 
       if (value.connectionId == user) { 
        index = key; 
       } 
      }) 
      $scope.online_users.splice(index, 1); 
    }); 
相關問題