admin管理员组

文章数量:1426795

I need to set connection id before hubConnection.start(); I look here and what i found: connection.id - Gets or sets the client id for the current connection. Tried this. No working. Here is the client code:

let hubUrl = 'http://localhost:5000/chat';
let hubConnection = new signalR.HubConnectionBuilder()
        .withUrl(hubUrl)
    .configureLogging(signalR.LogLevel.Information)
    .build();
hubConnection.id = "12345";
hubConnection.on("Send", function (data) {
    let elem = document.createElement("p");
    elem.appendChild(document.createTextNode(data));
    let firstElem = document.getElementById("chatroom").firstChild;
    document.getElementById("chatroom").insertBefore(elem, firstElem);

});

document.getElementById("sendBtn").addEventListener("click", function (e) {
    let message = document.getElementById("message").value;
    hubConnection.invoke("Send", message);
});

hubConnection.start();

hubConnection.id = "12345"; this do not change real connection id. And maybe you are know some other way how to write client-side connection?

I need to set connection id before hubConnection.start(); I look here https://github./SignalR/SignalR/wiki/SignalR-JS-Client and what i found: connection.id - Gets or sets the client id for the current connection. Tried this. No working. Here is the client code:

let hubUrl = 'http://localhost:5000/chat';
let hubConnection = new signalR.HubConnectionBuilder()
        .withUrl(hubUrl)
    .configureLogging(signalR.LogLevel.Information)
    .build();
hubConnection.id = "12345";
hubConnection.on("Send", function (data) {
    let elem = document.createElement("p");
    elem.appendChild(document.createTextNode(data));
    let firstElem = document.getElementById("chatroom").firstChild;
    document.getElementById("chatroom").insertBefore(elem, firstElem);

});

document.getElementById("sendBtn").addEventListener("click", function (e) {
    let message = document.getElementById("message").value;
    hubConnection.invoke("Send", message);
});

hubConnection.start();

hubConnection.id = "12345"; this do not change real connection id. And maybe you are know some other way how to write client-side connection?

Share Improve this question asked Oct 18, 2019 at 15:21 enkshleteenkshlete 831 gold badge1 silver badge8 bronze badges 7
  • Why do you need to set the connection ID? Why can't you just use what the system assigns? – mason Commented Oct 18, 2019 at 15:22
  • i want to connection.id == ApplicationUser.Id in identityFramework – enkshlete Commented Oct 18, 2019 at 15:25
  • Why do you want to do that? What if the same user logs in from multiple tabs/windows/browsers at the same time? That's just going to be a nightmare to maintain. Instead, associate the connection with a specific user on the server side. – mason Commented Oct 18, 2019 at 15:26
  • What if the same user logs in from multiple tabs/windows/browsers? id will be the same everywhere. its just another value nothing more.... oO – enkshlete Commented Oct 18, 2019 at 15:29
  • 3 A connection ID is supposed to be a unique value to individually identify a single connection. If you start having multiple connections with the same ID, things will break. You'll need to associate connections with specific users a different way. – mason Commented Oct 18, 2019 at 17:14
 |  Show 2 more ments

1 Answer 1

Reset to default 2

The connectionId can't be manually created or manipulated at all and it is created during negotiation request with the server.
You can read here that you can implement your own IConnectionIdFactory but it is not remended to do that.
Maximum what you can do is get the connectionId and use it for mapping users and groups and other logic inside your hub.

本文标签: javascriptHow to set connection id in SignalR clientStack Overflow