【发布时间】:2017-05-31 22:32:54
【问题描述】:
对于我的生活,我无法弄清楚我在这里做错了什么......我正在为多用户门户构建一个简单的通知系统,并尝试使用 Signal R 来完成推送通知。
我能够建立连接并向所有用户推送通知,但要针对特定用户向其显示通知,我需要跟踪用户 ID 并将其映射到服务器上的 connectionId。为了实现这一点,我将用户 ID 的加密字符串传递给服务器,并将存储用户 ID 的对象列表存储到创建的连接 ID。但是,当尝试将加密的 UserID 作为查询字符串传递时,它不会通过它。有什么我在这里搞砸的想法吗?
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
{
}
}
一切都顺利进行,直到我想要获取查询字符串,因为它不存在。
【问题讨论】:
-
尝试在连接而不是集线器上设置 qs。
-
这是错字吗:
Context.QueryString['userId'];?应该是双引号 -
@Pawel 你带领我走上了正确的道路。谢谢你。我在下面添加了更改