Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
galaxy-iOS
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mobile-group
galaxy-iOS
Commits
759a41ad
Commit
759a41ad
authored
Nov 26, 2024
by
David黄金龙
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
聊天室 相关功能
parent
49ea243c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
246 additions
and
145 deletions
+246
-145
YHIMTestViewController.swift
...vestreamSales(直播销售)/IM(环信)/C/YHIMTestViewController.swift
+246
-145
No files found.
galaxy/galaxy/Classes/Modules/LivestreamSales(直播销售)/IM(环信)/C/YHIMTestViewController.swift
View file @
759a41ad
...
...
@@ -11,7 +11,7 @@ import HyphenateChat
class
YHIMTestViewController
:
YHBaseViewController
{
private
let
roomID
:
String
=
"26523
1954673700
"
private
let
roomID
:
String
=
"26523
9166779417
"
private
let
yHID
:
String
=
"1040217809134595"
...
...
@@ -21,182 +21,273 @@ class YHIMTestViewController: YHBaseViewController {
//1.
gk_navTitle
=
"IM 测试"
let
addBtn
=
UIButton
()
addBtn
.
setTitle
(
"发送"
,
for
:
.
normal
)
view
.
addSubview
(
addBtn
)
addBtn
.
snp
.
makeConstraints
{
make
in
make
.
centerX
.
centerY
.
equalToSuperview
()
make
.
height
.
width
.
equalTo
(
200
)
if
EMClient
.
shared
()
.
isLoggedIn
{
YHHUD
.
flash
(
message
:
"已登录IM服务器"
)
// EMClient.shared().logout(true) { err in
// if let err = err {
// YHHUD.flash(message: err.errorDescription)
// } else {
// YHHUD.flash(message: "退出IM服务器-成功")
// }
// }
// return
}
addBtn
.
backgroundColor
=
UIColor
.
red
//for test hjl
addBtn
.
addTarget
(
self
,
action
:
#selector(
sendMessage
)
,
for
:
.
touchUpInside
)
setupUI
(
)
//2
//注册用户
// 异步方法
// [[EMClient sharedClient] registerWithUsername:@"username"
// password:@"your password"
// completion:^(NSString *aUsername, EMError *aError) {
// }];
//2、处理代理回调
EMClient
.
shared
()
.
roomManager
?
.
add
(
self
,
delegateQueue
:
nil
)
EMClient
.
shared
()
.
chatManager
?
.
add
(
self
,
delegateQueue
:
nil
)
}
deinit
{
EMClient
.
shared
()
.
chatManager
?
.
remove
(
self
)
EMClient
.
shared
()
.
roomManager
?
.
remove
(
self
)
}
}
private
extension
YHIMTestViewController
{
func
setupUI
()
{
var
tagView
:
UIView
?
// EMClient.shared().register(withUsername: "davidTest", password: "galaxy-666") { aUsername, error in
// if let err = error {
// print("注册失败")
// print(err.description)
// } else {
// print("注册成功")
//
//
// //登录账号
//// [[EMClient sharedClient] loginWithUsername:@"username"
//// password:@"your password"
//// completion:^(NSString *aUsername, EMError *aError) {
////
//// }];
//
// EMClient.shared().login(withUsername: "davidTest", password: "galaxy-666") { aUsername, error in
// if let err = error {
// print("登录失败")
// print(err.description)
// } else {
// print("登录成功")
//
//
//
// }
// }
// }
// }
//1.登录
do
{
let
addBtn
=
UIButton
()
addBtn
.
setTitle
(
"1、用户登录"
,
for
:
.
normal
)
view
.
addSubview
(
addBtn
)
addBtn
.
snp
.
makeConstraints
{
make
in
make
.
top
.
equalToSuperview
()
.
offset
(
80
)
make
.
centerX
.
equalToSuperview
()
make
.
width
.
equalTo
(
300
)
make
.
height
.
equalTo
(
44
)
}
addBtn
.
backgroundColor
=
UIColor
.
red
//for test hjl
addBtn
.
addTarget
(
self
,
action
:
#selector(
userLoginOp
)
,
for
:
.
touchUpInside
)
tagView
=
addBtn
}
//2.加入聊天室
do
{
let
addBtn
=
UIButton
()
addBtn
.
setTitle
(
"2、加入聊天室"
,
for
:
.
normal
)
view
.
addSubview
(
addBtn
)
if
let
tagView
=
tagView
{
addBtn
.
snp
.
makeConstraints
{
make
in
make
.
top
.
equalTo
(
tagView
.
snp
.
bottom
)
.
offset
(
20
)
make
.
centerX
.
equalToSuperview
()
make
.
width
.
equalTo
(
300
)
make
.
height
.
equalTo
(
44
)
}
}
addBtn
.
backgroundColor
=
UIColor
.
red
addBtn
.
addTarget
(
self
,
action
:
#selector(
joinChatRoom
)
,
for
:
.
touchUpInside
)
tagView
=
addBtn
}
EMClient
.
shared
()
.
roomManager
?
.
add
(
self
,
delegateQueue
:
nil
)
EMClient
.
shared
()
.
chatManager
?
.
add
(
self
,
delegateQueue
:
nil
)
//3.发送消息
do
{
let
addBtn
=
UIButton
()
addBtn
.
setTitle
(
"3、发送信息"
,
for
:
.
normal
)
view
.
addSubview
(
addBtn
)
if
let
tagView
=
tagView
{
addBtn
.
snp
.
makeConstraints
{
make
in
make
.
top
.
equalTo
(
tagView
.
snp
.
bottom
)
.
offset
(
20
)
make
.
centerX
.
equalToSuperview
()
make
.
width
.
equalTo
(
300
)
make
.
height
.
equalTo
(
44
)
}
}
addBtn
.
backgroundColor
=
UIColor
.
red
addBtn
.
addTarget
(
self
,
action
:
#selector(
sendMessage
)
,
for
:
.
touchUpInside
)
tagView
=
addBtn
}
//4.获取历史消息
do
{
let
addBtn
=
UIButton
()
addBtn
.
setTitle
(
"4、获取历史消息-需付费"
,
for
:
.
normal
)
view
.
addSubview
(
addBtn
)
if
let
tagView
=
tagView
{
addBtn
.
snp
.
makeConstraints
{
make
in
make
.
top
.
equalTo
(
tagView
.
snp
.
bottom
)
.
offset
(
20
)
make
.
centerX
.
equalToSuperview
()
make
.
width
.
equalTo
(
300
)
make
.
height
.
equalTo
(
44
)
}
}
addBtn
.
backgroundColor
=
UIColor
.
red
addBtn
.
addTarget
(
self
,
action
:
#selector(
fetchHistoryMessage
)
,
for
:
.
touchUpInside
)
tagView
=
addBtn
}
EMClient
.
shared
()
.
login
(
withUsername
:
yHID
,
token
:
"YWMtqUTnNKsxEe-ZVA-eCJXgmszPhuTA1UDqp8xrPJoVisk1wDGwqzER74uZ-bqVfmOVAwMAAAGTY4WtWgABUYBw0BfZJnoSM_kNt49EM81gKznPICMuPw-M2Fhj96-G7Q"
)
{
name
,
err
in
if
let
err
=
err
{
print
(
err
)
if
err
.
code
.
rawValue
==
200
{
EMClient
.
shared
()
.
roomManager
?
.
joinChatroom
(
"265249570750475"
,
ext
:
"nickname=DavidTest"
,
leaveOtherRooms
:
true
,
completion
:
{
room
,
err
in
if
let
err
=
err
{
print
(
"加入聊天室 - 失败"
)
print
(
err
)
}
else
{
print
(
"加入聊天室 - 成功"
)
}
})
}
else
{
print
(
"登录 - 失败"
)
//5、退出聊天室
do
{
let
addBtn
=
UIButton
()
addBtn
.
setTitle
(
"5、退出聊天室"
,
for
:
.
normal
)
view
.
addSubview
(
addBtn
)
if
let
tagView
=
tagView
{
addBtn
.
snp
.
makeConstraints
{
make
in
make
.
top
.
equalTo
(
tagView
.
snp
.
bottom
)
.
offset
(
20
)
make
.
centerX
.
equalToSuperview
()
make
.
width
.
equalTo
(
300
)
make
.
height
.
equalTo
(
44
)
}
}
else
{
print
(
"登录 - 成功"
)
//发送消息
EMClient
.
shared
()
.
roomManager
?
.
joinChatroom
(
"265249570750475"
,
ext
:
"nickname=DavidTest"
,
leaveOtherRooms
:
true
,
completion
:
{
room
,
err
in
if
let
err
=
err
{
print
(
"加入聊天室 - 失败"
)
print
(
err
)
}
else
{
print
(
"加入聊天室 - 成功"
)
}
})
}
addBtn
.
backgroundColor
=
UIColor
.
red
addBtn
.
addTarget
(
self
,
action
:
#selector(
quitChatRoom
)
,
for
:
.
touchUpInside
)
tagView
=
addBtn
}
//6.聊天室成员列表
do
{
let
addBtn
=
UIButton
()
addBtn
.
setTitle
(
"6、聊天室成员列表"
,
for
:
.
normal
)
view
.
addSubview
(
addBtn
)
if
let
tagView
=
tagView
{
addBtn
.
snp
.
makeConstraints
{
make
in
make
.
top
.
equalTo
(
tagView
.
snp
.
bottom
)
.
offset
(
20
)
make
.
centerX
.
equalToSuperview
()
make
.
width
.
equalTo
(
300
)
make
.
height
.
equalTo
(
44
)
}
}
addBtn
.
backgroundColor
=
UIColor
.
red
addBtn
.
addTarget
(
self
,
action
:
#selector(
fetchRoomMemberList
)
,
for
:
.
touchUpInside
)
tagView
=
addBtn
}
}
deinit
{
EMClient
.
shared
()
.
chatManager
?
.
remove
(
self
)
EMClient
.
shared
()
.
roomManager
?
.
remove
(
self
)
//6.聊天室成员列表
@objc
func
fetchRoomMemberList
(){
EMClient
.
shared
()
.
roomManager
?
.
getChatroomMemberListFromServer
(
withId
:
roomID
,
cursor
:
nil
,
pageSize
:
20
,
completion
:
{
result
,
error
in
if
let
err
=
error
{
YHHUD
.
flash
(
message
:
err
.
errorDescription
)
}
else
{
if
let
ret
=
result
{
let
count
=
(
ret
.
list
?
.
count
??
0
)
+
1
YHHUD
.
flash
(
message
:
"聊天室人数
\(
count
)
"
)
}
}
})
}
}
private
extension
YHIMTestViewController
{
//5.退出聊天室
@objc
func
quitChatRoom
(){
EMClient
.
shared
()
.
roomManager
?
.
leaveChatroom
(
roomID
,
completion
:
{
err
in
if
let
error
=
err
{
YHHUD
.
flash
(
message
:
error
.
errorDescription
)
}
else
{
YHHUD
.
flash
(
message
:
"退出聊天室-成功"
)
}
})
}
//4.获取历史消息
@objc
func
fetchHistoryMessage
(){
let
option
=
EMFetchServerMessagesOption
()
EMClient
.
shared
()
.
chatManager
?
.
fetchMessagesFromServer
(
by
:
self
.
roomID
,
conversationType
:
.
chatRoom
,
cursor
:
"0"
,
pageSize
:
50
,
option
:
option
,
completion
:
{
result
,
err
in
DispatchQueue
.
main
.
async
{
if
let
err
=
err
{
// 获取失败
printLog
(
"获取历史消息--失败"
)
printLog
(
err
)
YHHUD
.
flash
(
message
:
"获取历史消息-失败"
)
}
else
{
// 获取成功
printLog
(
result
)
YHHUD
.
flash
(
message
:
"获取历史消息-成功"
)
}
}
})
}
//3.发送消息
@objc
func
sendMessage
()
{
getRoomInfo
()
// 调用 initWithText 创建文本消息。`content` 为文本消息的内容。
let
textMessageBody
=
EMTextMessageBody
(
text
:
"金龙发送的信息"
)
// 消息接收方,单聊为对端用户的 ID,群聊为群组 ID,聊天室为聊天室 ID。
let
conversationId
=
"265249570750475"
let
message
=
EMChatMessage
(
conversationID
:
conversationId
,
body
:
textMessageBody
,
ext
:
nil
)
let
conversationId
=
roomID
let
message
=
EMChatMessage
(
conversationID
:
conversationId
,
body
:
textMessageBody
,
ext
:
[
"fromNickName"
:
"YinHe3775"
]
)
// 会话类型,单聊为 `EMChatTypeChat`,群聊为 `EMChatTypeGroupChat`,聊天室为 `EMChatTypeChatRoom`,默认为单聊。
message
.
chatType
=
.
chatRoom
// 发送消息。
// EMClient.shared().chatManager?.sendMessage(message, progress: nil) { result in
// switch result {
// case .success:
// print("消息发送成功")
// case .failure(let error):
// print("消息发送失败: \(error.localizedDescription)")
// }
// }
EMClient
.
shared
()
.
chatManager
?
.
send
(
message
,
progress
:
nil
,
completion
:
{
message
,
error
in
if
let
err
=
error
{
printLog
(
"消息发送 --- 失败"
)
YHHUD
.
flash
(
message
:
err
.
errorDescription
)
}
else
{
printLog
(
"消息发送 --- 成功"
)
//获取历史消息
let
option
=
EMFetchServerMessagesOption
();
EMClient
.
shared
()
.
chatManager
?
.
fetchMessagesFromServer
(
by
:
"265249570750475"
,
conversationType
:
.
chatRoom
,
cursor
:
""
,
pageSize
:
50
,
option
:
option
,
completion
:
{
result
,
err
in
if
let
err
=
err
{
// 获取失败
printLog
(
"获取历史消息 --- 失败"
)
printLog
(
err
)
}
else
{
// 获取成功
printLog
(
"获取历史消息 --- 成功"
)
printLog
(
result
)
}
})
YHHUD
.
flash
(
message
:
"消息发送-成功"
)
}
})
}
func
getRoomInfo
()
{
EMClient
.
shared
()
.
roomManager
?
.
getChatroomSpecificationFromServer
(
withId
:
roomID
,
fetchMembers
:
true
,
completion
:
{
emChatRoom
,
err
in
printLog
(
emChatRoom
)
//2.加入聊天室
@objc
func
joinChatRoom
()
{
EMClient
.
shared
()
.
roomManager
?
.
joinChatroom
(
self
.
roomID
,
ext
:
"nickname=DavidTest"
,
leaveOtherRooms
:
true
,
completion
:
{
room
,
err
in
if
let
err
=
err
{
YHHUD
.
flash
(
message
:
err
.
errorDescription
)
}
else
{
YHHUD
.
flash
(
message
:
"加入聊天室-成功"
)
}
})
EMClient
.
shared
()
.
roomManager
?
.
getChatroomSpecificationFromServer
(
withId
:
roomID
,
completion
:
{
emChatRoom
,
err
in
printLog
(
emChatRoom
)
}
//1.IM 用户登录
@objc
func
userLoginOp
()
{
EMClient
.
shared
()
.
login
(
withUsername
:
yHID
,
token
:
"YWMtqUTnNKsxEe-ZVA-eCJXgmszPhuTA1UDqp8xrPJoVisk1wDGwqzER74uZ-bqVfmOVAwMAAAGTY4WtWgABUYBw0BfZJnoSM_kNt49EM81gKznPICMuPw-M2Fhj96-G7Q"
)
{
name
,
err
in
if
let
err
=
err
{
}
else
{
if
let
roomInfo
=
emChatRoom
{
printLog
(
roomInfo
.
adminList
)
printLog
(
roomInfo
.
chatroomId
)
printLog
(
roomInfo
.
blacklist
)
printLog
(
roomInfo
.
memberList
)
if
err
.
code
.
rawValue
==
200
{
YHHUD
.
flash
(
message
:
err
.
errorDescription
)
}
else
{
YHHUD
.
flash
(
message
:
"登录-失败"
)
}
}
else
{
YHHUD
.
flash
(
message
:
"登录-成功"
)
}
})
}
}
// func getRoomInfo() {
// EMClient.shared().roomManager?.getChatroomSpecificationFromServer(withId: roomID, fetchMembers: true, completion: { emChatRoom, err in
// printLog(emChatRoom)
// if let err = err {
//
// } else {
//
// }
// })
//
//
// EMClient.shared().roomManager?.getChatroomSpecificationFromServer(withId: roomID, completion: { emChatRoom, err in
// printLog(emChatRoom)
//
// if let err = err {
// print(err)
// } else {
//
// if let roomInfo = emChatRoom {
// printLog(roomInfo.adminList)
// printLog(roomInfo.chatroomId)
// printLog(roomInfo.blacklist)
// printLog(roomInfo.memberList)
// }
//
// }
// })
//
// }
}
// 收到其他人加入聊天室的回调
...
...
@@ -205,28 +296,38 @@ extension YHIMTestViewController: EMChatroomManagerDelegate {
printLog
(
aChatroom
)
printLog
(
aUsername
)
printLog
(
ext
)
YHHUD
.
flash
(
message
:
"
\(
aChatroom
)
加入聊天室了"
)
}
func
userDidJoin
(
_
aChatroom
:
EMChatroom
,
user
aUsername
:
String
)
{
let
memberCount
=
aChatroom
.
occupantsCount
printLog
(
memberCount
)
YHHUD
.
flash
(
message
:
"
\(
aUsername
)
加入 聊天室了"
)
}
func
userDidLeave
(
_
aChatroom
:
EMChatroom
,
user
aUsername
:
String
)
{
let
memberCount
=
aChatroom
.
occupantsCount
printLog
(
memberCount
)
YHHUD
.
flash
(
message
:
"
\(
aUsername
)
离开 聊天室了"
)
}
}
extension
YHIMTestViewController
:
EMChatManagerDelegate
{
//收到消息
func
messagesDidReceive
(
_
aMessages
:
[
EMChatMessage
])
{
for
(
i
,
t
)
in
aMessages
.
enumerated
()
{
printLog
(
t
)
printLog
(
t
.
ext
)
var
nickName
:
String
=
""
if
let
ext
=
t
.
ext
{
nickName
=
ext
[
"fromNickName"
]
as?
String
??
"YinHe"
}
if
let
body
=
t
.
body
as?
EMTextMessageBody
,
body
.
type
==
.
text
{
printLog
(
body
.
text
)
YHHUD
.
flash
(
message
:
"
\(
nickName
)
:
\(
body
.
text
)
"
)
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment