本文介绍了如何检测时,在线/离线状态的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么时候互联网连接丢失和恢复,这样我可以警告说:哎呦,没有互联网和谷歌地图或包含数据的网格从我的服务器导出之间进行切换。

I want to know when internet connection is lost and regained, so that I can toggle between an alert saying "whoops, no internet" and a Google map or a grid containing data derived from my server.

This相关的问题并this其他相关的问题认为他们找到了答案,但他们没有。

This related question and this other related question think that they have the answer, but they do not.

他们的解决方案适用于Chrome版本34.0.1847.137男,微软IE浏览器v11.0.x.x,但不能与火狐v29.0.1,所以我寻求其与所有这三个浏览器的解决方案。

Their solution works with Chrome Version 34.0.1847.137 m, MS IE v11.0.x.x, but NOT with FireFox v29.0.1, so I am seeking a solution which works with all of those three browsers.

[更新] AS @Quad指出有定义是什么意思是网上的不同方式。我definign为我可以卖到,我需要展示给我的用户或者没有数据?

[Update] AS @Quad points out there are different ways of defining what it means to be online. I am definign it as "can I fetch the data which I need to show to my user or not?".

我有几个服务,这是负责从几个服务器获取数据(什么是最好的?一个单一的,参数化,服务?每台服务器一个服务?或者每种类型每个服务器数据的一个服务?我想到了后者,然后,每个服务可以映射到控制器,它映射到一个视图,但我是新来的角,所以很可能是错误的)。

I have several services, which are responsible for fetching data from several servers (what's best? A single, parameterized, service? One service per server? Or one service per type of data per server? I am thinking the latter, as each service can then map to a controller which maps to a view. But I am new to Angular, so may well be wrong).

此外,我不得不codeD服务负责当试图连接丢失重新连接。

Additionally, I had coded a service which is responsible for attempting to reconnect when connection is lost.

任何人谁试图在 $ http.get ,并得到404可以调用服务这将结果
  1)播放,有没有互联网(所以没有人会尝试连接)结果
  2)定期尝试连接到我的服务器和结果
  3)成功时,停止连接尝试和广播的应用程序是目前已恢复正常。

Anyone who tries an $http.get and gets 404 can invoke the service which would
1) broadcast that there was no internet (so that no one else would try to connect)
2) regularly attempt to connect to my server and
3) when successful, stop the connection attempts and broadcast that the app is now online again.

然而,这显得很klunky并在两个相关的问题提供解决方案显得优雅 - 除了FF :-(

However, that seemed very klunky and the solution offered in the two related questions seemed elegant - except for FF :-(

我不能在这里重新发明轮子。怎么别人做呢?其实,我很惊讶,有没有正式的解决方案,角

I cannot be reinventing the wheel here. How do others do it? In fact, I am surprised that there is not already an "official" Angular solution

推荐答案

这我就知道会拦截HTTP处理程序的最佳方式,如果其五百零一分之四百零一/等,然后根据处理它

The best way that I would know would be to intercept the HTTP handler, if its a 401 / 501/ ect then to handle it according

例如:

angular.module('myApp', ['myApp.services'],
    function ($httpProvider) {

    var interceptor = ['$rootScope', '$q', function ($rootScope, $q) {

        function success(response) {
            return response;
        }

        function error(response) {
            var status = response.status; // error code

            if ((status >= 400) && (status < 500)) {
                $rootScope.broadcast("AuthError", status);
                return;
            }

            if ( (status >= 500) && (status < 600) ) {
                $rootScope.broadcast("ServerError", status);
                return;
            }

            // otherwise
            return $q.reject(response);

        }

        return function (promise) {
            return promise.then(success, error);
        }

    }];
    $httpProvider.responseInterceptors.push(interceptor);

然后在code,对于监听的,只是添加在

then in your code that listens for the on of, just add in

$rootScope.$on("ServerError", someServerErrorFunction);

您还可以添加一个内部标志,当该标志只改变播出。

You could also add an internal flag and broadcast only when that flag changed.

但是,如果你正在寻找其中用户不与服务器通信太频繁的解决方案,你可以添加坪服务器每分钟左右一节,但是只要你喜欢,可能不响应。

But if you are looking for a solution where the user is not communicating with the server too frequently, you could add a section that pings the server every minute or so, but that may not be responsive as you like.

这篇关于如何检测时,在线/离线状态的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:30