問題總結
我的目標是使用一個用 SwiftUI 撰寫的 iOS 應用程式來連接 AgoraRtcEngineKit。我想創建一個純音頻的應用程式,并允許主機廣播音頻并允許聽眾收聽。
Agora 需要使用代幣。
我根據此處找到的 Agora 教程使用 Node.js 創建了 Agora 令牌服務器:https : //www.agora.io/en/blog/how-to-build-a-token-server-for-agora-applications-using -nodejs/
這是我的 Agora-Node-TokenServer 中的 index.js。此代碼基于此處找到的 Agora 教程:https : //github.com/digitallysavvy/Agora-Node-TokenServer/blob/master/index.js
const express = require('express')
const path = require('path')
const {RtcTokenBuilder, RtcRole} = require('agora-access-token');
const PORT = process.env.PORT || 5000
if (!(process.env.APP_ID && process.env.APP_CERTIFICATE)) {
throw new Error('You must define an APP_ID and APP_CERTIFICATE');
}
const APP_ID = process.env.APP_ID;
const APP_CERTIFICATE = process.env.APP_CERTIFICATE;
const app = express();
const nocache = (req, resp, next) => {
resp.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
resp.header('Expires', '-1');
resp.header('Pragma', 'no-cache');
next();
};
const generateAccessToken = (req, resp) => {
resp.header('Access-Control-Allow-Origin', '*');
const channelName = req.query.channelName;if (!channelName) {
return resp.status(500).json({ 'error': 'channel is required' });
}
// get uid
let uid = req.query.uid;
if(!uid || uid == '') {
uid = 0;
}
// get rtc role
let rtcrole = RtcRole.SUBSCRIBER;
if (req.query.rtcrole == 'publisher') {
rtcrole = RtcRole.PUBLISHER;
}
// get the expire time
let expireTime = req.query.expireTime;
if (!expireTime || expireTime == '') {
expireTime = 3600;
} else {
expireTime = parseInt(expireTime, 10);
}
// calculate privilege expire time
const currentTime = Math.floor(Date.now() / 1000);
const privilegeExpireTime = currentTime expireTime;
const rtctoken = RtcTokenBuilder.buildTokenWithUid(APP_ID, APP_CERTIFICATE, channelName, uid, rtcrole, privilegeExpireTime);
return resp.json({ 'rtctoken': rtctoken });
};
app.get('/access_token', nocache, generateAccessToken);
app.listen(PORT, () => {
console.log(`Listening on port: ${PORT}`);
});
我所期望的 我所期望的是能夠通過 Agora 成功驗證我的 iOS 應用程式。
實際結果 我無法通過 Agora 進行身份驗證。
- 我的 Node.js 令牌服務器成功地向我的 iOS 應用程式發送了一個令牌。
- 但是當我使用 rtckit.joinChannel(byToken:...) 將上述令牌提交給 Agora 時,沒有任何反應。永遠不會到達 joinChannel 的完成塊。
- 作為一個實驗,我使用 rtckit.joinChannel(byToken:...) 從 Agora 的控制臺向 Agora 提交了“臨時令牌”,這個令牌被成功接受并達到了完成塊。
我試過的
我的 iOS 應用程式可以通過使用 Agora 控制臺創建的臨時令牌來連接 Agora。Agora 的控制臺允許開發人員創建臨時令牌來測驗他們的應用程式。由于我的應用程式能夠使用這些臨時令牌進行身份驗證,它向我表明問題出在我創建的 NodeJS 令牌服務器的某個地方?
我在 Stack Overflow 上查看過類似的問題,例如:
- 如何為直播流和加入頻道的agora RTC生成令牌
- 確保我的 APP_ID 和 APP_CERTIFICATE 與 Agora 控制臺中的建議匹配:Agora Video Calling Android get error code 101
以下是我的 iOS 應用程式中的相關代碼,僅供參考。我基于這里找到的 Agora 教程的代碼:https: //github.com/maxxfrazer/Agora-iOS-Swift-Example/blob/main/Agora-iOS-Example/AgoraToken.swift和這里:https://www .agora.io/en/blog/creating-live-audio-chat-rooms-with-swiftui/
AgoraToken
import Foundation
class AgoraToken {
/// Error types to expect from fetchToken on failing ot retrieve valid token.
enum TokenError: Error {
case noData
case invalidData
case invalidURL
}
/// Requests the token from our backend token service
/// - Parameter urlBase: base URL specifying where the token server is located
/// - Parameter channelName: Name of the channel we're requesting for
/// - Parameter uid: User ID of the user trying to join (0 for any user)
/// - Parameter callback: Callback method for returning either the string token or error
static func fetchToken(
urlBase: String, channelName: String, uid: UInt,
callback: @escaping (Result<String, Error>) -> Void
) {
guard let fullURL = URL(string: "\(urlBase)/?channelName=\(channelName)/&uid=\(uid)/") else {
callback(.failure(TokenError.invalidURL))
return
}
print("fullURL yields \(fullURL)")
var request = URLRequest(
url: fullURL,
timeoutInterval: 10
)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { data, response, err in
print("Within URLSession.shared.dataTask response is \(String(describing: response)) and err is \(String(describing: err))")
if let dataExists = data {
print(String(bytes: dataExists, encoding: String.Encoding.utf8) ?? "URLSession no data exists")
}
guard let data = data else {
if let err = err {
callback(.failure(err))
} else {
callback(.failure(TokenError.noData))
}
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseDict = responseJSON as? [String: Any], let rtctoken = responseDict["rtctoken"] as? String {
print("rtc token is \(rtctoken)")
callback(.success(rtctoken))
} else {
callback(.failure(TokenError.invalidData))
}
}
task.resume()
}
}
播客檔案
# Uncomment the next line to define a global platform for your project
platform :ios, '14.8.1'
target 'AgoraPractice' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for AgoraPractice
pod 'AgoraRtm_iOS'
pod 'AgoraAudio_iOS'
target 'AgoraPracticeTests' do
inherit! :search_paths
# Pods for testing
end
target 'AgoraPracticeUITests' do
# Pods for testing
end
end
內容視圖
import SwiftUI
import CoreData
import AgoraRtcKit
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@State var joinedChannel: Bool = false
@ObservedObject var agoraObservable = AgoraObservable()
var body: some View {
Form {
Section(header: Text("Channel Information")) {
TextField(
"Channel Name", text: $agoraObservable.channelName
).disabled(joinedChannel)
TextField(
"Username", text: $agoraObservable.username
).disabled(joinedChannel)
}
Button(action: {
joinedChannel.toggle()
if !joinedChannel {
self.agoraObservable.leaveChannel()
} else {
self.agoraObservable.checkIfChannelTokenExists()
}
}, label: {
Text("\(joinedChannel ? "Leave" : "Join") Channel")
.accentColor(joinedChannel ? .red : .blue)
})
if joinedChannel {
Button(action: {
agoraObservable.rtckit.setClientRole(.audience)
}, label: {
Text("Become audience member")
})
Button(action: {
agoraObservable.rtckit.setClientRole(.broadcaster)
}, label: {
Text("Become broadcasting member")
})
}
}
}
}
struct UserData: Codable {
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 remoteUserIDs: Set<UInt> = []
@Published var channelName: String = ""
@Published var username: String = ""
// Temp Token and Channel Name
var tempToken:String = "XXXXXXX"
var tempChannelName:String = "XXXXX"
var rtcId: UInt = 0
//var rtcId: UInt = 1264211369
let tokenBaseURL:String = Secrets.baseUrl
// channelToken is rtctoken. I should change this variable name at some point to rtctoken...
var channelToken:String = ""
lazy var rtckit: AgoraRtcEngineKit = {
let engine = AgoraRtcEngineKit.sharedEngine(
withAppId: Secrets.agoraAppId, delegate: self
)
engine.setChannelProfile(.liveBroadcasting)
engine.setClientRole(.audience)
return engine
}()
}
extension AgoraObservable {
func checkIfChannelTokenExists() {
if channelToken.isEmpty {
joinChannelWithFetch()
} else {
joinChannel()
}
}
func joinChannelWithFetch() {
AgoraToken.fetchToken(
urlBase: tokenBaseURL,
channelName: self.channelName,
uid: self.rtcId
) { result in
switch result {
case .success(let tokenExists):
self.channelToken = tokenExists
print("func joinChannelWithFetch(): channelToken = \(self.channelToken) and rtcuid = \(self.rtcId)")
self.joinChannel()
case .failure(let err):
print(err)
// To Do: Handle this error with an alert
self.leaveChannel()
}
}
}
func joinChannel(){
print("func joinChannel(): channelToken = \(self.channelToken) and channelName = \(self.channelName) and rtcuid = \(self.rtcId)")
self.rtckit.joinChannel(byToken: self.channelToken, channelId: self.channelName, info: nil, uid: self.rtcId) { [weak self] (channel, uid, errCode) in
print("within rtckit.joinchannel yields: channel:\(channel) and uid:\(uid) and error:\(errCode)")
self?.rtcId = uid
}
// I need to error handle if user cannot loginto rtckit
}
func updateToken(_ newToken:String){
channelToken = newToken
self.rtckit.renewToken(newToken)
print("Updating token now...")
}
func leaveChannel() {
self.rtckit.leaveChannel()
}
}
extension AgoraObservable: AgoraRtcEngineDelegate {
/// Called when the user role successfully changes
/// - Parameters:
/// - engine: AgoraRtcEngine of this session.
/// - oldRole: Previous role of the user.
/// - newRole: New role of the user.
func rtcEngine(
_ engine: AgoraRtcEngineKit,
didClientRoleChanged oldRole: AgoraClientRole,
newRole: AgoraClientRole
) {
print("AgoraRtcEngineDelegate didClientRoleChanged triggered...old role: \(oldRole), new role: \(newRole)")
}
func rtcEngine(
_ engine: AgoraRtcEngineKit,
didJoinedOfUid uid: UInt,
elapsed: Int
) {
// Keeping track of all people in the session
print("rtcEngine didJoinedOfUid triggered...")
remoteUserIDs.insert(uid)
}
func rtcEngine(
_ engine: AgoraRtcEngineKit,
didOfflineOfUid uid: UInt,
reason: AgoraUserOfflineReason
) {
print("rtcEngine didOfflineOfUid triggered...")
// Removing on quit and dropped only
// the other option is `.becomeAudience`,
// which means it's still relevant.
if reason == .quit || reason == .dropped {
remoteUserIDs.remove(uid)
} else {
// User is no longer hosting, need to change the lookups
// and remove this view from the list
// userVideoLookup.removeValue(forKey: uid)
}
}
func rtcEngine(
_ engine: AgoraRtcEngineKit,
tokenPrivilegeWillExpire token: String
) {
print("tokenPrivilegeWillExpire delegate method called...")
AgoraToken.fetchToken(
urlBase: tokenBaseURL, channelName: self.channelName, uid: self.rtcId) { result in
switch result {
case .failure(let err):
fatalError("Could not refresh token: \(err)")
case .success(let newToken):
print("token successfully updated")
self.updateToken(newToken)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
}
uj5u.com熱心網友回復:
事實證明,當我嘗試為我的 http 請求定義“fullURL”以獲取令牌時,我犯了一個錯誤。我是 http 請求的新手,所以我不知道我犯了錯誤。
在我的 AgoraToken.swift 檔案中,我錯誤的 fullURL 定義是:
guard let fullURL = URL(string: "\(urlBase)/?channelName=\(channelName)/&uid=\(uid)/") else {...
因為您將能夠看到您是否不是像我這樣的菜鳥,使用此代碼 channelName 和 uid 都將發送到我的令牌服務器,并帶有斜杠。因此,如果我的 channelName 是“TupperwareParty”,我的令牌服務器將得到“TupperwareParty/”,如果我的 uid 是“123456”,我的令牌服務器將得到“123456/”。
我用這個新的 fullURL 定義修復了它......
guard let fullURL = URL(string: "\(urlBase)/?channelName=\(channelName)&uid=\(uid)") else {...
嘆...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372874.html
下一篇:串列索引沒有回傳值
