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

标签 asp.net-mvc-4 signalr

我有一个使用 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 文档中的此方法让它不断重新连接。

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.
});

关于asp.net-mvc-4 - SignalR 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16461174/

相关文章:

c# - 需要在消息处理程序中引用的全局对象

c# - Signalr SQL 横向扩展 : drawbacks of using multiple streams?

asp.net - ASP.NET SignalR 中的信号是什么?

c# - 编辑 ID - 存储更新、插入或删除语句影响了意外的行数 (0)

asp.net-mvc-4 - 使用结构图将 SetterProperty 注入(inject)到 Asp.Net MVC ActionFilter

javascript - 如何在 jquery 对话框模型表单或引导弹出窗口中显示 CRUD View

c# - 从 Controller 调用 SignalR Core Hub 方法时的连接 ID

asp.net-core - 避免 DateTime 转换,同时将其传递给 AspNetCore.SignalR .Net 客户端

LINQ 查询相关属性中的 ofType

entity-framework - 是否可以将现有的 MVC4 Web 应用程序升级到 MVC5?