【问题标题】:Signal R Querystring not passingSignalr 查询字符串未通过
【发布时间】: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 你带领我走上了正确的道路。谢谢你。我在下面添加了更改

标签: c# signalr


【解决方案1】:

正如 Pawel 所说,我在错误的项目中添加了 qs。需要在 connection.hub 而不是实际的集线器上调用它。

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

谢谢。

【讨论】:

  • 您的回答帮助我理解了如何通过查询字符串发送第二个参数,传统的 {hub.qs="key=value"} 让我混淆了传递第二个查询字符串参数和这个 json 格式实际上帮助。谢谢@Seth Duncan。干杯
猜你喜欢
  • 2016-04-06
  • 1970-01-01
  • 2020-02-14
  • 2014-10-17
  • 2014-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多