我的应用程序在公司网络(丑陋的代理和东西)下工作。而且效果不是很好。我希望使用https会有所帮助,但没有帮助。这是我在日志中看到的怪异模式:
[14:13:32 GMT+0600 (N. Central Asia Standard Time)] SignalR: Client subscribed to hub 'modemshub'.
[14:13:32 GMT+0600 (N. Central Asia Standard Time)] SignalR: Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionToken=6aktO0sramoQKhQ9DC7Cs7EbXMUou8LooQRxfup4R0oZCHpBmWBFjyLup%2F3wJLloR8GtJEiUk10YOZJBaSqN8aiGAfXRR4G9hujTFTyiJiz%2FyJ4oMlBIdxqeCc5anI6k&connectionData=%5B%7B%22name%22%3A%22modemshub%22%7D%5D'.
[14:13:32 GMT+0600 (N. Central Asia Standard Time)] SignalR: longPolling transport starting.
[14:13:32 GMT+0600 (N. Central Asia Standard Time)] SignalR: Opening long polling request to 'https://example.com/signalr/connect?transport=longPolling&clientProt…rlCzGHl5kVLClT5ex8&connectionData=%5B%7B%22name%22%3A%22modemshub%22%7D%5D'.
[14:13:33 GMT+0600 (N. Central Asia Standard Time)] SignalR: Long poll complete.
[14:13:33 GMT+0600 (N. Central Asia Standard Time)] SignalR: LongPolling connected.
[14:13:33 GMT+0600 (N. Central Asia Standard Time)] SignalR: longPolling transport connected. Initiating start request.
[14:13:33 GMT+0600 (N. Central Asia Standard Time)] SignalR: Opening long polling request to 'https://example.com/signalr/poll?transport=longPolling&clientProtoco…rlCzGHl5kVLClT5ex8&connectionData=%5B%7B%22name%22%3A%22modemshub%22%7D%5D'.
[14:13:33 GMT+0600 (N. Central Asia Standard Time)] SignalR: The start request succeeded. Transitioning to the connected state.
[14:13:38 GMT+0600 (N. Central Asia Standard Time)] SignalR: Long poll complete.
[14:13:38 GMT+0600 (N. Central Asia Standard Time)] SignalR: Stopping connection.
[14:13:38 GMT+0600 (N. Central Asia Standard Time)] SignalR: Fired ajax abort async = true.
因此,建立了连接,并在5秒后中止了连接(而ConnectionTimeout等于110秒)。并且此模式一次又一次地重复。那太奇怪了。
最佳答案
背景
根据Asp.net:
SignalR使用传输API创建传输连接,而传输API依赖于物理网络连接的存在来创建传输连接。当SignalR终止传输连接或传输API检测到物理连接断开时,传输连接结束。
物理连接可能很慢,或者连接可能会中断。根据中断时间等因素,可能会断开传输连接。然后,SignalR尝试重新建立传输连接。有时,传输连接API会检测到中断并断开传输连接,SignalR会立即发现连接丢失。在其他情况下,传输连接API和SignalR都不会立即意识到连接已丢失。对于长轮询以外的所有传输,SignalR客户端使用一个称为keepalive的功能来检查传输API无法检测到的连接丢失。
Troubleshooting
请注意,SignalR 2.1引入了保持活动状态以进行长时间轮询。如果某些内容干扰了分块的HTTP响应,则可能会出现问题。如果要使用disable
keepalive
功能,请将KeepAlive
设置为null
。 Keepalive
功能对于长轮询传输自动为disabled
。
如果您是using a Self-Host,请使用以下带有3个参数的参数:
GlobalHost.Configuration.ConnectionTimeout = new TimeSpan(0,0,110);
GlobalHost.Configuration.DisconnectTimeout = new TimeSpan(0,0,30);
GlobalHost.Configuration.KeepAlive = new TimeSpan(0,0,10);
作为支持长时间轮询的保持活动“赞”功能的其他替代方法,请创建一个名为
Ping
的服务器方法:public class MyHub : Hub
{
public void Ping()
{
}
}
然后,在客户端上创建一个间隔,在该间隔中您将
Ping
服务器:var proxy = $.connection.myHub,
intervalHandle;
...
$.connection.hub.disconnected(function() {
clearInterval(intervalHandle);
});
...
$.connection.hub.start().done(function() {
// Only when long polling
if($.connection.hub.transport.name === "longPolling") {
// Ping every 10s
intervalHandle = setInterval(function() {
// Ensure we're connected (don't want to be pinging in any other state).
if($.connection.hub.state === $.signalR.connectionState.connected) {
proxy.server.ping().fail(function() {
// Failed to ping the server, we could either try one more time to ensure we can't reach the server
// or we could fail right here.
TryAndRestartConnection(); // Your method
});
}
}, 10000);
}
});
我希望会有所帮助。