信号器持久连接查询参数。 [英] Signalr persistent connection with query params.
问题描述
我有一个持久的连接,我想从查询参数的一些种子信息开始。这是连接中的覆盖。
I have a persistent connection which I would like to start with some seed info using query params. Here is the override in the connection.
protected override Task OnConnected(IRequest request, string connectionId)
{
//GET QUERY PARAMS HERE
return base.OnConnected(request, connectionId);
}
现在我在global.asax文件中设置了我的路由,如下所示。
Now I have my route setup in global.asax file which looks like this.
RouteTable.Routes.MapConnection(myconnection,
/ myconnection);
RouteTable.Routes.MapConnection("myconnection", "/myconnection");
客户端代码如下所示
var connection = $.connection('/myconnection');
connection.start()
.done(() =>
{
});
有人能告诉我如何将查询字符串参数传递给此连接,以便我可以在覆盖,因为我似乎在这上面打砖墙。
Can someone tell me how I can pass query string params to this connecton so I can read them in the override as I seem to be hitting a brick wall on this.
干杯希望有人可以提供帮助,
Cheers hope someone can help,
戴夫
推荐答案
HUBS
var connection = $.connection('/myconnection');
$.connection.hub.qs = "name=John"; //pass your query string
并在服务器上获取它
var myQS = Context.QueryString["name"];
要在javascript中访问您的查询字符串,您可以使用类似
To access your query string in javascript you could use something like
function getQueryStringValueByKey(key) {
var url = window.location.href;
var values = url.split(/[\?&]+/);
for (i = 0; i < values.length; i++) {
var value = values[i].split("=");
if (value[0] == key) {
return value[1];
}
}
}
称之为:
var name = getQueryStringValueByKey("name");
PERSISTENT CONNECTION
//pass your query string
var connection = $.connection('/myconnection', "name=John", true);
protected override Task OnConnected(IRequest request, string connectionId)
{
//get the name here
var name = request.QueryString["name"];
return base.OnConnected(request, connectionId);
}
以下是您可以找到更多内容的源代码: https:// github .com / SignalR / SignalR / blob / master / src /Microsoft.AspNet.SignalR.Client.JS / jquery.signalR.core.js#L106
Here is the source code where you can find out more: https://github.com/SignalR/SignalR/blob/master/src/Microsoft.AspNet.SignalR.Client.JS/jquery.signalR.core.js#L106
这篇关于信号器持久连接查询参数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!