LiveRoom.ets
2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { ChatroomDestroyType,
ChatroomJoinedInfo,
ChatroomStatusListener,
EngineError,
IMEngine, Message, ReceivedInfo,
TextMessage} from '@rongcloud/imlib';
import { Logger } from 'wdKit/Index';
import { LiveRoomBaseInfo } from './LiveRoomBaseInfo'
import { JSON } from '@kit.ArkTS';
const TAG = "LiveRoomManager"
export class LiveRoom extends ChatroomStatusListener {
connectRoomBaseInfo?: LiveRoomBaseInfo
/// 回调
onJoined?: (room: LiveRoom) => void
onJoinFailed?: (room: LiveRoom, code: number) => void
onExited?: (room: LiveRoom) => void
constructor(baseInfo?: LiveRoomBaseInfo) {
super()
this.connectRoomBaseInfo = baseInfo
}
enterRoom() {
let roomId = this.connectRoomBaseInfo?.roomID
let msgCount = 10;
Logger.debug(TAG, `will enterRoom roomId: ${roomId}`);
IMEngine.getInstance().joinExistingChatroom(roomId, msgCount).then(result => {
if (EngineError.Success !== result.code) {
// 加入聊天室失败
Logger.error(TAG, 'onChatroomJoinFailed roomId: ' + roomId + " code: " + result.code);
return;
}
if (!result.data) {
// 聊天室加入信息失败
return;
}
let joinedInfo = result.data as ChatroomJoinedInfo;
});
}
exitRoom() {
let roomId = this.connectRoomBaseInfo?.roomID
IMEngine.getInstance().quitChatroom(roomId).then(result => {
if (EngineError.Success !== result.code) {
// 退出聊天室失败
return;
}
});
}
// ---- ChatroomStatusListener
onChatroomJoining(roomId: string): void {
Logger.debug(TAG, `onChatroomJoining roomId: ${roomId}`);
}
onChatroomJoined(roomId: string, info: ChatroomJoinedInfo): void {
Logger.debug(TAG, `onChatroomJoined roomId` + roomId + " " + JSON.stringify(info));
if (this.onJoined) {
this.onJoined(this)
}
}
onChatroomJoinFailed(roomId: string, code: EngineError): void {
Logger.error(TAG, 'onChatroomJoinFailed roomId: ' + roomId + " code: " + code);
if (this.onJoinFailed) {
this.onJoinFailed(this, code)
}
}
onChatroomQuited(roomId: string): void {
Logger.warn(TAG, 'onChatroomQuited roomId: ' + roomId);
if (this.onExited) {
this.onExited(this)
}
}
onChatroomDestroyed(roomId: string, type: ChatroomDestroyType): void {
Logger.debug(TAG, 'onChatroomDestroyed roomId: ' + roomId + " type: " + type);
}
// --- on message
onMessage(message: Message, info: ReceivedInfo) {
Logger.debug(TAG, 'onMessage message: ' + JSON.stringify(message));
if (message.objectName != "RC:TxtMsg") {
return
}
let textMsg = message.content as TextMessage
textMsg.content
}
}