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
1 Answer
Reset to default 2The 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
版权声明:本文标题:javascript - How to set connection id in SignalR client? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745476285a2659979.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论