AgoraIO / RTM

143 stars 160 forks source link

SwiftUI Agora RTM channel delegate not being called #179

Open riverbaymark opened 2 years ago

riverbaymark commented 2 years ago

I can successful log into the rtm service and get a successful error response (code 0) when sending a message. The issue is the delegate is not called for either item. I am using the latest RTM version 1.4.10. Below is a sample of my code :

`import SwiftUI import AgoraRtmKit import AgoraRtcKit

struct UserData: Codable { var rtmId: String var rtcId: UInt var username: String func toJSONString() throws -> String? { let jsonData = try JSONEncoder().encode(self) return String(data: jsonData, encoding: .utf8) } }

class AgoraObservable: NSObject, ObservableObject {

@Published var chatMessages: [ChatMessage] = []
var channel: AgoraRtmChannel?
@Published var channelName: String = "test"
@Published var agoraSessionToken = "0061537aa75dc534ada816eb808742d17c4IAA1r7RGzcXe/ZnDKEeLOCNnUuD0O4WOpz+TQ0fC9Aoiawx+f9gAAAAAEAC3KIpZy8BuYgEAAQDLwG5i"
@Published var username: String = ""
@Published var members: [String] = []
@Published var membersLookup: [String: (rtcId: UInt, username: String)] = [:] {
    didSet {
        members = self.membersLookup.values.compactMap {
            $0.username + (
                $0.rtcId == self.rtcId ? " (Me)" : ""
            )
        }
    }
}

var rtcId: UInt = 0
var rtmId = "test"
var rtmIsLoggedIn = false

lazy var rtckit: AgoraRtcEngineKit = {
    let engine = AgoraRtcEngineKit.sharedEngine(
        withAppId: "1537aa75dc534ada816eb808742d17c4", delegate: nil
    )
    engine.setChannelProfile(.liveBroadcasting)
    engine.setClientRole(.broadcaster)
    return engine
}()

lazy var rtmkit: AgoraRtmKit? = {
    let rtm = AgoraRtmKit(
        appId: "1537aa75dc534ada816eb808742d17c4", delegate: self
    )
    return rtm
}()

} extension AgoraObservable { func joinChannel() { if !self.rtmIsLoggedIn { rtmkit?.login(byToken: self.agoraSessionToken, user: self.rtmId) { loginResponse in if loginResponse != .ok { fatalError("Could not log in to RTM") } print("Successfully logged into rtm") self.rtmIsLoggedIn = true self.joinChannel() } return }

        self.channel = self.rtmkit?.createChannel(withId: self.channelName, delegate: self)
        self.channel?.join(completion: { joinStatus in
            if joinStatus == .channelErrorOk {
                print("Successfully joined channel")
                print(self.channel)

            } else {

                print(joinStatus)

                self.channel = nil

            }
        })
}

}

extension AgoraObservable: AgoraRtmChannelDelegate, AgoraRtmDelegate { func channel(_ channel: AgoraRtmChannel, memberJoined member: AgoraRtmMember) { print("Agora member: (member.userId)") }

func channel(_ channel: AgoraRtmChannel, messageReceived message: AgoraRtmMessage, from member: AgoraRtmMember) {
    print("Message received from: \(member.userId)")

    print(message)

    var color = Color.gray
    var isMe = false
    if member.userId == UIDevice.current.name {
        color = Color.blue
        isMe = true
    }
    let newMessage = ChatMessage(message: message.text, avatar: member.userId, color: color, isMe: isMe)

    chatMessages.append(newMessage)

    //parseMemberData(from: message.text)
}
func rtmKit(_ kit: AgoraRtmKit, messageReceived message: AgoraRtmMessage, fromPeer peerId: String) {
    parseMemberData(from: message.text)
}

func parseMemberData(from text: String) {
    guard let textData = text.data(using: .utf8),
          let decodedUserData = try? JSONDecoder().decode(UserData.self, from: textData)
    else {
        return
    }
    membersLookup[decodedUserData.rtmId] = (decodedUserData.rtcId, decodedUserData.username)
}
func channel(_ channel: AgoraRtmChannel, memberLeft member: AgoraRtmMember) {
    membersLookup.removeValue(forKey: member.userId)
}

}`

Any help would be greatly appreciated!