美文网首页iOSiOS开发关注
基于CocoaAsyncSocket 即时通讯(客户端 服务器

基于CocoaAsyncSocket 即时通讯(客户端 服务器

作者: Yan青天 | 来源:发表于2016-09-08 11:52 被阅读2061次

    这里简单写一下 基于CocoaAsyncSocket的即时通讯,包括心跳机制。超时服务器自动踢出消极客户端。(服务器,客户端分开工程写)

    一、服务器#####
    1、新建一个类 SerViceAPP 用于开启服务器
    #import <Foundation/Foundation.h>
    @interface SerViceAPP : NSObject
    -(void)openSerVice; //开启服务器
    @end
    
    
    #import "SerViceAPP.h"
    #import "GCDAsyncSocket.h"
    #import "ClientObj.h"
    @interface SerViceAPP()<GCDAsyncSocketDelegate>
    
    @property(nonatomic, strong)GCDAsyncSocket *serve;
    @property(nonatomic, strong)NSMutableArray *arrayClient;
    @property(nonatomic, strong)NSThread *checkThread;
    @end
    
    @implementation SerViceAPP
    -(instancetype)init{
        if (self = [super init]) {
            self.serve = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)];
            //开启个不死线程不断检测所连接的每个客户端的心跳
            self.checkThread = [[NSThread alloc]initWithTarget:self selector:@selector(checkClientOnline) object:nil];
            [self.checkThread start];
        }
        
        return self;
    }
    -(NSMutableArray *)arrayClient{
        if (!_arrayClient) {
            _arrayClient = [NSMutableArray array];
        }
        
        return _arrayClient;
    }
    
    -(void)openSerVice{
        
        NSError *error;
       BOOL sucess = [self.serve acceptOnPort:8088 error:&error];
        if (sucess) {
            NSLog(@"端口开启成功,并监听客户端请求连接...");
        }else {
            NSLog(@"端口开启失...");
        }
        //端口号port自己设置,动态端口的范围从1024到65535,
    }
    
    #pragma delegate
    
    - (void)socket:(GCDAsyncSocket *)serveSock didAcceptNewSocket:(GCDAsyncSocket *)clientSocket{
        NSLog(@"%@ IP: %@: %zd 客户端请求连接...",clientSocket,clientSocket.connectedHost,clientSocket.connectedPort);
        // 1.将客户端socket保存起来
        ClientObj *client = [[ClientObj alloc]init];
        client.scocket = clientSocket;
        client.timeNew = [NSDate date];
        [self.arrayClient addObject:client];
        [clientSocket readDataWithTimeout:-1 tag:0];
    }
    
    - (void)socket:(GCDAsyncSocket *)clientSocket didReadData:(NSData *)data withTag:(long)tag  {
        NSString *clientStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",clientStr);
        NSString *log = [NSString stringWithFormat:@"IP:%@ %zd data: %@",clientSocket.connectedHost,clientSocket.connectedPort,clientStr];
       
        for (ClientObj *socket in self.arrayClient) {
             if (![clientSocket isEqual:socket.scocket]) {
                 //群聊 发送给其他客户端
                 [self writeDataWithSocket:socket.scocket str:log];
             }else{
                 ///更新最新时间
                 socket.timeNew = [NSDate date];
             }
        }
        [clientSocket readDataWithTimeout:-1 tag:0];
    }
    
    - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
        NSLog(@"又下线");
        NSMutableArray *arrayNew = [NSMutableArray array];
        for (ClientObj *socket in self.arrayClient ) {
            if ([socket.scocket isEqual:sock]) {
                continue;
            }
            [arrayNew addObject:socket   ];
        }
        self.arrayClient = arrayNew;
    }
    
    -(void)exitWithSocket:(GCDAsyncSocket *)clientSocket{
    //    [self writeDataWithSocket:clientSocket str:@"成功退出\n"];
    //    [self.arrayClient removeObject:clientSocket];
    //    
    //    NSLog(@"当前在线用户个数:%ld",self.arrayClient.count);
    }
    
    - (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
        NSLog(@"数据发送成功..");
    }
    
    - (void)writeDataWithSocket:(GCDAsyncSocket*)clientSocket str:(NSString*)str{
        [clientSocket writeData:[str dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
    }
    
    #pragma checkTimeThread
    //这里设置35s检查一次 数组里所有的客户端socket 最后一次通讯时间,这样的话会有周期差(最多差35s),可以设置为1s检查一次,这样频率快
    //开启线程 启动runloop 循环检测客户端socket最新time
    
    - (void)checkClientOnline{
        @autoreleasepool {
            [NSTimer scheduledTimerWithTimeInterval:35 target:self selector:@selector(repeatCheckClinetOnline) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop]run];
        }
    }
    
    //移除 超过心跳时差的 client
    - (void)repeatCheckClinetOnline{
        if (self.arrayClient.count == 0) {
            return;
        }
        NSDate *date = [NSDate date];
        NSMutableArray *arrayNew = [NSMutableArray array];
        for (ClientObj *socket in self.arrayClient ) {
            if ([date timeIntervalSinceDate:socket.timeNew]>30) {
                continue;
            }
            [arrayNew addObject:socket   ];
        }
        self.arrayClient = arrayNew;
    }
    @end
    
    
    
    #import <Foundation/Foundation.h>
    #import "GCDAsyncSocket.h"
    @interface ClientObj : NSObject
    @property(nonatomic, strong)GCDAsyncSocket *scocket;
    @property(nonatomic, strong)NSDate *timeNew;//更新最新通讯时间
    @end
    
    
    二、客户端#####
    #import "ViewController.h"
    #import "GCDAsyncSocket.h"
    @interface ViewController ()<GCDAsyncSocketDelegate>
    @property(nonatomic,strong)GCDAsyncSocket *socket;
    @property (weak, nonatomic) IBOutlet UITextField *mesage;
    @property (weak, nonatomic) IBOutlet UIButton *sender;
    @property (nonatomic, strong)NSThread *thread;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        GCDAsyncSocket *socket = [[GCDAsyncSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_global_queue(0, 0)];
        [socket connectToHost:@"192.168.1.129" onPort:8088 error:nil];
        
        self.socket = socket;
        [self.sender addTarget:self action:@selector(sendmessage:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    #pragma delegate
    
    - (void)sendmessage:(UIButton*)sender{
        [self.socket writeData:[self.mesage.text dataUsingEncoding:NSUTF8StringEncoding ] withTimeout:-1 tag:0];
        
    }
    
    - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
        NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSLog(@"%@",dataStr);
        [sock readDataWithTimeout:-1 tag:0];
    }
    
    - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port {
        NSLog(@"连接成功");
        
        [self.socket readDataWithTimeout:-1 tag:0];
        //开启线程发送心跳
        [self.thread start];
    }
    
    -(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
        NSLog(@"断开连接 %@",err);
        //再次可以重连
        if (err) {
    //         [self.socket connectToHost:sock.connectedHost onPort:sock.connectedPort error:nil];
        }else{
    //        正常断开
        }
       
    }
    
    //开启不死线程不断发送心跳
    
    - (void)threadStart{
        @autoreleasepool {
            [NSTimer scheduledTimerWithTimeInterval:29 target:self selector:@selector(heartBeat) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop]run];
        }
    }
    
    - (void)heartBeat{
        [self.socket writeData:[@"heart" dataUsingEncoding:NSUTF8StringEncoding ] withTimeout:-1 tag:0];
    }
    
    - (NSThread*)thread{
        if (!_thread) {
            _thread = [[NSThread alloc]initWithTarget:self selector:@selector(threadStart) object:nil];
        }
        return _thread;
    }
    
    
    

    git demo:
    <a href="https://github.com/qhf012607/Socket" title="https://github.com/qhf012607/Socket">an example</a>

    最后

    可以通过使用终端 命令:telnet 192.168.1.1 8088(你的IP端口号)模拟没有发送心跳的客户端 多开几个客户端模拟群聊,服务器会自动踢出超时消极的客户端.

    觉得有用请给我一个赞!

    谢谢#.

    相关文章

      网友评论

      • Eveloson:app进入background一段时间后socket直接断开了,怎么解决呢,还有,心跳包发送失败会进入socketDidDisconnect是吗
      • ab4c3e6b705e:请问SerVice APP端是跑的模拟器还是真机?那我怎么获得IP地址呢?我用模拟器跑的,用电脑的IP地址,连不起来
        Yan青天:@向东191 模拟器,客户端端口号 还和 服务端的保持一致
      • Rchongg:客户端直接和客户端通讯?
        Yan青天:@Rchongg 客户端A 发给服务端 服务端发个客户端B
      • Zd_silent:6666666
      • 18096d2feac6:写的确实很简单实用,真不错,赞
      • Alan_Sim:正好在找这个,写的简单易懂,太实用了,多谢大神~
      • mayChunJ:很好 很实用

      本文标题:基于CocoaAsyncSocket 即时通讯(客户端 服务器

      本文链接:https://www.haomeiwen.com/subject/qefsettx.html