2016-11-28 100 views
0

我想在我的應用程序中創建聊天服務。這是我用於signalR角度服務的代碼。我想要做的是不重複我的代碼,我想繼續創建另一個功能,如第一個非工作案例,但我沒有弄清楚如何完成它。有人能告訴我問題在哪裏,或者這兩種類型的回報之間有什麼區別。在這兩種情況下,它的初始化,但只有在第二個從服務器端捕獲調用。我沒有包括集線器,因爲沒有什麼與此問題相關AngularJS與signalR服務不起作用的服務器調用

(function() { 
app.service("SignalRService", ["$rootScope", SignalRService]); 
"use strict"; 
function SignalRService($rootScope) { 
    //this is not working version 
    return { 
     channelHub: null, 
     initialize: function() { 
      //var chatHub = $.connection.channelHub; 
      var connection = $.hubConnection(); 
      this.channelHub = connection.createHubProxy('channelHub'); 
      // Start Hub 
      connection.start().done(function() { }); 
     }, 
     getMsg: function (eventName, callback) { 
      this.channelHub.on(eventName, function() { 
       var args = arguments; 
       $rootScope.$apply(function() { 
        callback.apply(this.channelHub, args); 
       }) 
      }) 
      console.log(eventName); 
     } 
    } 
    //but if i retype it like this its working w/o any problem 
    return {  
     getMsg: function (eventName, callback) { 
      var connection = $.hubConnection(); 
      var channelHub = connection.createHubProxy('channelHub'); 

      channelHub.on(eventName, function() { 
       var args = arguments; 
       $rootScope.$apply(function() { 
        callback.apply(channelHub, args); 
       }); 
      }); 
      connection.start().done(function() { }); 
     } 
    }; 
}})(); 

這裏是我的角度控制器,在那裏我稱之爲SERVIS初始化函數,並根據debuger和控制檯日誌,函數「newChannelMessage初始化並不會引發錯誤

(function() { 
app.controller("ChannelController", ["$scope", "$http", "SignalRService" ]); 
function ChannelController($scope, $http, SignalRService) { 
    "use strict"; 
    //**--------------------signalR--------------------------------**// 
    SignalRService.initialize(); 
    SignalRService.getMsg("newChannelMessage", function (message, username) { 
     $scope.channel.push(message, username); 
    }); 
}})(); 

這裏是我的服務器控制器,在那裏我稱之爲「newChannelMessage」的功能,但它永遠不會到達客戶端此功能並沒有在我的實現方法的第一種情況發生RLY

public class CommentController : BaseApiController 
{ 
    private ApplicationDbContext _context; 
    private CommentRepository _repository; 
    private IHubContext _ctx; 

    public CommentController() 
    { 
     //initialize of db context 
     _context = new ApplicationDbContext(); 
     _repository = new CommentRepository(); 
     _ctx = GlobalHost.ConnectionManager.GetHubContext<ChannelHub>(); 
    } 

    [Route("create")] 
    [HttpPost] 
    public async Task SendMsg([FromBody] JObject data) 
    { 
     var channelId = data["idData"].ToObject<int>(); 
     Comment comment = data["channelMsgData"].ToObject<Comment>(); 
     comment.UserId = await GetIdUsingUsername(comment.UserName);   
     await _repository.StoreChannelMessage(comment, channelId); 
     _ctx.Clients.All.newChannelMessage(comment.Message, comment.UserName); 
    } 

    public async Task<string> GetIdUsingUsername(string username) 
    { 
     var user = await _context.Users.FirstOrDefaultAsync(x => x.UserName == username); 
     var id = user.Id; 
     return id; 
    } 
} 

回答

0

請試試

function SignalRService($rootScope) { 

      var connection = null; 
      var channelHub = null; 

      var initialize = function() { 
       connection = $.hubConnection(); 
       channelHub = connection.createHubProxy('channelHub'); 
       connection.start().done(function() { 
        console.log("Hub started"); 
       }); 
      } 

      var getMsg = function (eventName, callback) { 
       var deferred = $q.defer(); 
       channelHub.on(eventName, function() { 
        var args = arguments; 
        $rootScope.$apply(function() { 
         callback.apply(channelHub , args); 
        }); 
       }); 
      } 


      return { 
       initialize: initialize, 
       getAllUsers: getAllUsers 
      } 
}