欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS使用GCDSocketManager实现长连接的方法

程序员文章站 2023-12-17 11:07:10
.h文件 #import #import "gcdasyncsocket.h" @interf...

.h文件

#import <foundation/foundation.h>
#import "gcdasyncsocket.h"
@interface gcdsocketmanager : nsobject
@property(nonatomic,strong) gcdasyncsocket *socket;
//单例
+ (instancetype)sharedsocketmanager;
//连接
- (void)connecttoserver;
//断开
- (void)cutoffsocket;
@end

.m文件

#import "gcdsocketmanager.h"
#define sockethost @"地址"
#define socketport 端口
@interface gcdsocketmanager()<gcdasyncsocketdelegate>
//握手次数
@property(nonatomic,assign) nsinteger pushcount;
//断开重连定时器
@property(nonatomic,strong) nstimer *timer;
//重连次数
@property(nonatomic,assign) nsinteger reconnectcount;
@end
@implementation gcdsocketmanager
//全局访问点
+ (instancetype)sharedsocketmanager {
  static gcdsocketmanager *_instance = nil;
  static dispatch_once_t oncetoken;
  dispatch_once(&oncetoken, ^{
    _instance = [[self alloc] init];
  });
  return _instance;
}
//可以在这里做一些初始化操作
- (instancetype)init
{
  self = [super init];
  if (self) {
    
  }
  return self;
}
#pragma mark 请求连接
//连接
- (void)connecttoserver {
  self.pushcount = 0;
  
  self.socket = [[gcdasyncsocket alloc] initwithdelegate:self delegatequeue:dispatch_get_main_queue()];
  
  nserror *error = nil;
  [self.socket connecttohost:sockethost onport:socketport error:&error];
  
  if (error) {
    dlog(@"socketconnecterror:%@",error);
  }
}
#pragma mark 连接成功
//连接成功的回调
- (void)socket:(gcdasyncsocket *)sock didconnecttohost:(nsstring *)host port:(uint16_t)port {
  dlog(@"socket连接成功");
  [self senddatatoserver];
}
//连接成功后向服务器发送数据
- (void)senddatatoserver {
  //发送数据代码省略...
  
  //发送
  [self.socket writedata:jsondata withtimeout:-1 tag:1];
  
  //读取数据
  [self.socket readdatawithtimeout:-1 tag:200];
}
//连接成功向服务器发送数据后,服务器会有响应
- (void)socket:(gcdasyncsocket *)sock didreaddata:(nsdata *)data withtag:(long)tag {
  
  [self.socket readdatawithtimeout:-1 tag:200];
  
  //服务器推送次数
  self.pushcount++;
  
  //在这里进行校验操作,情况分为成功和失败两种,成功的操作一般都是拉取数据
}
#pragma mark 连接失败
//连接失败的回调
- (void)socketdiddisconnect:(gcdasyncsocket *)sock witherror:(nserror *)err {
  dlog(@"socket连接失败");
  
  self.pushcount = 0;
  
  nsuserdefaults *userdefaults = [nsuserdefaults standarduserdefaults];
  
  nsstring *currentstatu = [userdefaults valueforkey:@"statu"];
  
  //程序在前台才进行重连
  if ([currentstatu isequaltostring:@"foreground"]) {
    
    //重连次数
    self.reconnectcount++;
    
    //如果连接失败 累加1秒重新连接 减少服务器压力
    nstimer *timer = [nstimer scheduledtimerwithtimeinterval:1.0 * self.reconnectcount target:self selector:@selector(reconnectserver) userinfo:nil repeats:no];
    
    self.timer = timer;
  }
}
//如果连接失败,5秒后重新连接
- (void)reconnectserver {
  
  self.pushcount = 0;
  
  self.reconnectcount = 0;
  
  //连接失败重新连接
  nserror *error = nil;
  [self.socket connecttohost:sockethost onport:socketport error:&error];
  if (error) {
    dlog(@"socektconnecterror:%@",error);
  }
}
#pragma mark 断开连接
//切断连接
- (void)cutoffsocket {
  dlog(@"socket断开连接");
  
  self.pushcount = 0;
  
  self.reconnectcount = 0;
  
  [self.timer invalidate];
  self.timer = nil;
  
  [self.socket disconnect];
}
@end

以上这篇ios使用gcdsocketmanager实现长连接的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: