iOS通过SocketRocket实现websocket的即时聊天

最近项目中要做即时通讯功能, 但没打算使用环信,融云等三方平台, 我查了一下资料,ios端实现起来还是比较简单的 ,我也写了一个小demo,和大家分享一下

首先到getHub上下载一个 FaceBook的 SocketRocket, 然后倒入工程; 我是直接使用 pod导入SocketRocket

首先pod导入SocketRocket

platform :ios, '8.0'
pod 'SocketRocket', '~> 0.5.0'

在控制器中导入头文件

#import <SocketRocket/SRWebSocket.h>

然后就可以使用webSocket了, 其实里面的东西也不是很多, 主要是: 1.打开连接 2.关闭连接 3.发送消息 4.接收消息;

简单粗暴的直接上代码吧

#import "ViewController.h"

#import "Masonry.h"                  // 实现自动布局的 
#import <SocketRocket/SRWebSocket.h>

#define HHMainScreenWidth [UIScreen mainScreen].bounds.size.width
#define HHMainScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<SRWebSocketDelegate, UITextFieldDelegate, UITextViewDelegate>

@property(nonatomic, strong)UITextView *shouField;
@property(nonatomic, strong)UITextField *FaField;

@property(nonatomic, strong)SRWebSocket *webSocket;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createView];

}


// 启动 webSocket
- (void)setSocket {
    _webSocket.delegate = nil;
    [_webSocket close];
//    _webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://114.55.57.51:8282"]]];
    _webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://10.220.117.205:8081/websocket"]]];
    _webSocket.delegate = self;
    NSLog(@"Opening Connection...");
    [_webSocket open];
}

// 协议方法 链接成功 给服务器发送id
- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
    NSLog(@"Websocket Connected");
//      如果需要发送数据到服务器使用下面代码
     NSError *error;
     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:@{@"id":@"chat",@"clientid":@"hxz",@"to":@""} options:NSJSONWritingPrettyPrinted error:&error];
     NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
     [webSocket send:jsonString];
}

// 协议方法  接收消息
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message {
    NSLog(@"接收的消息:%@", message);
    self.shouField.text = message;
}

// 协议方法 Websocket 打开失败
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
    NSLog(@":( Websocket 打开失败 %@", error);
    webSocket = nil;
    // 断开连接后每过1s重新建立一次连接
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self setSocket];
    });
}


// 连接关闭
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
    NSLog(@"WebSocket closed, reason:%@", reason);
    webSocket = nil;

}


- (void)sendButAction {
    NSLog(@"发送");
    if (self.FaField.text.length > 0) {
       [_webSocket send:self.FaField.text];
    } else {
        NSLog(@"输入为空");
    }
}

- (void)openButAction {
    NSLog(@"打开");
    [self setSocket];   // 启动 webSocket
}

- (void)offButAction {
    NSLog(@"关闭");
    _webSocket.delegate = nil;
    [_webSocket close];
}


-(void)createView {

    UILabel *jieshouLabel = [UILabel new];
    jieshouLabel.text = @"接收:";
    jieshouLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:jieshouLabel];
    [jieshouLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(10);
        make.height.mas_equalTo(40);
        make.left.equalTo(self.view).offset(10);
    }];


    UITextView *shouField = [UITextView new];
    shouField.delegate = self;
    shouField.textColor = [UIColor whiteColor];
    shouField.backgroundColor = [UIColor grayColor];
    shouField.returnKeyType = UIReturnKeySearch;
    shouField.font = [UIFont systemFontOfSize:13];
    shouField.layer.masksToBounds = YES;
    shouField.layer.cornerRadius = 5;
    [self.view addSubview:shouField];
    [shouField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(jieshouLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(140);
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
    }];
    self.shouField = shouField;


    UILabel *faSongLabel = [UILabel new];
    faSongLabel.text = @"发送:";
    faSongLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:faSongLabel];
    [faSongLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(shouField.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
        make.left.equalTo(self.view).offset(10);
    }];

    // 发
    UITextField *FaField = [UITextField new];
    FaField.delegate = self;
    FaField.textColor = [UIColor whiteColor];
    FaField.backgroundColor = [UIColor grayColor];
    FaField.returnKeyType = UIReturnKeySearch;
    FaField.font = [UIFont systemFontOfSize:13];
    FaField.layer.masksToBounds = YES;
    FaField.layer.cornerRadius = 5;
    [self.view addSubview:FaField];
    [FaField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(faSongLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(40);
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
    }];
    self.FaField = FaField;

    UIButton *but = [UIButton buttonWithType: UIButtonTypeCustom];
    but.backgroundColor = [UIColor greenColor];
    [but setTitle:@"发送" forState:UIControlStateNormal];
    but.layer.masksToBounds = YES;
    but.layer.cornerRadius = 5;
    [but addTarget:self action:@selector(sendButAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:but];
    [but mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(HHMainScreenWidth - 60);
        make.top.equalTo(FaField.mas_bottom).offset(20);

    }];


    // 打开
    UIButton *openBut = [UIButton buttonWithType: UIButtonTypeCustom];
    openBut.backgroundColor = [UIColor orangeColor];
    [openBut setTitle:@"打开" forState:UIControlStateNormal];
    openBut.layer.masksToBounds = YES;
    openBut.layer.cornerRadius = 5;
    [openBut addTarget:self action:@selector(openButAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:openBut];
    [openBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
        make.top.equalTo(but.mas_bottom).offset(20);

    }];


    // 关闭
    UIButton *offBut = [UIButton buttonWithType: UIButtonTypeCustom];
    offBut.backgroundColor = [UIColor orangeColor];
    [offBut setTitle:@"关闭" forState:UIControlStateNormal];
    offBut.layer.masksToBounds = YES;
    offBut.layer.cornerRadius = 5;
    [offBut addTarget:self action:@selector(offButAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:offBut];
    [offBut mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-20);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
        make.top.equalTo(but.mas_bottom).offset(20);

    }];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

要实现给好友,群主发消息等功能,需要后台做写工作, 前端再和后台约定一下用户ID什么的, 前端这边工作量不大,实现起来还是比较简单的;

猜你喜欢

转载自blog.csdn.net/Jesse0308/article/details/72303075