我在Angular应用程序中使用SignalR.当我在Angular中销毁组件时,我也想停止与集线器的连接.我使用命令:
this.hubConnection.stop();
Run Code Online (Sandbox Code Playgroud)
但是我在Chrome控制台中收到错误: Websocket已关闭状态代码:1006
在Edge中:错误错误:未捕获(在承诺中):错误:由于连接被关闭而取消了调用.错误:由于连接已关闭而取消了调用.
它实际上工作,连接已经停止,但我想知道为什么我得到错误.
这就是我启动集线器的方式:
this.hubConnection = new HubConnectionBuilder()
.withUrl("/matchHub")
.build();
this.hubConnection.on("MatchUpdate", (match: Match) => {
// some magic
})
this.hubConnection
.start()
.then(() => {
this.hubConnection.invoke("SendUpdates");
});
Run Code Online (Sandbox Code Playgroud)
编辑
我终于找到了这个问题.它是由Mongo改变流引起的.如果我从SendUpdates()方法中删除代码,则会触发OnDisconnected.
public class MatchHub : Hub
{
private readonly IMatchManager matchManager;
public MatchHub(IMatchManager matchManager)
{
this.matchManager = matchManager;
}
public async Task SendUpdates() {
using (var changeStream = matchManager.GetChangeStream()) {
while (changeStream.MoveNext()) {
var changeStreamDocument = changeStream.Current.FullDocument;
if (changeStreamDocument == null) {
changeStreamDocument = BsonSerializer.Deserialize<Match>(changeStream.Current.DocumentKey);
}
await Clients.Caller.SendAsync("MatchUpdate", changeStreamDocument);
}
}
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await base.OnDisconnectedAsync(exception);
}
}
Run Code Online (Sandbox Code Playgroud)
来自经理的方法GetChangeStream.
ChangeStreamOptions options = new ChangeStreamOptions() { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
var watch = mongoDb.Matches.Watch(options).ToEnumerable().GetEnumerator();
return watch;
Run Code Online (Sandbox Code Playgroud)
但我不知道如何解决它.
造成这种情况的原因有很多,但我认为最有可能的是以下一个:
我认为这是因为服务器如何处理连接/断开连接事件。我不能肯定地说,但连接关闭也需要在服务器上使用代码正确处理。尝试覆盖服务器上内置的 On Connected /Disconnected 方法并查看。我的假设只是您正在关闭它,但服务器没有正确关闭,因此没有中继正确的关闭响应。
作为评论找到:获取 websockets 以关闭代码 1006 关闭的原因
您不需要更改连接/断开连接,因为一切都工作正常。但作为答案,这个是最有可能的。
归档时间: |
|
查看次数: |
2893 次 |
最近记录: |