加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-4 – SignalR连接错误

发布时间:2020-12-16 03:59:08 所属栏目:asp.Net 来源:网络整理
导读:我有一个使用SignalR和MVC运行的简单聊天客户端项目,它几乎与 ASP.Net One相同(我只是在试验 – 客户端代码是相同的).我已经连接了以下内容来观察发生的事情: $.connection.hub.stateChanged(function(state){console.log(state)}); 连接工作正常但我注意到
我有一个使用SignalR和MVC运行的简单聊天客户端项目,它几乎与 ASP.Net One相同(我只是在试验 – 客户端代码是相同的).我已经连接了以下内容来观察发生的事情:

$.connection.hub.stateChanged(function(state){console.log(state)});

连接工作正常但我注意到如果我关闭IIS Express并在Chrome中观看控制台,我会看到:

//As you would expect the state goes from connected to connecting
Object {oldState: 1,newState: 2}

//Then it times out after about 30 secs and throws this awesomeness at the console
WebSocket connection to 'ws://localhost:61623/signalr?transport=webSockets&connectionToken=VDF640emz7PFMToC6vxle_5-7QS5dZMszV4SPbQO7EFEmSSsITnwKsZreqfl4MGq8TXitG2xB5F-2ZdHp-2t3shPzN2hemTY1ZmEWlB8NOn5orUVexaSoARk9XjEO5B00&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&messageId=B%2C7%7CL%2C0%7CM%2C0%7CN%2C0&tid=0' failed: WebSocket is closed before the connection is established. jquery.signalR-1.0.1.js:1117

//Reconnecting to Disconnecting - so why the error above?
Object {oldState: 2,newState: 4} 

//Then radio silence if I start IIS again...

2个问题:

尝试重新连接失败时如何避免错误?

为什么SignalR不继续尝试连接到IIS?我认为这是技术的重点……

编辑:同样的事情也发生在FireFox中

解决方法

最终,连接将死亡并停止重试.您可以通过SignalR Docs不断重新连接此方法.

How to continuously reconnect

How to continuously reconnect

In some applications you might want to automatically re-establish a
connection after it has been lost and the attempt to reconnect has
timed out. To do that,you can call the Start method from your Closed
event handler (disconnected event handler on JavaScript clients). You
might want to wait a period of time before calling Start in order to
avoid doing this too frequently when the server or the physical
connection are unavailable. The following code sample is for a
JavaScript client using the generated proxy.

$.connection.hub.disconnected(function() {
   setTimeout(function() {
       $.connection.hub.start();
   },5000); // Restart connection after 5 seconds.
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读