[英]Agora.io - how to renew the token automatically in Flutter
我正在使用Flutter构建移动应用程序。 我正在使用agora制作视频聊天应用程序。 临时令牌是从 Agora 手动获取的,并保存在const token
中。 但是过了一定时间(即2天),token就过期了,我需要重新从Agora网站上获取新的token。 我想从 Flutter 应用程序中自动更新令牌。 我从这里得到了一个要使用的tokenPrivilegeWillExpire
回调事件。 医生在那里说:
该回调在token过期前30秒触发,提醒应用获取新的token。收到该回调后,需要在服务端生成新的token,并调用RtcChannel.renewToken将新的token传递给SDK。
我只是不明白如何在tokenPrivilegeWillExpire
事件中使用renewToken
方法。
Q1)如何实现tokenPrivilegeWillExpire
回调事件?
tokenPrivilegeWillExpire
事件在令牌过期前 30 秒触发。 我试图了解如果在视频聊天开始之前令牌已经过时,情况会怎样。 我的发现如下:
这个文档页面说:
onRequestToken
(或 Web 上的onTokenPrivilegeDidExpire
):令牌过期时发生。 收到该回调后,在服务端生成新的token,调用joinChannel,传递新的token加入频道。
关于onRequestToken()
回调,Java(不是 Flutter)文档在这里说:
在令牌过期时发生。
加入频道时指定token后,一定时间后token失效,重新连接服务器需要新的token。
收到此回调后,在您的应用服务器上生成一个新令牌,并调用
renewToken
将新令牌传递给 SDK。
Q2)如何实现onRequestToken
事件?
我的最小代码片段如下:
import 'package:agora_rtc_engine/rtc_engine.dart';
.....//code omitted
const appId = "01898f62b31d4028beb16eafee844314";
const token = "00601898f62b31d4028beb16eafee844314IAAKhlw1zAa6Q6mHUPsXm9M3q/nfY4ZXJLlaag4XhctOnBhhfAYAAAAAEADDRZkOFcnaYQEAAQAVydph";
............// code omitted
void main() => runApp(MaterialApp(home: LiveSession1to1()));
class LiveSession1to1 extends StatefulWidget {
@override
_LiveSession1to1State createState() => _LiveSession1to1State();
}
class _LiveSession1to1State extends State<LiveSession1to1> {
late RtcEngine _engine;
@override
void initState() {
super.initState();
setState(() {});
//initAgora();
}
Future<void> initAgora() async {
RtcEngineContext context = RtcEngineContext(appId);
_engine = await RtcEngine.createWithContext(context);
_engine.setEventHandler(
RtcEngineEventHandler(
joinChannelSuccess: (String channel, int uid, int elapsed) {
// code goes here
},
userJoined: (int uid, int elapsed) {
// code goes here
},
userOffline: (int uid, UserOfflineReason reason) {
//code goes here
},
tokenPrivilegeWillExpire: (token) async {
//I need to renew token here
},
),
);
}
@override
Widget build(BuildContext context) {
// Code omitted here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.