接着上次讲的socket服务器 http://www.jianshu.com/p/81fd2464b14c
,这次讲客户端的实现,比较简单,还是直接撸代码
客户端
.h文件
#import <Foundation/Foundation.h>
typedef void (^STRINGBLOCK)(NSString *str);
@class GCDAsyncSocket;
@interface LWSocketClient : NSObject
//接收消息block
@property (nonatomic,copy)STRINGBLOCK receivedMessageBlock;//接收的消息回调
//设置主机ip和port
- (void)connectHost:(NSString *)host port:(uint16_t)port;
//发送消息
- (void)sendmessage:(NSString*)message;
@end
.m实现
#import "LWSocketClient.h"
#import "GCDAsyncSocket.h"
@interface LWSocketClient ()<GCDAsyncSocketDelegate>{
BOOL _isServer;
}
@property(nonatomic,strong)GCDAsyncSocket *socket;//socket
@property (nonatomic, strong)NSThread *thread; //心跳线程
@end
@implementation LWSocketClient
/*
-(void)becoMeServer{
_isServer = YES;
[self.thread start];
}
*/
#pragma mark socket 实体
-(GCDAsyncSocket *)socket{
return _socket = _socket?:[[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)];
}
#pragma mark连接服务器端口和ip
- (void)connectHost:(NSString *)host port:(uint16_t)port{
[self.socket connectToHost:host onPort:port error:nil];
}
#pragma mark 连接成功
- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
NSLog(@"连接成功");
[self.socket readDataWithTimeout:-1 tag:0];
//开启线程发送心跳
[self.thread start];
}
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"\n收到消息:%@",dataStr);
self.receivedMessageBlock?self.receivedMessageBlock(dataStr):nil;
[sock readDataWithTimeout:-1 tag:0];
}
#pragma mark 断开连接
-(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
NSLog(@"断开连接 %@",err);
//再次可以重连
if (err) {
[self connectHost:sock.connectedHost port:sock.connectedPort];
}
else{//正常断开
}
}
#pragma mark 消息发送
- (void)sendmessage:(NSString*)message{
if(self.socket.isDisconnected){
[self connectHost:self.socket.connectedHost port:self.socket.connectedPort];
}
[self.socket writeData:[message dataUsingEncoding:NSUTF8StringEncoding ] withTimeout:-1 tag:0];
}
#pragma mark 开启心跳定时发送
- (void)threadStart{
@autoreleasepool {
[NSTimer scheduledTimerWithTimeInterval:25 target:self selector:@selector(heartBeat) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]run];
}
}
#pragma mark 心跳信息
- (void)heartBeat{
[self sendmessage:@"heart"];
}
#pragma mark 心跳线程
- (NSThread*)thread{
if (!_thread) {
_thread = [[NSThread alloc]initWithTarget:self selector:@selector(threadStart) object:nil];
}
return _thread;
}
@end
调用调用调用调用调用调用调用调用~
#pragma mark 懒加载socket
-(LWSocketClient *)socketClient{
return _socketClient= _socketClient?:[[LWSocketClient alloc]init];
}
#pragma mark 连接服务器并处理服务器的消息block
- (void)connectHost:(NSString *)host port:(uint16_t)port receivedMessageBlock:(VOIDBLOCK)receivedMessage{
[self.socketClient connectHost:host port:port];
__weak typeof (&*self)weakSelf = self;
self.socketClient.receivedMessageBlock = ^(NSString *message){
};
}
#pragma mark 发送消息
- (void)sendmessage:(NSString*)message{
[self.socketClient sendmessage: message];
}
二维码生成直接看demo吧,后加的
网友评论