LiveRoomManager.ets
7.15 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import {
ChatroomDestroyType,
ChatroomJoinedInfo,
ChatroomStatusListener, ConnectionStatus, EngineError, IMEngine, InitOption,
Message,
ReceivedInfo} from '@rongcloud/imlib'
import { SpConstants } from 'wdConstant/Index'
import { CrptoUtils, DeviceUtil, EmitterEventId, EmitterUtils, Logger, SPHelper } from 'wdKit/Index'
import { HostEnum, HostManager, HttpBizUtil, HttpUrlUtils, HttpUtils, ResponseDTO } from 'wdNetwork/Index'
import { Context } from '@kit.AbilityKit';
import { LiveRoomBaseInfo } from './LiveRoomBaseInfo'
import { LiveRoom } from './LiveRoom'
const TAG = "LiveRoomManager"
export class LiveRoomManager {
private static roomManager: LiveRoomManager
private static RONGYUN_IM_APPKEY = "k51hidwqk50jb"
private static RONGYUN_IM_APPKEY_TEST = "m7ua80gbm7yim"
private hasInited: boolean = false
public gotContextFunc?: () => Context = undefined
private imTokenInfo?: string = undefined
private connectRoomBaseInfo?: LiveRoomBaseInfo
private connectedRoom?: LiveRoom
/// 连接成功回调
onConnectedRoom?:(room: LiveRoom) => void
private constructor() {
}
public static sharedManager() {
if (!LiveRoomManager.roomManager) {
LiveRoomManager.roomManager = new LiveRoomManager()
}
LiveRoomManager.roomManager.checkToInitManager()
return LiveRoomManager.roomManager
}
public connectLiveRoomWith(baseInfo: LiveRoomBaseInfo) {
this.connectRoomBaseInfo = baseInfo
if (this.imTokenInfo) {
this.connectWithIMToken(this.imTokenInfo)
return
}
this.fetchIMToken().then((token: string) => {
this.imTokenInfo = token;
this.connectWithIMToken(token)
})
}
public disconnect() {
this.connectedRoom?.exitRoom()
this.connectedRoom = undefined
IMEngine.getInstance().disconnect(true)
}
private checkToInitManager() {
if (this.hasInited) {
return
}
if (this.gotContextFunc) {
let initOption = new InitOption();
IMEngine.getInstance().init(this.gotContextFunc(), this.getAppKey(), initOption);
this.listenStatus()
this.hasInited = true
}
}
private listenStatus() {
EmitterUtils.receiveEvent(EmitterEventId.FORCE_USER_LOGIN_OUT, () => {
this.imTokenInfo = undefined
IMEngine.getInstance().disconnect(true)
})
IMEngine.getInstance().setConnectionStatusListener((status: ConnectionStatus) => {
switch (status) {
case ConnectionStatus.Connecting: {
Logger.debug(TAG, "ConnectionStatus => 开始连接, 连接中....")
} break
case ConnectionStatus.Connected: {
Logger.debug(TAG, "ConnectionStatus => 链接成功")
this.processConnected()
} break
case ConnectionStatus.DisconnectNetworkUnavailable: {
Logger.debug(TAG, "ConnectionStatus => 连接异常,SDK 会做好自动重连,开发者无须处理")
} break
case ConnectionStatus.DisconnectUserKicked: {
Logger.debug(TAG, "ConnectionStatus => 当前用户在其他设备上登录,此设备被踢下线")
} break
case ConnectionStatus.DisconnectConnectionTimeout: {
Logger.debug(TAG, "ConnectionStatus => 链接超时")
} break
case ConnectionStatus.DisconnectUserLogout: {
Logger.debug(TAG, "ConnectionStatus => 退出连接")
} break
case ConnectionStatus.DisconnectTokenIncorrect: {
Logger.debug(TAG, "ConnectionStatus => token无效,请检查AppKey配置,或者token过期,需要重新获取token")
} break
case ConnectionStatus.DisconnectTokenExpired: {
Logger.debug(TAG, "ConnectionStatus => token过期,需要重新获取token")
} break
default : {
Logger.debug(TAG, "ConnectionStatus => 其他状态:" + status)
} break
}
})
}
private connectWithIMToken(token: string) {
Logger.debug(TAG, "连接开始,IM Token:" + token)
const timeout = 5
IMEngine.getInstance().connect(token, timeout).then(result => {
if (EngineError.Success === result.code) {
// 连接成功
let userId = result.userId;
Logger.debug(TAG, "连接成功:" + userId)
return
}
if (EngineError.ConnectionExists === result.code) {
Logger.debug(TAG, "连接已存在")
this.processConnected()
return
}
Logger.debug(TAG, "连接结果:" + result.code)
});
}
private processConnected() {
this.connectedRoom = new LiveRoom(this.connectRoomBaseInfo)
// let listener: ChatroomStatusListener = {
// onChatroomJoining(roomId: string): void {
// Logger.debug(TAG, `onChatroomJoining roomId: ${roomId}`);
// LiveRoomManager.sharedManager().connectedRoom?.onChatroomJoining(roomId)
// },
//
// onChatroomJoined(roomId: string, info: ChatroomJoinedInfo): void {
// Logger.debug(TAG, `onChatroomJoining roomId: ${roomId}`);
// LiveRoomManager.sharedManager().connectedRoom?.onChatroomJoined(roomId, info)
// },
//
// onChatroomJoinFailed(roomId: string, code: EngineError): void {
// Logger.debug(TAG, `onChatroomJoining roomId: ${roomId}`);
// LiveRoomManager.sharedManager().connectedRoom?.onChatroomJoinFailed(roomId, code)
// },
//
// onChatroomQuited(roomId: string): void {
// Logger.debug(TAG, `onChatroomJoining roomId: ${roomId}`);
// LiveRoomManager.sharedManager().connectedRoom?.onChatroomQuited(roomId)
// },
//
// onChatroomDestroyed(roomId: string, type: ChatroomDestroyType): void {
// Logger.debug(TAG, `onChatroomJoining roomId: ${roomId}`);
// LiveRoomManager.sharedManager().connectedRoom?.onChatroomDestroyed(roomId, type)
// },
// }
IMEngine.getInstance().setChatroomStatusListener(this.connectedRoom);
IMEngine.getInstance().setMessageReceivedListener(this.connectedRoom.onMessage.bind(this.connectedRoom));
if (this.onConnectedRoom) {
this.onConnectedRoom(this.connectedRoom)
}
}
private fetchIMToken() {
return new Promise<string>(async (success, fail) => {
let userId = HttpUtils.getUserId()
if (userId.length == 0) {
const deviceId = DeviceUtil.clientId()
userId = await CrptoUtils.md5(deviceId)
}
let url = HttpUrlUtils.getLiveIMToken() + `?userId=${userId}&userName=${userId}`
HttpBizUtil.get<ResponseDTO<string>>(url).then((data: ResponseDTO<string>) => {
if (data.data) {
success(data.data)
} else {
Logger.error(TAG, "接口没返回im token")
fail(data.message)
}
}).catch((error: Error) => {
Logger.error(TAG, "error " + JSON.stringify(error))
fail(error.message)
})
})
}
private getAppKey() {
const appKey = SPHelper.default.getSync(SpConstants.ROMGYUN_IM_APPKEY, "") as string
if (appKey.length > 0) {
return appKey
}
const isOnlineEnv = HostManager.getHost() === HostEnum.HOST_PRODUCT
return isOnlineEnv ? LiveRoomManager.RONGYUN_IM_APPKEY : LiveRoomManager.RONGYUN_IM_APPKEY_TEST
}
}
interface LiveIMToken {
// 连接IM的token
refreshToken: string
//token过期时间
deadline: number
}