【IM-04】iOS端单聊&群聊
2019-12-23 本文已影响0人
Geeks_Chen
前沿
本章主要介绍如何在iOS端实现单聊和群聊,服务端是本地搭建,数据库使用mysql。
1.app演示
才知道简书不支持video
444用户登录.jpeg
444用户选择聊天对象.jpeg
444聊天.jpeg
333登录.png
333选择对象.png
333聊天.png
2.单聊实现工程
2.1.登录
[[SocketIOManager sharedInstance] emitOrder:@"login" with:@{@"userID":self.userField.text,@"type":@"chat"}];
2.2.选择好友
[[SocketIOManager sharedInstance] emitOrder:@"userList" with:@{}];
2.3.聊天
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:self.toUser forKey:@"toUser"];
[dic setValue:self.currentUser forKey:@"fromUser"];
[dic setValue:self.msgField.text forKey:@"msg"];
[dic setValue:@"chat" forKey:@"type"];
[dic setValue:@"" forKey:@"ext"];
//发送order
[[SocketIOManager sharedInstance] emitOrder:@"sendMsg" with:dic];
3.群聊实现过程
3.1.登录
[[SocketIOManager sharedInstance] emitOrder:@"login" with:@{@"userID":self.userField.text,@"type":@"roomChat"}];
3.2.获取房间列表
[[SocketIOManager sharedInstance] emitOrder:@"roomList" with:@{}];
[[SocketIOManager sharedInstance] onOrder:@"roomList" completion:^(id _Nonnull response) {
NSArray *roomList = response[0][@"roomList"];
if (roomList.count) {
[roomList enumerateObjectsUsingBlock:^(NSMutableDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"=====%@",obj);
if ([obj[@"roomid"] intValue] != 0) {
NSInteger index = [obj[@"roomid"] intValue]-1;
NSMutableDictionary *dd = self.roomListArray[index];
[dd setValue:obj[@"number"] forKey:@"number"];
}
}];
[self.roomListTableView reloadData];
[self.roomListTableView.mj_header endRefreshing];
}else {
NSLog(@"没有用户");
}
}];
3.3.选择房间
[[SocketIOManager sharedInstance] emitOrder:@"join" with:@{@"userID":self.uid,@"roomid":info[@"roomId"]}];
[[SocketIOManager sharedInstance] onOrder:@"join" completion:^(id _Nonnull response) {
NSString *msg = response[0][@"msg"];
if ([msg isEqualToString:@"1"]) {
RoomChatViewController *roomChatVC = [RoomChatViewController new];
roomChatVC.roomid = response[0][@"roomid"];
roomChatVC.uid = self.uid;
[self.navigationController pushViewController:roomChatVC animated:YES];
}else {
NSLog(@"用户进入房间失败");
}
}];
3.4.开始聊天
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setValue:@"" forKey:@"toUser"];
[dic setValue:self.uid forKey:@"fromUser"];
[dic setValue:self.msgField.text forKey:@"msg"];
[dic setValue:@"roomChat" forKey:@"type"];
[dic setValue:@"" forKey:@"ext"];
if (self.msgField.text.length) {
//发送order
[[SocketIOManager sharedInstance] emitOrder:@"sendMsg" with:dic];
}else {
NSLog(@"不能发送空消息");
}
[[SocketIOManager sharedInstance] onOrder:@"sendMsg" completion:^(id _Nonnull response) {
if (response) {
MessageModel *model = [[MessageModel alloc] init];
model.toUser = response[0][@"toUser"];
model.fromUser = response[0][@"fromUser"];
model.msg = response[0][@"msg"];
model.type = response[0][@"type"];
model.ext = response[0][@"ext"];
[self.dataArray addObject:model];
[self.chatTableView reloadData];
[self scrollTableViewToBottom];
self.msgField.text = @"";
}
}];
不管大佬们关不关注公众号,我都会放上本章的Demo