iOS使用socket实现聊天功能
2022-01-19 本文已影响0人
萤火驻守心间
关于GCDAsyncSocket实现即时通讯功能
客户端需要做的:
1、连接服务器,连接socket,发送心跳
2、编码数据包,发送消息给服务器
3、接收处理服务器返回的消息
4、提供手动断联socket方法,在需要的地方调用
5、socket非手动断联,重新请求连接
服务端需要做的:
1、用户心跳的维持和刷新
2、用户的调用
3、数据包的拆解和分发等
新建socket管理类BLSocketManager
.h文件
@interface BLSocketManager : NSObject
+ (instancetype)sharedManager;
- (void)disConnectClearTime;
//收到消息回调
@property (nonatomic, copy) void(^getChatData)(id result);//回调根据自身需求返回,此处为了便于查看,暂时使用id类型
//发送确认消息
- (void)sendSureDatawithFriend:(int)friendid;
@end
.m文件
#import "BLSocketManager.h"
#import <GCDAsyncSocket.h>
#import "NSMutableData+BLMutableData.h"//进行数据的编码和解码过程
@interface BLSocketManager ()<GCDAsyncSocketDelegate>
@property (strong, nonatomic) GCDAsyncSocket *gcdSocket;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) BOOL disconnect;
@property (nonatomic, strong) NSString *hostUrl;
@property (nonatomic, strong) NSString *hostCode;
@end
static BLSocketManager *userInfo = nil;
@implementation BLSocketManager
+ (instancetype)sharedManager {
static dispatch_once_t once;
dispatch_once(&once, ^{
userInfo = [[BLSocketManager alloc]init];
});
return userInfo;
}
//间隔5s发一次心跳,可根据实际需求自己调节
-(NSTimer *)timer{
if (!_timer) {
__weak typeof(self)weakSelf = self;
if (@available(iOS 10.0, *)) {
_timer = [NSTimer timerWithTimeInterval:5 repeats:true block:^(NSTimer * _Nonnull timer) {
[weakSelf heartBeat];
}];
} else {
_timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(heartBeat) userInfo:nil repeats:true];
}
}
return _timer;
}
//初始化socket,设置代理
-(GCDAsyncSocket *)gcdSocket{
if (!_gcdSocket) {
_gcdSocket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
}
return _gcdSocket;
}
//请求后台接口获取ip和端口,开始连接tcp
- (void)addConnectSendHeart{
NSString *urlString = @"https://im/route/selectServer";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"userId"] = @"";
dict[@"deviceId"] = @"";
// [MyRequest call:HTTPMethod_POST postWithUrl:urlString userDic:dict token:[BaseUserManager shareUser].token?[BaseUserManager shareUser].token:@"" Callback:^(BOOL isSuccess, NSDictionary *result) {
// if (isSuccess) {
// NSDictionary *datadic = result[@"data"];
// self.hostUrl = datadic[@"ip"];
// self.hostCode = datadic[@"serverPort"];
// [self startSocketConnect];
// }else{
// [SVProgressHUD showAutoInfo:@"IM服务器连接失败"];
// }
// }];
}
//开始连接
- (void)startSocketConnect{
[self.gcdSocket connectToHost:self.hostUrl onPort:[self.hostCode intValue] error:nil];
}
#pragma mark - GCDAsyncSocketDelegate
//当套接字连接并准备好读写时调用,添加timer到runLoop中定时发心跳,LoginMsgRequest为自定义数据类型,可根据自身需求定义协议算法
//写入数据到套接字,完成后调用读取数据didReadData的代理方法
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
//读取socket上第一个可用的字节,如果超时值为负数,读操作将不会使用超时值
[self.gcdSocket readDataWithTimeout:-1 tag:0];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
[self heartBeat];
NSData *yourdata = [NSData data];
//可将包含userid、deviceid、token和消息类型(code)的数据转化为data,然后按照协商好的方式进行编码
NSMutableData *temData = [NSMutableData encodeSocketProtoBufWithData:yourdata andCode:3];
[self.gcdSocket writeData:temData withTimeout:-1 tag:0];
}
//当套接字断开连接时调用
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(nullable NSError *)err {
//判断是否是手动断联,disconnect标记为手动断开socket
if (self.disconnect) {
}else{
if (err) {
if (self.hostUrl) {
[self addConnectSendHeart];
}
}
}
}
//当套接字完成读取请求数据时调用,可根据和后台定义的数据传输格式读取想获得的数据
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
[self.gcdSocket readDataWithTimeout:-1 tag:0];
__weak typeof(self)weakSelf = self;
[NSMutableData decodeSocketProtoBufData:data backContent:^(int procode, NSData * _Nonnull contentData) {
[weakSelf backChatMessageContentData:contentData proCode:procode];
}];
}
//前端后台协商的数据返回类型及处理
- (void)backChatMessageContentData:(NSData *)contentData proCode:(int)procode{
//procode为后台定义的消息类型,可根据code进行不同消息处理
if (procode == 11) {
//聊天消息
// DeliveryChatMsgRequest *requst = [DeliveryChatMsgRequest parseFromData:contentData error:nil];
//收到消息后的回调
if (self.getChatData) {
// self.getChatData(requst.chatMsg);
self.getChatData(contentData);
}
//给后台发送确认消息
NSData *yourdata = [NSData data];
NSMutableData *temData = [NSMutableData encodeSocketProtoBufWithData:yourdata andCode:5];
[self.gcdSocket writeData:temData withTimeout:-1 tag:0];
}else if (procode == 13){
//通知消息
}else if (procode == 5){
}
}
//发送确认消息,用于消息列表点击查看时调用,区分已读和未读消息
- (void)sendSureDatawithFriend:(int)friendid{
NSData *yourdata = [NSData data];
NSMutableData *temData = [NSMutableData encodeSocketProtoBufWithData:yourdata andCode:5];
[self.gcdSocket writeData:temData withTimeout:-1 tag:0];
}
//将"ping"和code按和后台协商的传输格式进行传输,发送心跳
- (void)heartBeat {
// HeartbeatMsgRequest *beatMsg = [[HeartbeatMsgRequest alloc]init];
// beatMsg.msg = @"ping";
NSData *yourData = [NSData data];
NSMutableData *temData = [NSMutableData encodeSocketProtoBufWithData:yourData andCode:1];
[self.gcdSocket writeData:temData withTimeout:-1 tag:0];
}
//当套接字完成请求数据时调用,如果有错误则不调用
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
[sock readDataWithTimeout:-1 tag:tag];
}
-(void)disConnectClearTime{
if (self.timer) {
[self.timer invalidate];
self.timer = nil;
}
self.disconnect = true;
[self.gcdSocket disconnect];
}
@end