refactor(agora): 변경을 용이하게 하기 위해 RTM과 RTC코드 분리 및 재배치
This commit is contained in:
		@@ -12,24 +12,28 @@ import AgoraRtmKit
 | 
			
		||||
final class Agora {
 | 
			
		||||
    static let shared = Agora()
 | 
			
		||||
    
 | 
			
		||||
    var rtcEngineDelegate: AgoraRtcEngineDelegate?
 | 
			
		||||
    var rtmDelegate: AgoraRtmDelegate?
 | 
			
		||||
    
 | 
			
		||||
    var rtcEngine: AgoraRtcEngineKit?
 | 
			
		||||
    
 | 
			
		||||
    var rtmKit: AgoraRtmKit?
 | 
			
		||||
    var rtmChannel: AgoraRtmChannel?
 | 
			
		||||
    
 | 
			
		||||
    func initialize() {
 | 
			
		||||
        initRtcEngine()
 | 
			
		||||
        initRtmClient()
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func deInit() {
 | 
			
		||||
        deInitRtcEngine()
 | 
			
		||||
        deInitRtmClient()
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    // MARK: RTC
 | 
			
		||||
    var rtcEngine: AgoraRtcEngineKit?
 | 
			
		||||
    var rtcEngineDelegate: AgoraRtcEngineDelegate?
 | 
			
		||||
    
 | 
			
		||||
    func initRtcEngine() {
 | 
			
		||||
        rtcEngine = AgoraRtcEngineKit.sharedEngine(withAppId: AGORA_APP_ID, delegate: rtcEngineDelegate)
 | 
			
		||||
        rtcEngine?.setChannelProfile(.liveBroadcasting)
 | 
			
		||||
        rtcEngine?.enableAudio()
 | 
			
		||||
        rtcEngine?.enableAudioVolumeIndication(500, smooth: 3, reportVad: true)
 | 
			
		||||
        
 | 
			
		||||
        rtmKit = AgoraRtmKit(appId: AGORA_APP_ID, delegate: rtmDelegate)
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func deInit() {
 | 
			
		||||
    func deInitRtcEngine() {
 | 
			
		||||
        if let rtcEngine = rtcEngine {
 | 
			
		||||
            rtcEngine.leaveChannel(nil)
 | 
			
		||||
            
 | 
			
		||||
@@ -37,20 +41,56 @@ final class Agora {
 | 
			
		||||
                AgoraRtcEngineKit.destroy()
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        rtmChannel?.leave(completion: nil)
 | 
			
		||||
        rtmKit?.logout(completion: nil)
 | 
			
		||||
        rtcEngine = nil
 | 
			
		||||
        rtmChannel = nil
 | 
			
		||||
        rtmKit = nil
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func joinRtcChannel(rtcToken: String, channelName: String) {
 | 
			
		||||
        let userId = UserDefaults.int(forKey: .userId)
 | 
			
		||||
        
 | 
			
		||||
        rtcEngine?.joinChannel(
 | 
			
		||||
            byToken: rtcToken,
 | 
			
		||||
            channelId: channelName,
 | 
			
		||||
            info: nil,
 | 
			
		||||
            uid: UInt(userId),
 | 
			
		||||
            joinSuccess: nil
 | 
			
		||||
        )
 | 
			
		||||
        
 | 
			
		||||
        rtcEngine?.setAudioProfile(.musicHighQualityStereo)
 | 
			
		||||
        rtcEngine?.setAudioScenario(.gameStreaming)
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func setRole(role: AgoraClientRole) {
 | 
			
		||||
        self.rtcEngine?.setClientRole(role)
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func joinChannel(
 | 
			
		||||
        roomInfo: GetRoomInfoResponse,
 | 
			
		||||
    func mute(_ isMute: Bool) {
 | 
			
		||||
        rtcEngine?.muteLocalAudioStream(isMute)
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func speakerMute(_ isMute: Bool) {
 | 
			
		||||
        rtcEngine?.muteAllRemoteAudioStreams(isMute)
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    // MARK: RTM
 | 
			
		||||
    var rtmKit: AgoraRtmKit?
 | 
			
		||||
    var rtmChannel: AgoraRtmChannel?
 | 
			
		||||
    var rtmDelegate: AgoraRtmDelegate?
 | 
			
		||||
    
 | 
			
		||||
    func initRtmClient() {
 | 
			
		||||
        rtmKit = AgoraRtmKit(appId: AGORA_APP_ID, delegate: rtmDelegate)
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func deInitRtmClient() {
 | 
			
		||||
        rtmChannel?.leave(completion: nil)
 | 
			
		||||
        rtmKit?.logout(completion: nil)
 | 
			
		||||
        rtmChannel = nil
 | 
			
		||||
        rtmKit = nil
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func rtmLogin(
 | 
			
		||||
        creatorId: Int,
 | 
			
		||||
        rtmToken: String,
 | 
			
		||||
        channelName: String,
 | 
			
		||||
        rtmChannelDelegate: AgoraRtmChannelDelegate,
 | 
			
		||||
        onConnectSuccess: @escaping (Bool) -> Void,
 | 
			
		||||
        onConnectFail: @escaping () -> Void
 | 
			
		||||
@@ -61,36 +101,25 @@ final class Agora {
 | 
			
		||||
        
 | 
			
		||||
        let userId = UserDefaults.int(forKey: .userId)
 | 
			
		||||
        
 | 
			
		||||
        rtcEngine?.joinChannel(
 | 
			
		||||
            byToken: roomInfo.rtcToken,
 | 
			
		||||
            channelId: roomInfo.channelName,
 | 
			
		||||
            info: nil,
 | 
			
		||||
            uid: UInt(userId),
 | 
			
		||||
            joinSuccess: nil
 | 
			
		||||
        )
 | 
			
		||||
        
 | 
			
		||||
        rtcEngine?.setAudioProfile(.musicHighQualityStereo)
 | 
			
		||||
        rtcEngine?.setAudioScenario(.gameStreaming)
 | 
			
		||||
        
 | 
			
		||||
        rtmChannel = rtmKit?.createChannel(
 | 
			
		||||
            withId: roomInfo.channelName,
 | 
			
		||||
            withId: channelName,
 | 
			
		||||
            delegate: rtmChannelDelegate
 | 
			
		||||
        )
 | 
			
		||||
        
 | 
			
		||||
        rtmKit?.login(
 | 
			
		||||
            byToken: roomInfo.rtmToken,
 | 
			
		||||
            byToken: rtmToken,
 | 
			
		||||
            user: String(userId),
 | 
			
		||||
            completion: { [unowned self] loginErrorCode in
 | 
			
		||||
                if loginErrorCode == .ok {
 | 
			
		||||
                    self.rtmChannel?.join(completion: { joinChannelErrorCode in
 | 
			
		||||
                        if joinChannelErrorCode == .channelErrorOk {
 | 
			
		||||
                            if userId == roomInfo.creatorId {
 | 
			
		||||
                            if userId == creatorId {
 | 
			
		||||
                                self.setRole(role: .broadcaster)
 | 
			
		||||
                            } else {
 | 
			
		||||
                                self.setRole(role: .audience)
 | 
			
		||||
                            }
 | 
			
		||||
                            
 | 
			
		||||
                            onConnectSuccess(userId == roomInfo.creatorId)
 | 
			
		||||
                            onConnectSuccess(userId == creatorId)
 | 
			
		||||
                        } else {
 | 
			
		||||
                            onConnectFail()
 | 
			
		||||
                        }
 | 
			
		||||
@@ -128,14 +157,6 @@ final class Agora {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func mute(_ isMute: Bool) {
 | 
			
		||||
        rtcEngine?.muteLocalAudioStream(isMute)
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func speakerMute(_ isMute: Bool) {
 | 
			
		||||
        rtcEngine?.muteAllRemoteAudioStreams(isMute)
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    func sendMessageToGroup(textMessage: String, completion: @escaping AgoraRtmSendChannelMessageBlock) {
 | 
			
		||||
        let message = AgoraRtmMessage(text: textMessage)
 | 
			
		||||
        rtmChannel?.send(message, completion: completion)
 | 
			
		||||
 
 | 
			
		||||
@@ -369,8 +369,12 @@ final class LiveRoomViewModel: NSObject, ObservableObject {
 | 
			
		||||
                        
 | 
			
		||||
                        self.isActiveRoulette = data.isActiveRoulette
 | 
			
		||||
                        self.isLoading = true
 | 
			
		||||
                        self.agora.joinChannel(
 | 
			
		||||
                            roomInfo: data,
 | 
			
		||||
                        
 | 
			
		||||
                        self.agora.joinRtcChannel(rtcToken: data.rtcToken, channelName: data.channelName)
 | 
			
		||||
                        self.agora.rtmLogin(
 | 
			
		||||
                            creatorId: data.creatorId,
 | 
			
		||||
                            rtmToken: data.rtmToken,
 | 
			
		||||
                            channelName: data.channelName,
 | 
			
		||||
                            rtmChannelDelegate: self,
 | 
			
		||||
                            onConnectSuccess: self.agoraConnectSuccess,
 | 
			
		||||
                            onConnectFail: self.agoraConnectFail
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user