2017-01-02 3 views
0

난 내가 사용자가 내 응용 프로그램에서 연결하거나 분리 할 때 서버에서 호출하는 방법을 가지고 각 서비스를각도 클라이언트

(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); 
    }); 
관련 문제