Signalr-无法读取服务器上的查询字符串 [英] Signalr - Can't read query strings on server

查看:94
本文介绍了Signalr-无法读取服务器上的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里疯了.我做了chatHub教程,一切正常.我连接到服务器.我可以向每个客户广播一条消息.

I'm going crazy here. I did the chatHub tutorial and everything works fine. I connect to the server. I can broadcast a message to every client.

然后我尝试添加查询字符串,因为我希望获得有关用户的一些信息,因为许多用户可以在同一会话中进行连接.

Then I tried to add query strings because I would like to have some information on my user as many user can connect in the same session.

这是我的javascript

This is my javascript

(function ($) {
            // Declare a proxy to reference the hub.
            var chat = $.connection.chatHub;
            //chat.state.userName = "test";
            //chat.qs = "userToken3=blablabla";
            chat.qs = { 'uid' : '2541fdsf862d' };
            //$.connection.chatHub.qs = {"userToken2" : 125457};
            // Create a function that the hub can call to broadcast messages.
            chat.client.broadcastMessage = function (name, message) {
                // Html encode display name and message.
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page.
                $('#discussion').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            //$('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start({ 'uid': '2541fdsf862d' }).done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub.
                    chat.server.send("user", $('#message').val());
                    // Clear text box and reset focus for next comment.
                    $('#message').val('').focus();
                });
            });
        })(jQuery);

如果我在此函数中设置断点时查看监视列表,则可以看到查询字符串"uid"设置正确.

If I take a look at my watchlist when I set a breakpoint in this function, I can see that the query string "uid" is perfectly set.

但是在服务器上,我无法读取参数.

However on the server, I can't read the parameter.

public override Task OnConnected()
{
    string name = Context.User.Identity.Name;
    var caller = Clients.Caller;
    var username = Clients.Caller.userName; 
    var queryString = Context.Request.QueryString;
    var queryString2 = Context.QueryString;
    var uid = Context.QueryString["uid"];
    _connections.Add(name, Context.ConnectionId);

    return base.OnConnected();
}

在这种情况下,uid为null.如果我查看服务器上的send方法:

In this situation, uid is null. If I look at the send method on the server:

[Authorize]
public void Send(string name, string message)
{
    // Call the broadcastMessage method to update clients.
    Clients.All.broadcastMessage(name, message);
    var queryString = Context.Request.QueryString;
    var context = Context;
}

uidqueryString变量中不可用.

我的代码有什么问题?有人有任何线索吗?

What is wrong with my code? Does anyone have any clue?

推荐答案

我刚刚找到了解决方案.这是因为我必须通过$.connection.hub而不是通过chat.qs

I've just found the solution. It's because I have to pass the query string via $.connection.hub and not via chat.qs

所以,解决方案是

$.connection.hub.qs = { 'uid': '2541fdsf862d' };

这篇关于Signalr-无法读取服务器上的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆