前言
- 函数式+链式(以为例)
//例如
make.left.equalTo(self.view).offset(10)
//像这样一直,把需要执行的代码一直写下去,就可以称为链式。
//像这样通过去调用函数,返回自身,可以简单的理解为就是函数式。
- GCDAsyncSocket是一个写的很好的Socket框架,但是感觉使用代理写起来比较麻烦,另外基于学习函数式+链式编程,所以顺便简单封装一下GCDAsyncSocket库。写的不好的话,请大神指教。
进入主题
-
项目大致结构(还没有包含GCDAsyncSocket所有功能,但我会持续研究)
首先 pod 'CocoaAsyncSocket'
主要的内容(只贴.h文件,完整项目可以在本文最后调到Github下载)
//
// DVSocketManager.h
// DVSocketHelper
//
// Created by devil on 2019/6/24.
// Copyright © 2019 devil. All rights reserved.
//
#import
#import "DVSocketConfig.h"
NS_ASSUME_NONNULL_BEGIN
//错误回调
typedef void(^DVErrorCallBack)(DVActionCode code,NSString *msg);
//非错误、读取数据、写数据之外的所有回调
typedef void(^DVActionCallBack)(DVActionCode code,id socketManager,_Nullable id result);
//读取数据回调
typedef void(^DVReadDataCallBack)(DVActionCode code,id socketManager,NSUInteger partialLength,_Nullable id result,long tag);
//写数据回调
typedef void(^DVWriteDataCallBack)(DVActionCode code,id socketManager,NSUInteger partialLength,NSUInteger totalLength,long tag);
@interface DVSocketManager : NSObject
+(DVSocketManager *(^)(void)) instance;
//当前ClientSocket
-(id (^)(void)) currentClientSocket;
//设置连接地址和端口
-(DVSocketManager *(^)(NSString *address)) setAddress;
-(DVSocketManager *(^)(uint16_t port)) setPort;
//设置配置
-(DVSocketManager *(^)(DVSocketConfig *config)) setSocketConfig;
//编辑已有的配置
-(DVSocketManager *(^)(void(^editConfig)(DVSocketConfig *config))) editSocketConfig;
//设置错误回调
-(DVSocketManager *(^)(DVErrorCallBack errorCallBack)) setErrorCallBack;
//设置非错误回调
-(DVSocketManager *(^)(DVActionCallBack actionCallBack)) setActionCallBack;
//设置读取数据回调
-(DVSocketManager *(^)(DVReadDataCallBack readDataCallBack)) setReadDataCallBack;
//设置写数据回调
-(DVSocketManager *(^)(DVWriteDataCallBack writeDataCallBack)) setWriteDataCallBack;
//连接主机
-(DVSocketManager *(^)(void)) connect;
//作为主机监听端口
-(DVSocketManager *(^)(uint16_t port)) acceptOnPort;
//作为主机监听端口,端口为已设置的端口
-(DVSocketManager *(^)(void)) acceptOnCurrentSettingPort;
//作为主机监听端口
-(DVSocketManager *(^)(NSString *interface,uint16_t port)) acceptOnInterface;
//作为主机监听端口
-(DVSocketManager *(^)(NSURL *url,uint16_t port)) acceptOnUrl;
//读取数据(一次)
-(DVSocketManager *(^)(long tag)) readData;
//设置每一次成功发送完数据之后都读取数据
-(DVSocketManager *(^)(BOOL mark)) setReadDataWithAnySendData;
//写数据(content为NSData时默认为二进制流)
-(DVSocketManager *(^)(id content,long tag)) writeData;
//写文件数据
-(DVSocketManager *(^)(NSData *fileData,long tag)) writeFileWithData;
//写文件数据
-(DVSocketManager *(^)(NSString *path,long tag)) writeFileWithPath;
//关闭连接
-(DVSocketManager *(^)(void)) closeConnection;
//是否已连接
-(BOOL (^)(void)) isConnected;
@end
NS_ASSUME_NONNULL_END
- 配置
//
// DVSocketConfig.h
// DVSocketHelper
//
// Created by devil on 2019/7/4.
// Copyright © 2019 devil. All rights reserved.
//
#import
NS_ASSUME_NONNULL_BEGIN
//事件码
typedef NS_ENUM(NSUInteger, DVActionCode) {
//DVErrorCallBack中回调
DV_OtherError = 0,//其它错误
DV_ConnectTimeoutError = 1 << 0,//连接超时
DV_ConnectError = 1 << 1,//连接错误或者断开有错误
DV_InputError = 1 << 2,//输入数据错误
DV_WriteDataError = 1 << 3,//写数据时发生错误
//DVActionCallBack中回调
DV_Disconnect = 1 << 4,//正常手动断开连接
DV_ConnectSuccess = 1 << 5,//连接成功
DV_StartConnect = 1 << 6,//开始连接
DV_DidReceiveTrust = 1 << 7,
DV_DocketDidSecure = 1 << 8,
DV_SocketDidCloseReadStream = 1 << 9,
DV_DidAcceptNewSocket = 1 << 10,
//DVWriteDataCallBack中回调
DV_WriteFileData = 1 << 11,//写文件数据
DV_WriteFileDataSuccess = 1 << 12,//写文件数据成功
DV_WriteOtherData = 1 << 13,//写其它数据(字符串)
DV_WriteOtherDataSuccess = 1 << 14,//写数据成功
//DVReadDataCallBack中回调
DV_ReadData = 1 << 15,//读取数据
DV_ReadDataSuccess = 1 << 16//读取数据成功
};
@interface DVSocketConfig : NSObject
//连接超时时间(默认不限时-1)
@property (nonatomic,assign) NSTimeInterval connectTimeout;
//读取数据超时时间(默认不限时-1)
@property (nonatomic,assign) NSTimeInterval readDataTimeout;
//写数据超时时间(默认不限时-1)
@property (nonatomic,assign) NSTimeInterval writeDataTimeout;
//是否每次发送完数据之后都读取数据(默认NO)
@property (nonatomic,assign) BOOL isAfterAnySendDataToReadData;
//发送字符串数据的格式(每次发送都按照这个格式,只能包含一个%@,例如 @"||%@||",赋值时自动设置isOpenSendDataFormat为YES)
@property (nonatomic,copy) NSString *sendStringDataFormat;
//是否开启发送数据使用特定格式(默认不开启)
@property (nonatomic,assign) BOOL isOpenSendStringDataFormat;
+(instancetype) defaultConfig;
@end
NS_ASSUME_NONNULL_END
- 使用
DVSocketManager.instance()
.setAddress(@"127.0.0.1")
.setPort(9999)
.setReadDataWithAnySendData(YES)
.editSocketConfig(^(DVSocketConfig *config){
config.sendStringDataFormat = @"||%@||";
})
.setErrorCallBack(^(DVActionCode code,NSString *msg){
NSLog(@"错误-->code:%ld\tmsg:%@",(long)code,msg);
[self showMessage:msg];
})
.setActionCallBack(^(DVActionCode code,id socketManager,_Nullable id result){
NSLog(@"action-->%@",result);
if (code & DV_Disconnect) {
[self showMessage:@"断开连接成功"];
}else if (code & DV_ConnectSuccess){
[self showMessage:@"连接成功"];
}
})
.setReadDataCallBack(^(DVActionCode code,id socketManager,NSUInteger partialLength,_Nullable id result,long tag){
if (code & DV_ReadDataSuccess) {
NSString *read = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(@"read-->%@",read);
self.txt_readContent.text = [NSString stringWithFormat:@"%@\n服务器:%@",self.txt_readContent.text,read];
}
})
.setWriteDataCallBack(^(DVActionCode code,id socketManager,NSUInteger partialLength,NSUInteger totalLength,long tag){
if (code & DV_WriteFileData) {
NSLog(@"文件发送进度-->%.2f",partialLength*1.0/totalLength);
}else if(code & DV_WriteFileDataSuccess){
NSLog(@"文件发送成功");
}else if(code & DV_WriteOtherDataSuccess){
NSLog(@"write-->code:%ld\tresult:%lu",(long)code,(unsigned long)totalLength);
NSString *text = self.txt_content.text;
self.txt_readContent.text = [NSString stringWithFormat:@"%@\n我:%@",self.txt_readContent.text,text];
self.txt_content.text = @"";
}
}).connect();