2017-05-31 62 views
0

对于我的生活,我无法弄清楚我在这里做错了什么......我正在为多用户门户构建一个简单的通知系统,并试图使用Signal R来完成推送通知。Signal R Querystring不通过

我能够建立一个连接并将通知推送给所有用户,但要定位特定用户以显示通知,以便我需要跟踪用户ID并将其映射到服务器上的connectionId。为了实现这一点,我将一个加密的用户ID字符串传递给服务器,并存储将用户ID存储到创建的Connection ID的对象列表。但是,当试图将加密的用户ID作为查询字符串传递时,它不会传递它。任何想法,我在这里搞砸了吗?

的Javascript

/////Connect to NotificationHub 

var nHub = $.connection.notificationHub; 
$.connection.notificationHub.qs = { "userId": "1A3BCF" }; 

////Register Add Notification Method 
nHub.client.showNotification = function (message, icon, url) { 
    console.log("Notification Received!"); 
    console.log("Message: " + message); 
    console.log("Icon: " + icon); 
    console.log("URL: " + url); 
}; 

$.connection.hub.start() 
.done(function() { 

    console.log("Successful Connection to Notification Hub"); 

}) 
.fail(function() { 
    console.log("Error Connecting to Notification Hub!"); 
}); 

C#集线器

List<NotificationConnection> connectedUsers = new List<NotificationConnection>(); 

    public override Task OnConnected() 
    { 
     var us = new NotificationConnection(); 
     us.userId= Context.QueryString['userId'];; 
     us.connectionId = Context.ConnectionId; 
     connectedUsers.Add(us); 
     return base.OnConnected(); 
    } 

    public void showNotification(NotificationTargetType target, int objectId, string message, string icon, string url) 
    { 
     var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>(); 

     if (target == NotificationTargetType.User) 
     { 
      var user = connectedUsers.Where(o => o.userId== objectId); 
      if (user.Any()) 
      { 
       hubContext.Clients.Client(user.First().connectionId).showNotification(message, icon, url); 
      } 
     } 
     else 
     { 

     } 

    } 

再次一切进展顺利通过,直到我要抢查询字符串,因为它不存在。

+1

尝试在连接上设置qs而不是集线器。 – Pawel

+0

这是一个错字:'Context.QueryString ['userId'];'?它应该是双引号 – CodingYoshi

+1

@Pawel你把我带到了正确的道路上。谢谢。我已经在下面添加了更改 –

回答

0

正如帕维尔提到的,我将qs加入了错误的项目。需要在connection.hub上调用它,而不是实际的集线器。

$.connection.hub.qs = { "userId": "1A3BCF" }; 

谢谢。