|
@@ -15,14 +15,13 @@
|
|
|
|
|
|
@interface JYgRPCClient() {
|
|
|
|
|
|
- BOOL regStatus;
|
|
|
+ BOOL isRunning;
|
|
|
}
|
|
|
|
|
|
@property (nonatomic,weak)NSTimer *hostTimer;
|
|
|
|
|
|
@property (nonatomic,assign)NSInteger msgIndex;
|
|
|
|
|
|
-//@property (nonatomic,weak)NSTimer *heartTimer;
|
|
|
|
|
|
@end
|
|
|
|
|
@@ -55,45 +54,38 @@
|
|
|
|
|
|
|
|
|
-(void)initData {
|
|
|
+ if (self.hostTimer!=nil) {
|
|
|
+ NSLog(@"先清理...");
|
|
|
+ [self clearTimer];
|
|
|
+ }
|
|
|
self.msgIndex = 0;
|
|
|
- regStatus = NO;
|
|
|
+ isRunning = NO;
|
|
|
//不断监听 10秒执行一次 注册
|
|
|
+ NSLog(@"初始化定时器");
|
|
|
self.hostTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(hostTimerFireMethod) userInfo:nil repeats:YES];
|
|
|
[self.hostTimer fire];
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-}
|
|
|
--(void)heartTimerFireMethod {
|
|
|
-// NSLog(@"执行一次");
|
|
|
-
|
|
|
- MsgPushReq *req = [[MsgPushReq alloc]init];
|
|
|
- [self.service pushBackWithRequest:req handler:^(MsgPushResp * _Nullable response, NSError * _Nullable error) {
|
|
|
-// NSLog(@"%@~~~%@",response,error);
|
|
|
- }];
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-(void)hostTimerFireMethod {
|
|
|
-// NSLog(@"执行一次请求获取host");
|
|
|
- if (!regStatus) {
|
|
|
+ NSLog(@"执行一次请求获取host");
|
|
|
+ if (!isRunning) { //还未获取地址
|
|
|
+ isRunning = YES;
|
|
|
[self getHost];
|
|
|
- }else {
|
|
|
- [self.hostTimer invalidate];
|
|
|
- self.hostTimer = nil;
|
|
|
+ }else {//等待...
|
|
|
+ NSLog(@"等待...");
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
-(void)clearTimer {
|
|
|
- regStatus = YES;
|
|
|
[self.hostTimer invalidate];
|
|
|
self.hostTimer = nil;
|
|
|
+ isRunning = NO;
|
|
|
}
|
|
|
|
|
|
-(void)getHost {
|
|
|
//请求
|
|
|
- NSLog(@"请求一次post");
|
|
|
WeakSelf;
|
|
|
[[LXNetworkManager shareLXNetworkManager]postWithURLStr:[NSString stringWithFormat:@"%@jyapp/free/config",kHost] parameters:nil progress:^(NSProgress *progress) {
|
|
|
} success:^(id responseObject) {
|
|
@@ -102,7 +94,7 @@
|
|
|
[weakSelf registeredHost:pushGrpcServer];
|
|
|
}
|
|
|
} failure:^(NSError *error) {
|
|
|
-// NSLog(@"error=%@",error);
|
|
|
+ isRunning = NO;
|
|
|
}];
|
|
|
}
|
|
|
|
|
@@ -117,14 +109,15 @@
|
|
|
|
|
|
WeakSelf;
|
|
|
[self.service registerWithRequest:req handler:^(RegResp * _Nullable response, NSError * _Nullable error) {
|
|
|
- if(error==nil){
|
|
|
+ if(error==nil && response!=nil){
|
|
|
NSLog(@"注册接收成功:%@",response.pushId);
|
|
|
[weakSelf clearTimer];
|
|
|
[[NSUserDefaults standardUserDefaults] setValue:response.pushId forKey:GRPCPushID];
|
|
|
[GlobalData shareInstance].rpcPushID =response.pushId;
|
|
|
[weakSelf listeningRPC];
|
|
|
}else {
|
|
|
-// NSLog(@"注册失败:%@",error);
|
|
|
+ NSLog(@"注册失败:%@",error);
|
|
|
+ isRunning = NO;
|
|
|
}
|
|
|
}];
|
|
|
}
|
|
@@ -135,9 +128,9 @@
|
|
|
msg.pushId = [GlobalData shareInstance].rpcPushID;
|
|
|
WeakSelf;
|
|
|
[self.service pushMsgWithRequest:msg eventHandler:^(BOOL done, MsgResp * _Nullable response, NSError * _Nullable error) {
|
|
|
- if (error==nil) {
|
|
|
-// NSLog(@"收到消息数据无异常:response:%@",response);
|
|
|
- [weakSelf showLocalPushView:[self dealWithPushData:response]];
|
|
|
+ if (error==nil && response!=nil) {
|
|
|
+ NSLog(@"收到消息数据无异常:response:%@",response);
|
|
|
+ [weakSelf showLocalPushView:[weakSelf dealWithPushData:response]];
|
|
|
}else {
|
|
|
// NSLog(@"gprc接收接收错误:%@",error);
|
|
|
NSLog(@"尝试重连");
|
|
@@ -149,6 +142,8 @@
|
|
|
|
|
|
//构建info 推送数据
|
|
|
-(NSDictionary *)dealWithPushData:(MsgResp *)res {
|
|
|
+
|
|
|
+
|
|
|
NSDictionary *dict = @{
|
|
|
@"type":res.pushType,
|
|
|
@"msgType":[NSString stringWithFormat:@"%lld",res.msgType],//1 - 2
|
|
@@ -182,7 +177,7 @@
|
|
|
//判断第几个底部栏
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:kLoginOut object:[NSString stringWithFormat:@"%@",pushDict[@"info"][@"body"]] userInfo:@{@"item_index":[NSString stringWithFormat:@"%lu",(unsigned long)tab.selectedIndex]}];
|
|
|
}
|
|
|
- }else {
|
|
|
+ }else if (msgType==1){
|
|
|
self.msgIndex++;
|
|
|
if (@available(iOS 10.0, *)) {
|
|
|
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
|
|
@@ -210,6 +205,8 @@
|
|
|
notif.soundName = UILocalNotificationDefaultSoundName;
|
|
|
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
|
|
|
}
|
|
|
+ }else {
|
|
|
+ NSLog(@"其他... ...");
|
|
|
}
|
|
|
|
|
|
}
|