接着上次讲的socket服务器
,这次讲客户端的实现,比较简单,还是直接撸代码
.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吧,后加的
客户端没有写单独的demo,直接写在这个开源项目里了,因为读者可能经常采用MVC、MVP、MVVM这些设计模式,但本项目用了viper设计模式的部分功能,不知道大家能不能看懂
LWChatListVC里面右上角扫描按钮扫描可识别二维码然后自动连接你的桌面socket服务器,此时就可以聊天了
客户端demo地址👇这儿
喜欢的可以下载看看,登录密码我忘记了
可以在登录LoginPresenter里屏蔽掉密码输入
pragma mark 登录
- (void)loginAction{
if([LWUser shareInstance].userName.length && [LWUser shareInstance].password.length){
[self.interactor loginWithPhone:[LWUser shareInstance].userName password:[LWUser shareInstance].password success:^(NSDictionary *dict) {
[UIApplication sharedApplication].keyWindow.rootViewController = [[LWTabbarVC alloc]init];
} failure:^(NSError *error) {
}];
}
}