顺序订阅一系列可观察量

时间:2017-09-24 09:13:25

标签: angular rxjs observable fork-join

在这里,我使用rxjs中的forkJoin来并行地订阅一个observable数组。但是我想逐个订阅它们,什么是最好的解决方案?

以下是我的代码:

var observables = [];

Observable.forkJoin(observables)
    .subscribe(() => {
        this.msgs = [];
        this.msgs.push({
            severity: 'success',
            summary: 'Saved Successfully'
        });
        this.onSaveComplete();
    }, (error: any) => this.errorMessage = <any>error);
}, (error: any) => this.errorMessage = <any>error);

2 个答案:

答案 0 :(得分:4)

forkJoin的替代意味着您需要以顺序方式订阅可观察数组。 mergeconcat是这种情况下的方法。在您的情况下,在使用mergeconcat时,修改您的代码并在您的可观察数组的最开头使用扩展运算符。

var observables = [];
Observable.concat(...observables)
            .subscribe(() => {
                this.msgs = [];
                this.msgs.push({
                    severity: 'success',
                    summary: 'Saved Successfully'
                });
                this.onSaveComplete();
            }, (error: any) => this.errorMessage = <any>error);
    }, (error: any) => this.errorMessage = <any>error);

答案 1 :(得分:0)

使用the switchMap operator制作一系列相关请求。错误将传播,您可以在链的末尾捕获或以其他方式处理它们。

worker_processes auto;
error_log logs/error.log debug;
events {

    worker_connections 1024;
}
rtmp {

    server {

        listen 1935;
        allow play all;
        chunk_size 4000;

        #hls - "live" full-resolution HLS videostream from our incoming encoder stream and tells where to put the HLS video manifest and video fragments
        application live {

            allow play all;
            live on;
            meta on;
            #record all;
            #record_path /video_recordings;
            #record_unique off;
            hls on;
            hls_nested on;
            hls_path /HLS/live;
            hls_fragment 10s;
            hls_type live;
            allow publish 127.0.0.1;
            #allow publish 192.168.2.0/24;
            #allow publish 192.168.22.0/24;
            #allow publish 70.62.116.192/27;
            #deny publish all;
            allow publish all;
            # create the sampled HLS streams
            # 240p - low def 288kbps / 480p standard def 448kbps / 720p hi def at 2048kbps / source
            # create 3 other stored HLS variants in vod
            exec ffmpeg -i rtmp://localhost:1935/$app/$name
            -c:v libx264 -acodec copy -b:v 256k -vf "scale=480:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -g 60 -hls_list_size 0 -f flv rtmp://localhost:1935/savehls/$name_low
            -c:v libx264 -acodec copy -b:v 768k -vf "scale=720:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -g 60 -hls_list_size 0 -f flv rtmp://localhost:1935/savehls/$name_mid
            -c:v libx264 -acodec copy -b:v 1920k -vf "scale=1080:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -g 60 -hls_list_size 0 -f flv rtmp://localhost:1935/savehls/$name_high;

            # create local mp4 archive
            exec ffmpeg -i rtmp://localhost:1935/$app/$name -c:v libx264 -vprofile baseline -acodec aac -strict -2 -f mp4 /video_recordings/$name.mp4;
            # exec ffmpeg -y -i $path -c:v libx264 -vprofile baseline -acodec aac -strict -2 -f mp4 $dirname/$name.mp4;
        }

        # hls converted streams
        # - streams for mobile live and archives the HLS video manifest and video fragments
        application savehls {

            allow play all;
            live on;
            hls on;
            hls_nested on;
            hls_path /HLS/streams;
            hls_fragment 10s;
            hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution
            hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution
            hls_variant _high BANDWIDTH=2048000; # High bitrate, HD 720p resolution
            # turn off clean-up, if we want these to stay
            # - note: the ts files will only have the last 5 seconds or so of ts files. so doing this is of limited help
            hls_cleanup on;
        }

        #allows you to play your recordings of your live streams using a URL like "rtmp://my-ip:1935/vod/filename.flv"
        #application vod {
        #play /video_recordings;
        #}

    }
}

http {

    include /etc/nginx/geoip/*.http.scope;
    include /usr/local/nginx/conf/mime.types;
    sendfile off;
    tcp_nopush on;
    directio 512;
    # geoip variables/filters
    #geoip_country /usr/share/GeoIP/GeoIP.dat;
    vhost_traffic_status_zone;
    map $http_user_agent $filter_user_agent {

        default 'unknown';
        ~iPhone ios;
        ~Android android;
        ~(MSIE|Mozilla) windows;
    }
    vhost_traffic_status_filter_by_set_key $filter_user_agent agent::*;
    #vhost_traffic_status_filter_by_set_key $geoip_country_code country::*;

    server {

        listen 80;
        server_name xxx.160.78.xx;

        # stats variables
        #vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;
        vhost_traffic_status_filter_by_set_key $filter_user_agent agent::$server_name;

        # vod caches
        vod_metadata_cache metadata_cache 2048m;
        vod_response_cache response_cache 128m;

        # vod variables
        open_file_cache max=1000 inactive=5m;
        open_file_cache_valid 2m;
        open_file_cache_min_uses 1;
        open_file_cache_errors on;
        aio on;
        # vod share
        location /vodhls/ {

            vod hls;
            vod_mode local;
            vod_align_segments_to_key_frames on;
            alias /video_recordings/;
            gzip on;
            gzip_types application/vnd.apple.mpegurl;
            vod_last_modified_types *;
            add_header Access-Control-Allow-Headers 'origin,range,accept-encoding,referrer';
            add_header Access-Control-Expose-Headers 'Server,range,Content-Length,Content-Range';
            add_header Access-Control-Allow-Methods 'GET, HEAD, OPTIONS';
            add_header Access-Control-Allow-Origin '*';
            expires 100d;
            add_header Last-Modified "Sun, 19 Nov 2000 08:52:00 GMT";
        }

        # hls - http-location for full-resolution desktops
        # - "http://my-ip/live/my-stream-key/index.m3u8"
        location /live {

            default_type application/octet-stream;
            types {

                application/dash+xml mpd;
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            alias /HLS/live;
            # disable cache and add CORS headers
            add_header Cache-Control no-cache;
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
            add_header 'Access-Control-Allow-Headers' 'Range';

            # allow CORS preflight requests
            if ($request_method = 'OPTIONS') {

                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Headers' 'Range';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }
        }

        # HLS - create the http-location for mobile-device live streams and hls stream archives
        # - "http://my-ip/mobile/my-stream-key/index.m3u8"
        location /streams {

            types {

                application/dash+xml mpd;
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /HLS/streams;
            add_header Cache-Control no-cache;
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
            add_header 'Access-Control-Allow-Headers' 'Range';
        }

        # creates http access to vod folder
        location /vod {

            alias /video_recordings;
            index index.html;
            add_header Cache-Control no-cache;
            include /usr/local/nginx/conf/mime.types;
        }

        #allows us to see how stats on viewers on our Nginx site using a URL like: "http://my-ip/stats"
        location /mwstats {

            rtmp_stat all;
            rtmp_stat_stylesheet /stat.xsl;
            stub_status;
            access_log off;
        }

        location /vod_status {

            vod_status;
            access_log off;
        }

        location /mwhoststats {

            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }

        #allows us to host some webpages which can show our videos: "http://my-ip/my-page.html"
        #location / {
        #default_type text/html;
        #include /usr/local/nginx/conf/mime.types;
        #include /usr/local/nginx/conf/cors.conf;
        #root html;
        #index index.html index.htm;

        #}
    }
}