您的位置:首页 > 理论基础 > 计算机网络

iOS开发——网络编程OC篇&GCDAsyncSocket编程

2015-07-29 17:59 507 查看
GCDAsyncSocket编程

同上一篇文章一样,这里也是使用Socket实现一个聊天室,但是这里使用的是一个常用的框架实现的:GCDAsyncSocket

一:导入这个框架

二:声明这个Socket的成员变量,定义一个消息数组  

1 @interface ViewController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate,GCDAsyncSocketDelegate>{
2
3     GCDAsyncSocket *_socket;
4 }
5 @property (weak, nonatomic) IBOutlet NSLayoutConstraint *inputViewConstraint;
6 @property (weak, nonatomic) IBOutlet UITableView *tableView;
7
8 @property (nonatomic, strong) NSMutableArray *chatMsgs;//聊天消息数组
9
10 @end


懒加载消息数组

1 -(NSMutableArray *)chatMsgs{
2     if (!_chatMsgs) {
3         _chatMsgs = [NSMutableArray array];
4     }
5
6     return _chatMsgs;
7 }


三:链接服务器

1 - (IBAction)connectToHost:(id)sender {
2     // 1.建立连接
3     NSString *host = @"127.0.0.1";
4     int port = 12345;
5
6     // 创建一个Socket对象
7     _socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
8
9     // 连接
10     NSError *error = nil;
11     [_socket connectToHost:host onPort:port error:&error];
12     if (error) {
13         NSLog(@"%@",error);
14     }
15 }


链接与断开连接的代理方法

1 #pragma mark -AsyncSocket的代理
2 #pragma mark 连接主机成功
3 -(void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
4     NSLog(@"连接主机成功");
5 }
6
7 #pragma mark 与主机断开连接
8 -(void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err{
9
10     if(err){
11         NSLog(@"断开连接 %@",err);
12     }
13 }


四:登陆

1 - (IBAction)loginBtnClick:(id)sender {
2
3     // 登录
4     // 发送用户名和密码
5     // 在这里做的时候,只发用户名,密码就不用发送
6
7     // 如果要登录,发送的数据格式为 "iam:zhangsan";
8     // 如果要发送聊天消息,数据格式为 "msg:did you have dinner";
9
10     //登录的指令
11     NSString *loginStr = @"iam:zhangsan";
12
13     //把Str转成NSData
14     NSData *data = [loginStr dataUsingEncoding:NSUTF8StringEncoding];
15
16
17     //[_outputStream write:data.bytes maxLength:data.length];
18     // 发送登录指令给服务
19     [_socket writeData:data withTimeout:-1 tag:101];
20 }


五:发送数据

1 -(BOOL)textFieldShouldReturn:(UITextField *)textField{
2
3     NSString *text = textField.text;
4
5     NSLog(@"%@",text);
6     // 聊天信息
7     NSString *msgStr = [NSString stringWithFormat:@"msg:%@",text];
8
9     //把Str转成NSData
10     NSData *data = [msgStr dataUsingEncoding:NSUTF8StringEncoding];
11
12     // 刷新表格
13     [self reloadDataWithText:msgStr];
14
15     // 发送数据
16     [_socket writeData:data withTimeout:-1 tag:102];
17
18     // 发送完数据,清空textField
19     textField.text = nil;
20
21     return YES;
22 }
23
24 #pragma mark 数据成功发送到服务器
25 -(void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
26     NSLog(@"数据成功发送到服务器");
27     //数据发送成功后,自己调用一下读取数据的方法,接着_socket才会调用下面的代理方法
28     [_socket readDataWithTimeout:-1 tag:tag];
29 }
30
31 #pragma mark 服务器有数据,会调用这个方法
32 -(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
33     // 从服务器接收到的数据
34     NSString *recStr =  [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
35
36     NSLog(@"%@ %ld %@",[NSThread currentThread],tag, recStr);
37
38     if (tag == 102) {//聊天返回的数据
39         // 刷新表格
40         [self reloadDataWithText:recStr];
41     }
42 //    }else if(tag == 101 ){//登录返回数据,不应该把数据添加到表格里
43 //
44 //
45 //    }
46
47 }


六:加载聊天数据到界面,并且实现滚动到对应的位置,键盘的键盘

1 -(void)reloadDataWithText:(NSString *)text{
2     [self.chatMsgs addObject:text];
3
4     // UI刷新要主线程
5     dispatch_async(dispatch_get_main_queue(), ^{
6         [self.tableView reloadData];
7
8         // 数据多,应该往上滚动
9         NSIndexPath *lastPath = [NSIndexPath indexPathForRow:self.chatMsgs.count - 1 inSection:0];
10         [self.tableView scrollToRowAtIndexPath:lastPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
11     });
12
13 }
14
15 #pragma mark 表格的数据源
16
17 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
18     return self.chatMsgs.count;
19 }
20
21
22 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
23 {
24     static NSString *ID = @"Cell";
25     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
26
27     cell.textLabel.text = self.chatMsgs[indexPath.row];
28
29     return cell;
30 }
31
32 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
33     [self.view endEditing:YES];
34 }


键盘处理

1
2     // 监听键盘
3     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbFrmWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
4 }
5
6
7 -(void)kbFrmWillChange:(NSNotification *)noti{
8     NSLog(@"%@",noti.userInfo);
9
10     // 获取窗口的高度
11
12     CGFloat windowH = [UIScreen mainScreen].bounds.size.height;
13
14
15
16     // 键盘结束的Frm
17     CGRect kbEndFrm = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
18      // 获取键盘结束的y值
19     CGFloat kbEndY = kbEndFrm.origin.y;
20
21
22     self.inputViewConstraint.constant = windowH - kbEndY;
23 }


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: