javascript 带有查询参数的 Signalr 持久连接。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15585100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Signalr persistent connection with query params.
提问by CompareTheMooCat
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);
}
Now I have my route setup in global.asax file which looks like this.
现在我在 global.asax 文件中设置了我的路线,看起来像这样。
RouteTable.Routes.MapConnection("myconnection", "/myconnection");
RouteTable.Routes.MapConnection("myconnection", "/myconnection");
And the client code looks like this
客户端代码看起来像这样
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,
干杯希望有人可以帮助,
Dave
戴夫
回答by Matija Grcic
HUBS
集线器
var connection = $.connection('/myconnection');
$.connection.hub.qs = "name=John"; //pass your query string
and to get it on the server
并在服务器上获取它
var myQS = Context.QueryString["name"];
To access your query string in javascript you could use something like
要在 javascript 中访问您的查询字符串,您可以使用类似
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];
}
}
}
Call it:
称它为:
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);
}
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
这是您可以找到更多信息的源代码:https: //github.com/SignalR/SignalR/blob/master/src/Microsoft.AspNet.SignalR.Client.JS/jquery.signalR.core.js#L106