作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在 hubConnection.start(); 之前设置连接 ID我看这里https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client我发现了什么:connection.id - 获取或设置当前连接的客户端 ID。试过这个。不工作。这是客户端代码:
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";这不会改变真实的连接 ID。也许您知道如何编写客户端连接的其他方式?
最佳答案
connectionId 根本无法手动创建或操作,它是在与服务器协商请求期间创建的。您可以阅读here您可以实现自己的 IConnectionIdFactory
但不建议这样做。您最多可以做的是获取 connectionId 并使用它来映射用户和组以及集线器内的其他逻辑。
关于javascript - 如何在SignalR客户端中设置连接ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58453803/
我是一名优秀的程序员,十分优秀!