我有一个运行hub类的C#服务器,其中只包含一个方法,如下所示,
public class HothHub : Hub
{
public async Task AddSingleUserGroup(string name)
{
await Groups.AddToGroupAsync(Context.ConnectionId, name);
}
}
我还有一个JavaScript客户端,它通过以下代码连接到集线器,
var connection;
async function signalRStart() {
connection = new signalR.HubConnectionBuilder()
.withUrl("https://somesignalrurl.com/hothhub", { withCredentials: false })
.withAutomaticReconnect()
.configureLogging(signalR.LogLevel.Information)
.build();
connection.on("hothHubToHothUpdate", () => {
console.log("Called by the server!");
});
connection.onreconnecting(error => {
console.log("Connection lost due to error " + error + ". Reconnecting.");
});
// Start the connection.
await start();
}
async function start() {
try {
await connection.start();
connection.invoke("addSingleUserGroup", "someUniqueUserName");
} catch (err) {
console.log(err);
setTimeout(start, 5000);
}
};
现在,当客户端启动连接并在自身上运行start()时,这个部分似乎运行良好。成功地连接到signalR集线器。我遇到的问题是connection.invoke("addSingleUserGroup", "someUniqueUserName");
何时运行,尽管错误并不总是发生。在第一次运行时,服务器端的方法将被成功命中,但是它看起来像是后续的调用失败,这是客户端返回的错误,
Uncaught (in promise) Error: Failed to invoke 'addSingleUserGroup' due to an error on the server. HubException: Method does not exist.
at _callbacks.<computed> (signalr.js:1252:36)
at HubConnection._processIncomingData (signalr.js:1364:33)
at HubConnection.connection.onreceive (signalr.js:985:52)
at webSocket.onmessage (signalr.js:2236:30)
我在这里读过几篇文章,但大多数文章似乎都与客户端调用错误的方法名称有关,在调用方法名称的开头使用了大写字母,还有一些文章提到了方法期望1类型参数并接收另一种类型的问题,尽管在我的示例中,很难想象服务器如何不将传入的参数视为字符串,这就是传递的内容。有人对这里可能出了什么问题或者我可以尝试什么有任何想法吗?谢谢!
发布于 2022-11-29 16:24:41
不幸的是,我没有一个真正的答案,但在部署解决方案到我的Azure应用服务,发行版不会产生错误。似乎只有在调试模式下,错误才会持续存在,但正如我所说,我不确定原因。
https://stackoverflow.com/questions/74546522
复制相似问题