AgoraKit 无法向远程用户发送 `userinfo`

AgoraKit cannot send `userinfo` to remote users

我正在将 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)")
        }

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

    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

    }

在鲍勃这边,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 已过时。

假设您已经使用 registerLocalUserAccount:appId: 方法注册了 Alice 和 Bob。他们都必须使用 joinChannelByUserAccount:token:channelId:joinSuccess: 方法加入频道。确保 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: 委托方法时。

我从你的代码片段中猜测你的 class 已经符合 AgoraRtcEngineDelegate 协议。所以你所要做的就是实现这个委托方法并在方法体内调用getUserInfoByUid:withError:

示例:

第 1 步:

让你的class符合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 步:

用户注册成功后加入频道

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