【问题标题】:AgoraKit cannot send `userinfo` to remote usersAgoraKit 无法向远程用户发送 `userinfo`
【发布时间】:2020-05-31 17:41:34
【问题描述】:

我正在将 Agora 集成到我的 iOS Swift 5 应用程序中,基本用例显示出莫名其妙的行为。假设爱丽丝打电话给鲍勃,当爱丽丝加入指定用户信息的频道时:

        agoraKit.joinChannel(
            byToken: nil
          , channelId: "chan-1234"
          , info: "alice-userid"
          , uid: 0
        ) { [weak self] (sid, uid, elapsed) in
            print("\n Join channel ...: for sid: \(sid), uid: \(uid), dt: \(elapsed)")
        }

info: "alice-userid" 的数据无法被 Bob 访问:

    func rtcEngine(_ engine: AgoraRtcEngineKit, firstRemoteVideoDecodedOfUid uid:UInt, size:CGSize, elapsed:Int) {

        let userInfo = agoraKit.getUserInfo(byUid: uid, withError: nil)        
        print("added user: \(uid), with userinfo: \(userInfo)") // get uid, nil

    }

在 Bob 这边,userInfo 为零。所以我尝试使用用户帐户加入,

         agoraKit.registerLocalUserAccount("alice-userid", appId: AppID)
        agoraKit.joinChannel(
              byUserAccount: "alice-account-data"
            , token: nil
            , channelId: "chan-1234"
        ) { [weak self] (sid, uid, elapsed) in
            print("\n Join channel ...: for sid: \(sid), uid: \(uid), dt: \(elapsed)")
        }

这完全失败了。我在这里引用文档:

在第二种情况下,文档有拼写错误,API 已过期。

【问题讨论】:

    标签: ios swift agora.io


    【解决方案1】:

    假设您已经使用registerLocalUserAccount:appId: 方法注册了 Alice 和 Bob。他们都必须使用joinChannelByUserAccount:token:channelId:joinSuccess:方法加入频道。确保 registerLocalUserAccount:appId: 方法返回 0 表示客户端注册成功。

    现在如Agora documentation 中所述关于getUserInfoByUid:withError: 方法:

    用户加入频道后,SDK获取远程用户的用户ID和用户账号,缓存在映射表对象(AgoraUserInfo)中,并在本地客户端触发didUpdatedUserInfo回调。收到didUpdatedUserInfo回调后,可以调用该方法,通过传入用户ID,从userInfo对象中获取用户的用户账号。

    这意味着调用getUserInfoByUid:withError: 的正确时机是在调用rtcEngine:didUpdatedUserInfo:withUid: 委托方法时。

    我从你的 sn-ps 猜测你的类已经符合 AgoraRtcEngineDelegate 协议。所以你所要做的就是实现这个委托方法并在方法体内调用getUserInfoByUid:withError:

    示例:

    第 1 步:

    让你的类符合 AgoraRtcEngineDelegate:

    // Assign delegate when instantiating
    lazy var agoraKit: AgoraRtcEngineKit = {
      AgoraRtcEngineKit.sharedEngine(withAppId: "YourAppID", delegate: self)
    }()
    
    // Assign delegate later
    agoraKit.delegate = self
    
    // Conform to AgoraRtcEngineDelegate
    extension MyClass: AgoraRtcEngineDelegate {
      //...
    }
    

    第 2 步:

    使用registerLocalUserAccount:appId:方法注册用户。

    // User with 'bob' username will be registered
    let registerResponse: Int32 = agoraKit.registerLocalUserAccount("bob", appId: "YourAppID")
    if registerResponse == 0 {
        // Successfully registered
    } else {
        // Failed to register
    }
    

    第三步:

    用户注册成功后加入频道。

    agoraKit.joinChannel(byUserAccount: "bob", token: nil, channelId: "room123") { (channel, uid, elapsed) in
        print("User joined channel \(channel) with \(uid). Elapsed time is \(elapsed)ms.")
    }
    

    第 4 步:

    实现rtcEngine:didUpdatedUserInfo:withUid:委托方法。

    // This method will be triggered after Agora SDK
    // caches user ID and user account of the remote user. 
    func rtcEngine(_ engine: AgoraRtcEngineKit, didUpdatedUserInfo userInfo: AgoraUserInfo, withUid uid: UInt) {
        var error: AgoraErrorCode = .noError
        let userInfoWithUid = agoraKit.getUserInfo(byUid: uid, withError: &error)
        if error == .noError {
            // Do something with AgoraUserInfo object
        }
    }
    

    【讨论】:

    • 您能提供一个代码示例吗?我不是很关注你。在文档中,我什至看不到 swift 的委托方法 ... (docs.agora.io/en/faq/string)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 2020-02-17
    • 1970-01-01
    相关资源
    最近更新 更多