基于CocoaAsyncSocket 即時通訊(客戶端 服務(wù)器 心跳包)

這里簡單寫一下 基于CocoaAsyncSocket的即時通訊,包括心跳機制。超時服務(wù)器自動踢出消極客戶端。(服務(wù)器,客戶端分開工程寫)

一、服務(wù)器#####
1、新建一個類 SerViceAPP 用于開啟服務(wù)器
#import <Foundation/Foundation.h>
@interface SerViceAPP : NSObject
-(void)openSerVice; //開啟服務(wù)器
@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(@"端口開啟成功,并監(jiān)聽客戶端請求連接...");
    }else {
        NSLog(@"端口開啟失...");
    }
    //端口號port自己設(shè)置,動態(tài)端口的范圍從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]) {
             //群聊 發(fā)送給其他客戶端
             [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(@"當(dāng)前在線用戶個數(shù):%ld",self.arrayClient.count);
}

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
    NSLog(@"數(shù)據(jù)發(fā)送成功..");
}

- (void)writeDataWithSocket:(GCDAsyncSocket*)clientSocket str:(NSString*)str{
    [clientSocket writeData:[str dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
}

#pragma checkTimeThread
//這里設(shè)置35s檢查一次 數(shù)組里所有的客戶端socket 最后一次通訊時間,這樣的話會有周期差(最多差35s),可以設(shè)置為1s檢查一次,這樣頻率快
//開啟線程 啟動runloop 循環(huán)檢測客戶端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];
    //開啟線程發(fā)送心跳
    [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{
//        正常斷開
    }
   
}

//開啟不死線程不斷發(fā)送心跳

- (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 title="https://github.com/qhf012607/Socket">an example</a>

最后

可以通過使用終端 命令:telnet 192.168.1.1 8088(你的IP端口號)模擬沒有發(fā)送心跳的客戶端 多開幾個客戶端模擬群聊,服務(wù)器會自動踢出超時消極的客戶端.

覺得有用請給我一個贊!

謝謝#.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容