asp.net - SignalR 第一次连接很好,但是当重新连接启动时,它没有完成重新连接

标签 asp.net angularjs signalr signalr.client

我正在使用 SignalR 将通知推送到 Angular 应用程序。

我以前让它工作过(虽然它看起来有点参差不齐),现在我遇到了更多关于重新连接的问题。

我已经到了这样的地步:盯着文档的演示代码和其他人的代码似乎从来没有像它声称的那样在 SignalR 中工作。因此,我发布了我的代码,看看是否有任何我公然做错的事情会导致 SignalR 无法始终如一地工作以及始终如一地启动、重新连接和断开连接。

我有一个如下所示的通知服务:

.factory('Notification', ['$rootScope', '$log', '$localstorage', 'User', 'ENV', 'Events',
    function ($rootScope, $log, $localstorage, User, ENV, Events) {
        var self = this;

        this.proxy = null;
        this.connection = null;
        this.initializeSockets = function () {

            self.connection  = $.hubConnection(ENV.socketEndpoint, { useDefaultPath: true, logging: true })
            self.connection.qs = { token: User.getToken() };
            self.proxy = self.connection.createHubProxy('notificationEmitter');
            self.proxy.on('onNotification', function (notification) {
                $rootScope.$emit(notification.Topic, notification.Data);
            });

            if (self.connection.state === $.signalR.connectionState.disconnected) {
                self.connection.start(function () {})
                    .done(function (conn) {
                    $log.info('Connected to notification emitter');
                }).fail(function (a) {
                    $log.info('Unable to connect to connect:' + a)
                });
            }

            self.connection.starting = function (data) {
                $log.info('starting'); -> NEVER SEEMS TO GET CALLED
            }
            self.connection.disconnected = function (data) {
                $log.info('disconnected'); -> NEVER SEEMS TO GET CALLED
                setTimeout(function () {
                    proxy.connection.start();
                }, 20000); // Restart connection after 5 seconds
            };

            self.connection.reconnecting = function (data) {
                $log.info('reconnecting');
            }
            self.connection.reconnected = function (data) {
                $log.info('reconnected');
            }
            self.connection.connectionSlow = function (data) {
                $log.info('connectionSlow');
            }
            self.connection.error = function(error){
                $log.info('Signal R Error:' + error);
            }
        }

        $rootScope.$on(Events.userAuthenticated, function (event, user) {
            self.initializeSockets();
        })

        return self;

    }])

正如我所提到的,它似乎在我第一次开始处理它时大部分时间都在连接,但是在第一页刷新之后(以及之后的每次刷新)我从 SignalR 日志中得到以下内容..

enter image description here

最佳答案

starting: Raised before any data is sent over the connection.

所以这与连接无关。

The OnReconnected event handler in a SignalR Hub can execute directly after OnConnected but not after OnDisconnected for a given client. The reason you can have a reconnection without a disconnection is that there are several ways in which the word "connection" is used in SignalR.

重新连接发生在连接之后。所以你从未见过断开连接的日志是正常的。

If the client stops running without having a chance to call the Stop method, the server waits for the client to reconnect, and then ends the SignalR connection after the disconnect timeout period.

因此,您实际上在每次刷新页面时都这样做。您无需调用 Stop 即可停止运行连接。

如果你没有任何问题,这个生命周期是很正常的。

检查 more

关于asp.net - SignalR 第一次连接很好,但是当重新连接启动时,它没有完成重新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35933836/

相关文章:

asp.net-mvc-3 - 用于 asp.net MVC 聊天应用程序的 signalR 与 html5 websockets

javascript - ASP.NET JavaScript 斜杠符号转义

c# - 找不到文件 'bin\DotNetOpenAuth.AspNet.xml'。 - 发布到 Elastic Beanstalk 时

c# - SignalR 2.2.0 : client cannot call hub, 集线器未被调用

javascript - 如何在 AngularJS 中自动上传文件?

javascript - 如何在一个 html 文件中 "compile"AngularJS 应用程序并保持所有源内联。 ( Grunt )

c# - 将用户映射到连接 ID 的最佳方法是什么

c# - 显示 GridView 中的行数

asp.net - 是否使用 CMS

javascript - karma 测试运行器未运行任何测试