Authored by 盖剑秋

增加对强行关闭app的行为的断线重连,动态配置长连接接口。

@@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
8 8
9 #import <Foundation/Foundation.h> 9 #import <Foundation/Foundation.h>
10 10
11 -@interface YH_ChannelModel : NSObject 11 +@interface YH_ChannelModel : NSObject <NSCoding>
12 12
13 @property (nonatomic, copy) NSString *roomTitle; 13 @property (nonatomic, copy) NSString *roomTitle;
14 @property (nonatomic, copy) NSString *roomURL; 14 @property (nonatomic, copy) NSString *roomURL;
@@ -10,4 +10,28 @@ @@ -10,4 +10,28 @@
10 10
11 @implementation YH_ChannelModel 11 @implementation YH_ChannelModel
12 12
  13 +- (instancetype)initWithCoder:(NSCoder *)aDecoder {
  14 + if (self = [super init]) {
  15 + _roomTitle = [aDecoder decodeObjectForKey:@"roomTitle"];
  16 + _roomURL = [aDecoder decodeObjectForKey:@"roomURL"];
  17 + _channel_id = [aDecoder decodeObjectForKey:@"channel_id"];
  18 + _cid = [aDecoder decodeObjectForKey:@"cid"];
  19 + _app = [aDecoder decodeObjectForKey:@"app"];
  20 + _type = [aDecoder decodeObjectForKey:@"type"];
  21 + _room = [aDecoder decodeObjectForKey:@"room"];
  22 + }
  23 + return self;
  24 +}
  25 +
  26 +- (void)encodeWithCoder:(NSCoder *)aCoder {
  27 +
  28 + [aCoder encodeObject:_roomTitle forKey:@"roomTitle"];
  29 + [aCoder encodeObject:_roomURL forKey:@"roomURL"];
  30 + [aCoder encodeObject:_channel_id forKey:@"channel_id"];
  31 + [aCoder encodeObject:_cid forKey:@"cid"];
  32 + [aCoder encodeObject:_app forKey:@"app"];
  33 + [aCoder encodeObject:_type forKey:@"type"];
  34 + [aCoder encodeObject:_room forKey:@"room"];
  35 +
  36 +}
13 @end 37 @end
@@ -38,6 +38,7 @@ @@ -38,6 +38,7 @@
38 #define SOCKET_LIVE_PORT 9512 38 #define SOCKET_LIVE_PORT 9512
39 39
40 #define SOCKET_COMMAND @"cmd" 40 #define SOCKET_COMMAND @"cmd"
  41 +#define kPushging @"pushing_key"
41 42
42 @interface YH_RootViewController ()<TXLivePushListener, UITableViewDelegate, UITableViewDataSource,YH_SocketServiceDelegate> 43 @interface YH_RootViewController ()<TXLivePushListener, UITableViewDelegate, UITableViewDataSource,YH_SocketServiceDelegate>
43 44
@@ -139,6 +140,7 @@ @@ -139,6 +140,7 @@
139 [self setUpPush]; 140 [self setUpPush];
140 _socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)]; 141 _socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
141 _currentRetryCount = 0; 142 _currentRetryCount = 0;
  143 + [self.socketService connect];
142 } 144 }
143 145
144 - (void)setUpPush { 146 - (void)setUpPush {
@@ -153,63 +155,63 @@ @@ -153,63 +155,63 @@
153 - (void)loadChannelData { 155 - (void)loadChannelData {
154 156
155 //测试数据。 157 //测试数据。
156 - YH_ChannelModel *model1 = [YH_ChannelModel new];  
157 - model1.roomTitle = @"啦啦啦啦";  
158 - model1.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";  
159 - YH_ChannelModel *model2 = [YH_ChannelModel new];  
160 - model2.roomTitle = @"噜噜噜噜";  
161 - model2.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";  
162 - YH_ChannelModel *model3 = [YH_ChannelModel new];  
163 - model3.roomTitle = @"咯囖囖囖囖";  
164 - model3.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";  
165 - YH_ChannelModel *model4 = [YH_ChannelModel new];  
166 - model4.roomTitle = @"哩哩哩哩哩";  
167 - model4.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";  
168 - _channels = @[model1,model2,model3,model4];  
169 - [_channelTable reloadData];  
170 -  
171 -// if (_loading) {  
172 -// return;  
173 -// }  
174 -// _loading = YES;  
175 -// AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];  
176 -// manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];  
177 -// [manager  
178 -// GET:@"http://newboys.test.yoho.cn/yohoboyins/v4/qcloud/getPushFlow"  
179 -// parameters:nil progress:NULL  
180 -// success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {  
181 -// if ([responseObject isKindOfClass:[NSDictionary class]]) {  
182 -// id data = responseObject[@"data"];  
183 -// if (![data isKindOfClass:[NSArray class]]) {  
184 -// [self toastTip:@"没有可用直播间"];  
185 -// return;  
186 -// }  
187 -// NSMutableArray *tempAry = @[].mutableCopy;  
188 -// [data enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {  
189 -// if (![obj isKindOfClass:[NSDictionary class]]) {  
190 -// *stop = YES;  
191 -// [self toastTip:@"没有可用直播间"];  
192 -// }  
193 -// YH_ChannelModel *model = [YH_ChannelModel new];  
194 -// model.roomTitle = obj[@"name"];  
195 -// model.roomURL = obj[@"url"];  
196 -// model.channel_id = obj[@"channel_id"];  
197 -// model.cid = obj[@"cid"];  
198 -// model.app = obj[@"app"];  
199 -// model.type = obj[@"type"];  
200 -// model.room = obj[@"id"];  
201 -// [tempAry addObject:model];  
202 -// }];  
203 -// _channels = [NSArray arrayWithArray:tempAry];  
204 -// [_channelTable reloadData];  
205 -// }  
206 -// NSLog(@"bbbb%@",responseObject);  
207 -// _loading = NO;  
208 -// }  
209 -// failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {  
210 -// _loading = NO;  
211 -// [self toastTip:[NSString stringWithFormat:@"房间加载失败:%@",error.localizedDescription]];  
212 -// }]; 158 +// YH_ChannelModel *model1 = [YH_ChannelModel new];
  159 +// model1.roomTitle = @"啦啦啦啦";
  160 +// model1.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
  161 +// YH_ChannelModel *model2 = [YH_ChannelModel new];
  162 +// model2.roomTitle = @"噜噜噜噜";
  163 +// model2.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
  164 +// YH_ChannelModel *model3 = [YH_ChannelModel new];
  165 +// model3.roomTitle = @"咯囖囖囖囖";
  166 +// model3.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
  167 +// YH_ChannelModel *model4 = [YH_ChannelModel new];
  168 +// model4.roomTitle = @"哩哩哩哩哩";
  169 +// model4.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
  170 +// _channels = @[model1,model2,model3,model4];
  171 +// [_channelTable reloadData];
  172 +
  173 + if (_loading) {
  174 + return;
  175 + }
  176 + _loading = YES;
  177 + AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  178 + manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
  179 + [manager
  180 + GET:@"http://newboys.test.yoho.cn/yohoboyins/v4/qcloud/getPushFlow"
  181 + parameters:nil progress:NULL
  182 + success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  183 + if ([responseObject isKindOfClass:[NSDictionary class]]) {
  184 + id data = responseObject[@"data"];
  185 + if (![data isKindOfClass:[NSArray class]]) {
  186 + [self toastTip:@"没有可用直播间"];
  187 + return;
  188 + }
  189 + NSMutableArray *tempAry = @[].mutableCopy;
  190 + [data enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  191 + if (![obj isKindOfClass:[NSDictionary class]]) {
  192 + *stop = YES;
  193 + [self toastTip:@"没有可用直播间"];
  194 + }
  195 + YH_ChannelModel *model = [YH_ChannelModel new];
  196 + model.roomTitle = obj[@"name"];
  197 + model.roomURL = obj[@"url"];
  198 + model.channel_id = obj[@"channel_id"];
  199 + model.cid = obj[@"cid"];
  200 + model.app = obj[@"app"];
  201 + model.type = obj[@"type"];
  202 + model.room = obj[@"id"];
  203 + [tempAry addObject:model];
  204 + }];
  205 + _channels = [NSArray arrayWithArray:tempAry];
  206 + [_channelTable reloadData];
  207 + }
  208 + NSLog(@"bbbb%@",responseObject);
  209 + _loading = NO;
  210 + }
  211 + failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  212 + _loading = NO;
  213 + [self toastTip:[NSString stringWithFormat:@"房间加载失败:%@",error.localizedDescription]];
  214 + }];
213 215
214 } 216 }
215 217
@@ -305,6 +307,8 @@ @@ -305,6 +307,8 @@
305 [_livePush startPush:rtmpUrl]; 307 [_livePush startPush:rtmpUrl];
306 } 308 }
307 _inPushing = YES; 309 _inPushing = YES;
  310 + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_currentChannel];
  311 + [[NSUserDefaults standardUserDefaults] setObject:data forKey:kPushging];
308 return YES; 312 return YES;
309 } 313 }
310 314
@@ -389,7 +393,7 @@ @@ -389,7 +393,7 @@
389 if (_socket.isConnected) { 393 if (_socket.isConnected) {
390 NSDictionary *params = @{@"cmd":@(11), 394 NSDictionary *params = @{@"cmd":@(11),
391 @"room":_currentChannel.room?:@"666", 395 @"room":_currentChannel.room?:@"666",
392 - @"msg":@(666),//结束时的瞬时在线人数 396 + @"msg":_onlineCountLabel.text,//结束时的瞬时在线人数
393 @"videoLen":_timeLabel.text, 397 @"videoLen":_timeLabel.text,
394 @"uid":@"" 398 @"uid":@""
395 }; 399 };
@@ -401,7 +405,7 @@ @@ -401,7 +405,7 @@
401 405
402 406
403 - (void)connectSocket { 407 - (void)connectSocket {
404 - [_socket connectToHost:SOCKET_LIVE_HOST onPort:SOCKET_LIVE_PORT error:nil]; 408 + [_socket connectToHost:_socketService.socketLiveHost onPort:_socketService.socketLivePort error:nil];
405 } 409 }
406 410
407 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port 411 - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
@@ -453,6 +457,7 @@ @@ -453,6 +457,7 @@
453 dispatch_async(dispatch_get_main_queue(), ^{ 457 dispatch_async(dispatch_get_main_queue(), ^{
454 DLog(@"通知结束成功"); 458 DLog(@"通知结束成功");
455 [self toastTip:@"通知结束成功"]; 459 [self toastTip:@"通知结束成功"];
  460 + [[NSUserDefaults standardUserDefaults] removeObjectForKey:kPushging];
456 [sock disconnect]; 461 [sock disconnect];
457 _inPushing = NO; 462 _inPushing = NO;
458 [_socketTimer invalidate]; 463 [_socketTimer invalidate];
@@ -638,7 +643,7 @@ @@ -638,7 +643,7 @@
638 NSLog(@"%@",task.originalRequest.URL.absoluteString); 643 NSLog(@"%@",task.originalRequest.URL.absoluteString);
639 644
640 [_timer invalidate]; 645 [_timer invalidate];
641 - 646 + [self.socketService endPaly:_timeLabel.text];
642 [self socketSendEndNotify];//通知结束。 647 [self socketSendEndNotify];//通知结束。
643 } 648 }
644 } 649 }
@@ -937,6 +942,11 @@ @@ -937,6 +942,11 @@
937 } 942 }
938 943
939 - (void)initChannelPannel { 944 - (void)initChannelPannel {
  945 +
  946 + NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kPushging];
  947 + if (data.length) {
  948 + _currentChannel = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  949 + }
940 _channelPannel = [UIView new]; 950 _channelPannel = [UIView new];
941 _channelPannel.backgroundColor = [UIColor clearColor]; 951 _channelPannel.backgroundColor = [UIColor clearColor];
942 [self.view addSubview:_channelPannel]; 952 [self.view addSubview:_channelPannel];
@@ -962,6 +972,12 @@ @@ -962,6 +972,12 @@
962 [_channelPannel addSubview:_liveButton]; 972 [_channelPannel addSubview:_liveButton];
963 973
964 974
  975 + if (_currentChannel) {
  976 + _inPushing = YES;
  977 + _liveButton.selected = YES;
  978 + [_liveButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  979 +
  980 + }
965 981
966 982
967 _channelTable = [UITableView new]; 983 _channelTable = [UITableView new];