是否可以捕获signalR连接断开事件?错误:连接断开,错误webSocket关闭,状态码:1006

时间:2019-04-09 10:32:26

标签: websocket signalr

我正在使用signalR v.1.1.2 https://github.com/aspnet/SignalR#readme(是的,已经过时了) 我已经成功初始化并启动了signalR连接。 几分钟后,连接断开,我收到了signalR内部库触发的烦人,犹豫的错误记录在控制台中。

Error: Connection disconnected with error 'Error: WebSocket closed with status code: 1006 ()'

我的目标是使用适当的事件侦听器捕获此错误并立即重新连接,或者通过重试连接模式弹出窗口通知用户。我只是赶不上信号灯关闭事件。我想念什么吗?

我非常拼命,以至于我写了20种方法来解决这个问题,但是没有一个对我有用。我确定这些方法都绑定到我设置并运行的连接实体。关于连接生命周期的signalR文档页面,请https://docs.microsoft.com/en-us/aspnet/signalr/overview/guide-to-the-api/handling-connection-lifetime-events#how-to-notify-the-user-about-disconnections

欢迎任何帮助或建议。 预先感谢。

这些工作都没有:

  console.log("disconnected inline cb")
})

connection.connectionSlow((resp) => {
  console.log("connectionSlow inline cb")
})

connection.onclose((resp) => {
  console.log("onclose inline cb")
})

connection.close((resp) => {
  console.log("close inline cb")
})

connection.on('Disconnected', (resp) => {
  console.log("camelCase Disconnected inline cb")
})

connection.on('ConnectionSlow', (resp) => {
  console.log("camelCase ConnectionSlow inline cb")
})

connection.on('OnClose', (resp) => {
  console.log("camelCase OnClose inline cb")
})

connection.on('Close', (resp) => {
  console.log("camelCase Close inline cb")
})

connection.on('disconnected', (resp) => {
  console.log("lowercase Disconnected inline cb")
})

connection.on('connectionSlow', (resp) => {
  console.log("lowercase ConnectionSlow inline cb")
})

connection.on('connectionslow', (resp) => {
  console.log("lowercase 2 Connectionslow inline cb")
})

connection.on('close', (resp) => {
  console.log("lowercase close inline cb")
})

connection.on('onClose', (resp) => {
  console.log("lowercase OnClose inline cb")
})

connection.on('onclose', (resp) => {
  console.log("lowercase 2 Onclose inline cb")
})

connection.connection.disconnected((resp) => {
  console.log("connection.connection disconnected inline cb")
})

connection.connection.onclose((resp) => {
  console.log("connection.connection onclose inline cb")
})

connection.connection.connectionSlow((resp) => {
  console.log("connection.connection connectionSlow inline cb")
})

connection.connection.close((resp) => {
  console.log("connection.connection close inline cb")
})

connection.connection.on('close', (resp) => {
  console.log("connection.connection lowercase close cb")
})

connection.connection.on('Close', (resp) => {
  console.log("connection.connection camelcase close cb")
})

connection.connection.on('disconnected', (resp) => {
  console.log("connection.connection disconnected inline cb")
})

connection.connection.on('onclose', (resp) => {
  console.log("connection.connection onclose inline cb")
})

connection.connection.on('onClose', (resp) => {
  console.log("connection.connection lowercase onclose inline cb")
})

connection.connection.on('connectionSlow', (resp) => {
  console.log("connection.connection lowercase connectionSlow inline cb")
})
connection.connection.on('connectionslow', (resp) => {
  console.log("connection.connection lowercase2 connectionSlow inline cb")
})

connection.connection.on('Disconnected', (resp) => {
  console.log("connection.connection camelCase disconnected inline cb")
})

connection.connection.on('OnClose', (resp) => {
  console.log("connection.connection camelCase onclose inline cb")
})

connection.connection.on('ConnectionSlow', (resp) => {
  console.log("connection.connection camelCase connectionSlow inline cb")
})```

2 个答案:

答案 0 :(得分:1)

所以几个小时后,我们已经解决了问题。
我们所做的是使用适合文档的const RADIAN = Math.PI / 180; const renderCustomizedLabel = ({ cx, cy, midAngle, innerRadius, outerRadius, percent, index }) => { const radius = innerRadius + (outerRadius - innerRadius) * 0.5; const x = cx + radius * Math.cos(-midAngle * RADIAN); const y = cy + radius * Math.sin(-midAngle * RADIAN); return ( <text x={x} y={y} fill="white" textAnchor={x > cx ? "start" : "end"} dominantBaseline="central" > {/* {`${(percent * 100).toFixed(0)}%`} */} {data[index].name} </text> ); }; 语法绑定“关闭,断开连接...”事件。

然而,对我们有用的是显式声明.on事件绑定到connectionHub的连接对象,以便通过signalR内部http连接库在出错时成功触发此功能。

.onclose

答案 1 :(得分:0)

强烈建议您至少使用v2.x。这可能就是为什么较新的文档无法帮助您的部分原因。

您可以看到我的答案here on SO

从本质上讲,这将使您捕获断开连接事件,如果需要的话,将弹出一个模式(您必须对此进行编码),然后在5秒钟后重新连接。