Authored by 盖剑秋

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

... ... @@ -8,7 +8,7 @@
#import <Foundation/Foundation.h>
@interface YH_ChannelModel : NSObject
@interface YH_ChannelModel : NSObject <NSCoding>
@property (nonatomic, copy) NSString *roomTitle;
@property (nonatomic, copy) NSString *roomURL;
... ...
... ... @@ -10,4 +10,28 @@
@implementation YH_ChannelModel
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
_roomTitle = [aDecoder decodeObjectForKey:@"roomTitle"];
_roomURL = [aDecoder decodeObjectForKey:@"roomURL"];
_channel_id = [aDecoder decodeObjectForKey:@"channel_id"];
_cid = [aDecoder decodeObjectForKey:@"cid"];
_app = [aDecoder decodeObjectForKey:@"app"];
_type = [aDecoder decodeObjectForKey:@"type"];
_room = [aDecoder decodeObjectForKey:@"room"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_roomTitle forKey:@"roomTitle"];
[aCoder encodeObject:_roomURL forKey:@"roomURL"];
[aCoder encodeObject:_channel_id forKey:@"channel_id"];
[aCoder encodeObject:_cid forKey:@"cid"];
[aCoder encodeObject:_app forKey:@"app"];
[aCoder encodeObject:_type forKey:@"type"];
[aCoder encodeObject:_room forKey:@"room"];
}
@end
... ...
... ... @@ -38,6 +38,7 @@
#define SOCKET_LIVE_PORT 9512
#define SOCKET_COMMAND @"cmd"
#define kPushging @"pushing_key"
@interface YH_RootViewController ()<TXLivePushListener, UITableViewDelegate, UITableViewDataSource,YH_SocketServiceDelegate>
... ... @@ -139,6 +140,7 @@
[self setUpPush];
_socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
_currentRetryCount = 0;
[self.socketService connect];
}
- (void)setUpPush {
... ... @@ -153,63 +155,63 @@
- (void)loadChannelData {
//测试数据。
YH_ChannelModel *model1 = [YH_ChannelModel new];
model1.roomTitle = @"啦啦啦啦";
model1.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
YH_ChannelModel *model2 = [YH_ChannelModel new];
model2.roomTitle = @"噜噜噜噜";
model2.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
YH_ChannelModel *model3 = [YH_ChannelModel new];
model3.roomTitle = @"咯囖囖囖囖";
model3.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
YH_ChannelModel *model4 = [YH_ChannelModel new];
model4.roomTitle = @"哩哩哩哩哩";
model4.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
_channels = @[model1,model2,model3,model4];
[_channelTable reloadData];
// if (_loading) {
// return;
// }
// _loading = YES;
// AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
// [manager
// GET:@"http://newboys.test.yoho.cn/yohoboyins/v4/qcloud/getPushFlow"
// parameters:nil progress:NULL
// success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// if ([responseObject isKindOfClass:[NSDictionary class]]) {
// id data = responseObject[@"data"];
// if (![data isKindOfClass:[NSArray class]]) {
// [self toastTip:@"没有可用直播间"];
// return;
// }
// NSMutableArray *tempAry = @[].mutableCopy;
// [data enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// if (![obj isKindOfClass:[NSDictionary class]]) {
// *stop = YES;
// [self toastTip:@"没有可用直播间"];
// }
// YH_ChannelModel *model = [YH_ChannelModel new];
// model.roomTitle = obj[@"name"];
// model.roomURL = obj[@"url"];
// model.channel_id = obj[@"channel_id"];
// model.cid = obj[@"cid"];
// model.app = obj[@"app"];
// model.type = obj[@"type"];
// model.room = obj[@"id"];
// [tempAry addObject:model];
// }];
// _channels = [NSArray arrayWithArray:tempAry];
// YH_ChannelModel *model1 = [YH_ChannelModel new];
// model1.roomTitle = @"啦啦啦啦";
// model1.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
// YH_ChannelModel *model2 = [YH_ChannelModel new];
// model2.roomTitle = @"噜噜噜噜";
// model2.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
// YH_ChannelModel *model3 = [YH_ChannelModel new];
// model3.roomTitle = @"咯囖囖囖囖";
// model3.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
// YH_ChannelModel *model4 = [YH_ChannelModel new];
// model4.roomTitle = @"哩哩哩哩哩";
// model4.roomURL = @"rtmp://2718.livepush.myqcloud.com/live/2718_01973243308211e6a2cba4dcbef5e35a?bizid=2718";
// _channels = @[model1,model2,model3,model4];
// [_channelTable reloadData];
// }
// NSLog(@"bbbb%@",responseObject);
// _loading = NO;
// }
// failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// _loading = NO;
// [self toastTip:[NSString stringWithFormat:@"房间加载失败:%@",error.localizedDescription]];
// }];
if (_loading) {
return;
}
_loading = YES;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager
GET:@"http://newboys.test.yoho.cn/yohoboyins/v4/qcloud/getPushFlow"
parameters:nil progress:NULL
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject isKindOfClass:[NSDictionary class]]) {
id data = responseObject[@"data"];
if (![data isKindOfClass:[NSArray class]]) {
[self toastTip:@"没有可用直播间"];
return;
}
NSMutableArray *tempAry = @[].mutableCopy;
[data enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (![obj isKindOfClass:[NSDictionary class]]) {
*stop = YES;
[self toastTip:@"没有可用直播间"];
}
YH_ChannelModel *model = [YH_ChannelModel new];
model.roomTitle = obj[@"name"];
model.roomURL = obj[@"url"];
model.channel_id = obj[@"channel_id"];
model.cid = obj[@"cid"];
model.app = obj[@"app"];
model.type = obj[@"type"];
model.room = obj[@"id"];
[tempAry addObject:model];
}];
_channels = [NSArray arrayWithArray:tempAry];
[_channelTable reloadData];
}
NSLog(@"bbbb%@",responseObject);
_loading = NO;
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
_loading = NO;
[self toastTip:[NSString stringWithFormat:@"房间加载失败:%@",error.localizedDescription]];
}];
}
... ... @@ -305,6 +307,8 @@
[_livePush startPush:rtmpUrl];
}
_inPushing = YES;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_currentChannel];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:kPushging];
return YES;
}
... ... @@ -389,7 +393,7 @@
if (_socket.isConnected) {
NSDictionary *params = @{@"cmd":@(11),
@"room":_currentChannel.room?:@"666",
@"msg":@(666),//结束时的瞬时在线人数
@"msg":_onlineCountLabel.text,//结束时的瞬时在线人数
@"videoLen":_timeLabel.text,
@"uid":@""
};
... ... @@ -401,7 +405,7 @@
- (void)connectSocket {
[_socket connectToHost:SOCKET_LIVE_HOST onPort:SOCKET_LIVE_PORT error:nil];
[_socket connectToHost:_socketService.socketLiveHost onPort:_socketService.socketLivePort error:nil];
}
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
... ... @@ -453,6 +457,7 @@
dispatch_async(dispatch_get_main_queue(), ^{
DLog(@"通知结束成功");
[self toastTip:@"通知结束成功"];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kPushging];
[sock disconnect];
_inPushing = NO;
[_socketTimer invalidate];
... ... @@ -638,7 +643,7 @@
NSLog(@"%@",task.originalRequest.URL.absoluteString);
[_timer invalidate];
[self.socketService endPaly:_timeLabel.text];
[self socketSendEndNotify];//通知结束。
}
}
... ... @@ -937,6 +942,11 @@
}
- (void)initChannelPannel {
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kPushging];
if (data.length) {
_currentChannel = [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
_channelPannel = [UIView new];
_channelPannel.backgroundColor = [UIColor clearColor];
[self.view addSubview:_channelPannel];
... ... @@ -962,6 +972,12 @@
[_channelPannel addSubview:_liveButton];
if (_currentChannel) {
_inPushing = YES;
_liveButton.selected = YES;
[_liveButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
_channelTable = [UITableView new];
... ...