C# 信号机-Can';无法读取服务器上的查询字符串
我快疯了。我做了chatHub教程,一切都很好。我连接到服务器。我可以向每个客户广播一条消息 然后,我尝试添加查询字符串,因为我希望在同一个会话中,尽可能多的用户可以连接到我的用户,因此我希望获得一些关于我的用户的信息 这是我的javascriptC# 信号机-Can';无法读取服务器上的查询字符串,c#,asp.net,asp.net-mvc,signalr,C#,Asp.net,Asp.net Mvc,Signalr,我快疯了。我做了chatHub教程,一切都很好。我连接到服务器。我可以向每个客户广播一条消息 然后,我尝试添加查询字符串,因为我希望在同一个会话中,尽可能多的用户可以连接到我的用户,因此我希望获得一些关于我的用户的信息 这是我的javascript (function ($) { // Declare a proxy to reference the hub. var chat = $.connection.chatHub;
(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>: ' + 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为null。如果我查看服务器上的send方法:
[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;
}
queryString
变量中没有uid
我的代码有什么问题?有人有什么线索吗?我刚刚找到了解决办法。这是因为我必须通过
$.connection.hub
传递查询字符串,而不是通过chat.qs
因此,解决办法是
$.connection.hub.qs = { 'uid': '2541fdsf862d' };
如果要从JavaScript在signalR OnConnect中传递参数,请将它们添加到connection.qs属性。e、 g
self.hubConnection = $.hubConnection();
self.hubConnection.qs = { param1: value, param2: value };
然后在信号器集线器中的OnConnect方法中,通过
var param1 = this.Context.QueryString["param1"];
var param2 = this.Context.QueryString["param2"];
var param1 = this.Context.QueryString["param1"];
var param2 = this.Context.QueryString["param2"];