gpt4 book ai didi

ios - AgoraKit 无法发送 `userinfo` 给远程用户

转载 作者:行者123 更新时间:2023-12-01 21:46:59 26 4
gpt4 key购买 nike

我正在将 Agora 集成到我的 iOS Swift 5 应用程序中,基本用例揭示了莫名其妙的行为。假设爱丽丝调用鲍勃,当爱丽丝加入指定用户信息的 channel 时:

        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)")
}

这完全失败了。我在这里引用文档:
  • Building a 1-to-many iOS video app with Agora
  • FAQ - Strings

  • 在第二种情况下,文档有拼写错误,并且 API 已过时。

    最佳答案

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

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

    After a user joins the channel, the SDK gets the user ID and user account of the remote user, caches them in a mapping table object (AgoraUserInfo), and triggers the didUpdatedUserInfo callback on the local client. After receiving the didUpdatedUserInfo callback, you can call this method to get the user account of the user from the userInfo object by passing in the user ID.



    这意味着调用 getUserInfoByUid:withError: 的正确时机是什么时候 rtcEngine:didUpdatedUserInfo:withUid:委托(delegate)方法被调用。

    我从您的片段中猜测您的类(class)已经符合 AgoraRtcEngineDelegate 协议(protocol)。所以你所要做的就是实现这个委托(delegate)方法并调用 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
    }

    第 3 步:

    用户注册成功后加入 channel 。

    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:委托(delegate)方法。

    // 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
    }
    }

    关于ios - AgoraKit 无法发送 `userinfo` 给远程用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62120175/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com