Skip to content Skip to sidebar Skip to footer

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(

Solution 1:

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

functiongetQueryStringValueByKey(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 stringvar connection = $.connection('/myconnection', "name=John", true);

protectedoverride Task OnConnected(IRequest request, string connectionId)
    {
        //get the name herevar name = request.QueryString["name"];

        returnbase.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

Post a Comment for "Signalr Persistent Connection With Query Params."