第一部分:GCDAsyncSocket在单例模式中的创建
因为项目中只能存在一个socket长连接,所以我们可以在单例模式中创建和使用它们。
如下代码:
SocketManager.h 文件
#import <Foundation/Foundation.h>
#import "GCDAsyncSocket.h"
/*
*block类型 用于获取服务器返回的数据,适用于身份证识别模块
*/
typedef void(^SocketReturnMessage)(NSString *message);
/*
*用于人脸比对模块
*/
typedef void(^FaceCompareTaskBlock)(NSData *data,NSInteger tag,NSString *message,GCDAsyncSocket *socket);
/*
*监听是否断开连接
*/
typedef void(^DidDisconnectWithHostBlock)();
/*
*GCDAsyncSocketDelegate为GCDAsyncSocket的代理协议
*/
@interface SocketManager : NSObject<GCDAsyncSocketDelegate>
/*
*创建GCDAsyncSocket tcp的socket对象
*/
@property (nonatomic,strong) GCDAsyncSocket *socket;
/*
*主机端口
*/
@property (nonatomic,assign) NSUInteger port;
/*
*创建SocketReturnMessage类型的block对象
*/
@property (nonatomic,copy) SocketReturnMessage socketMessageBlock;
/*
*SocketReturnMessage类型block的方法
*/
- (void)retumessageWithBlock:(SocketReturnMessage)socketMessageBlock;
/*
*意思同上
*/
@property(nonatomic,copy)FaceCompareTaskBlock faceCompareTask;
- (void)didFinishFaceCompareTaskWithBlock:(FaceCompareTaskBlock)faceCompareTask;
/*
*同上
*/
@property (nonatomic,copy)DidDisconnectWithHostBlock disconnectWithHost;
- (void)didDisconnectWithHostBlock:(DidDisconnectWithHostBlock)disconnectWithHost;
/*
*初始化方法
*/
+ (SocketManager *)shareManager;
/*
*连接服务/主机的方法
*/
- (void)connectToHostWithPort:(NSUInteger)port;
@end
SocketManager.m 文件
#import "SocketManager.h"
#define ConnectTime -1
@implementation SocketManager
#pragma mark 伪单例模式
+ (SocketManager *)shareManager
{
static SocketManager *shareManager = nil;
static dispatch_once_t onecToken;
dispatch_once(&onecToken, ^{
shareManager = [[SocketManager alloc]init];
});
return shareManager;
}
#pragma mark连接服务器
- (void)connectToHostWithPort:(NSUInteger)port
{
if (self.socket == nil || [self.socket isDisconnected])
{
self.socket = [[GCDAsyncSocket alloc]init];
}
self.socket.delegate = self;
self.socket.delegateQueue = dispatch_get_main_queue();
NSError *error = nil;
BOOL isConnectHost = [self.socket connectToHost:@"***.***.***.***"
onPort:(uint16_t)port
withTimeout:ConnectTime
error:&error];
if (isConnectHost == NO)//没有链接到服务器
{
NSLog(@"ERROR:%@",error.description);
}
}
#pragma mark 已连接到服务器
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port
{
NSString *message = [NSString stringWithFormat:@"DidConnectToHost.Host:%@--Port:%hu",host,port];
self.socketMessageBlock(message);
}
#pragma mark 连接失败,可以在这里设置重连
- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
NSString *message = [NSString stringWithFormat:@"SocketDidDisconnect.ERROR:%@\n",err.description];
self.socketMessageBlock(message);
self.disconnectWithHost();
//TODO:设置重连
}
#pragma mark 长时间没有接收到数据
- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
NSString *message = [NSString stringWithFormat:@"DidAcceptNewSocket:%@",newSocket];
self.socketMessageBlock(message);
}
#pragma mark 服务器返回数据
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{
[sock readDataWithTimeout:-1 tag:0];
NSString *message = [NSData dataWithGBKEncodingOfData:data];
self.socketMessageBlock(message);
self.faceCompareTask(data, tag, message, sock);
}
#pragma mark 服务器返回的数据段长度
- (void)socket:(GCDAsyncSocket *)sock didReadPartialDataOfLength:(NSUInteger)partialLength tag:(long)tag
{
NSString *message = [NSString stringWithFormat:@"DidReadPartialDataOfLength:%lu",(unsigned long)partialLength];
self.socketMessageBlock(message);
}
#pragma mark 已经向服务器发送数据
- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
NSString *message = [NSString stringWithFormat:@"DidWriteDataWithTag:%ld",tag];
self.socketMessageBlock(message);
}
- (void)socket:(GCDAsyncSocket *)sock didWritePartialDataOfLength:(NSUInteger)partialLength tag:(long)tag
{
NSString *message = [NSString stringWithFormat:@"DidWritePartialDataOfLength:%lu",(unsigned long)partialLength];
self.socketMessageBlock(message);
}
- (void)retumessageWithBlock:(SocketReturnMessage)socketMessageBlock
{
self.socketMessageBlock = socketMessageBlock;
}
- (void)didFinishFaceCompareTaskWithBlock:(FaceCompareTaskBlock)faceCompareTask
{
self.faceCompareTask = faceCompareTask;
}
- (void)didDisconnectWithHostBlock:(DidDisconnectWithHostBlock)disconnectWithHost
{
self.disconnectWithHost = disconnectWithHost;
}
说明:
1.将超时时间设置为
-1
,可以一直保持连接,直到我们获取到想要的数据后手动断开连接。
2.将connectToHostWithPort :
方法提取出来,是为了方便我们断开后重新连接。
第二部分:GCDAsyncSocket的使用
因为我们在使用socket时用到了拼接数据,所以在第二部分中我们先说一下OC 中的data
转byte[]数组
。
1.data
转byte[]
NSData *data = UIImageJPEGRepresentation(faceImg, 1.0);
Byte *imageBytes = (Byte *)[data bytes ];
2.添加拼接数据
在添加拼接数据时,主要用到了NSMutableData
类中的- (void)appendData:(NSData *)other;
方法。举个例子,如下:
[responseBytes appendData:[NSData dataWithBytes:cardNumberBytes length:cardNumberLength]];
responseBytes是一个NSMutableData类的对象,cardNumberLength是该数据的长度。
3.socket中大端字节序和小端字节序的问题
关于大端字节序和小端字节序的问题可以参考:轻松记住大端小端的含义(附对大端和小端的解释) 和详解大端模式和小端模式
下面来看一下我项目中使用到的的转换大小端的方法:
+ (char *)toLittleBytesWithInt:(int)length buffer:(char *)buffer
{
for (int i = 0; i < 4; i++)
{
buffer[i] = (UInt8)0xff;
}
buffer[0] = (UInt8)(length & 0xff);
buffer[1] = (UInt8)(length >> 8 & 0xff);
buffer[2] = (UInt8)(length >> 16 & 0xff);
buffer[3] = (UInt8)(length >> 24 & 0xff);
return buffer;
}
+ (int) fromLittleBytesWithBytes:(unsigned char*) pdata{
int ndata3 = pdata[3] << 24;
int ndata2 = pdata[2] << 16;
int ndata1 = pdata[1] << 8;
int ndata0 = pdata[0] ;
int nlenght = ndata3 + ndata2 + ndata1 + ndata0;
return nlenght;
}
4.向服务器发送数据和读取服务器返回数据
[self.manager.socket writeData:responseBytes withTimeout:-1 tag:0];
[self.manager.socket readDataToLength:4 withTimeout:-1 tag:0];
//解析服务器返回数据
[self.manager didFinishFaceCompareTaskWithBlock:^(NSData *data, NSInteger tag, NSString *message, GCDAsyncSocket *socket) {
}];
我也是在一步一步地连接和使用socket,如有不足的地方,还请各位多多指教。