LiveRoom.ets
3.91 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { ChatroomDestroyType,
ChatroomJoinedInfo,
ChatroomStatusListener,
EngineError,
IMEngine, Message, ReceivedInfo,
TextMessage} from '@rongcloud/imlib';
import { Logger } from 'wdKit/Index';
import { LiveRoomItemBean } from 'wdBean/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
///用于展示的历史消息,包含聊天室和直播间
onHistoryMessage?: (liveRoomItemBean: LiveRoomItemBean) => void
///用于管理直播间状态的消息,直播垫片,直播结束,上墙/取消上墙,置顶/取消置顶等
onLiveMessage?: (liveRoomItemBean: LiveRoomItemBean) => 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
let liveRoomItemBean = JSON.parse(textMsg.content) as LiveRoomItemBean
if (liveRoomItemBean == undefined) {
return
}
let optionType = liveRoomItemBean.optionType != undefined ? liveRoomItemBean.optionType : liveRoomItemBean.dataType
if (this.isHistoryMessage(optionType)) {
if (this.onHistoryMessage) {
this.onHistoryMessage(liveRoomItemBean)
}
return
}
if (this.onLiveMessage) {
// liveRoomItemBean.pv = Number(liveRoomItemBean.pv) + 1000000 + ""
this.onLiveMessage(liveRoomItemBean)
}
}
isHistoryMessage(optionType: string): boolean {
let isHistoryMessage = false
switch (optionType) {
case "ZH_TEXT_MSG":
case "ZH_IMAGE_MSG":
case "ZH_TEXT_AND_IMAGE_MSG":
case "ZH_AUDIO_MSG":
case "ZH_VIDEO_MSG": {
isHistoryMessage = true
} break;
default:
break;
}
return isHistoryMessage
}
}