Skip to content

Commit fada29c

Browse files
committed
feat(): add sample usage for BuildTokenWithRtm2 method in RtcTokenBuilder2Sample.cpp to demonstrate enhanced RTC and RTM token generation capabilities
1 parent 1070f56 commit fada29c

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

DynamicKey/AgoraDynamicKey/cpp/sample/RtcTokenBuilder2Sample.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,11 @@ int main(int argc, char const *argv[]) {
6060
privilege_expiration_in_seconds);
6161
std::cout << "Token With RTM:" << result << std::endl;
6262

63+
result = RtcTokenBuilder2::BuildTokenWithRtm2(app_id, app_certificate, channel_name, account, UserRole::kRolePublisher, token_expiration_in_seconds,
64+
join_channel_privilege_expiration_in_seconds, pub_audio_privilege_expiration_in_seconds,
65+
pub_video_privilege_expiration_in_seconds, pub_data_stream_privilege_expiration_in_seconds, account,
66+
token_expiration_in_seconds);
67+
std::cout << "Token With RTM:" << result << std::endl;
68+
6369
return 0;
6470
}

DynamicKey/AgoraDynamicKey/cpp/src/RtcTokenBuilder2.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,51 @@ class RtcTokenBuilder2 {
273273
*/
274274
static std::string BuildTokenWithRtm(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
275275
const std::string& user_account, UserRole role, uint32_t token_expire, uint32_t privilege_expire = 0);
276+
277+
/**
278+
* Builds an RTC and RTM token with account.
279+
*
280+
* @param app_id The App ID issued to you by Agora.
281+
* @param app_certificate Certificate of the application that you registered in
282+
* the Agora Dashboard.
283+
* @param channel_name Unique channel name for the AgoraRTC session in the
284+
* string format. The string length must be less than 64 bytes. Supported
285+
* character scopes are:
286+
* - The 26 lowercase English letters: a to z.
287+
* - The 26 uppercase English letters: A to Z.
288+
* - The 10 digits: 0 to 9.
289+
* - The space.
290+
* - "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">",
291+
* "?", "@", "[", "]", "^", "_", " {", "}", "|", "~", ",".
292+
* @param rtc_account The user account.
293+
* @param rtc_role See #userRole.
294+
* - Role_Publisher = 1: RECOMMENDED. Use this role for a voice/video call or a
295+
* live broadcast.
296+
* - Role_Subscriber = 2: ONLY use this role if your live-broadcast scenario
297+
* requires authentication for
298+
* [Co-host](https://docs.agora.io/en/video-calling/get-started/authentication-workflow?#co-host-token-authentication).
299+
* In order for this role to take effect, please contact our support team to
300+
* enable authentication for Hosting-in for you. Otherwise, Role_Subscriber
301+
* still has the same privileges as Role_Publisher.
302+
* @param rtc_token_expire represented by the number of seconds elapsed since now.
303+
* If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set rtc_token_expire as 600(seconds).
304+
* @param join_channel_privilege_expire represented by the number of seconds elapsed since now.
305+
* If, for example, you want to join channel and expect stay in the channel for 10 minutes, set join_channel_privilege_expire as 600(seconds).
306+
* @param pub_audio_privilege_expire represented by the number of seconds elapsed since now.
307+
* If, for example, you want to enable publish audio privilege for 10 minutes, set pub_audio_privilege_expire as 600(seconds).
308+
* @param pub_video_privilege_expire represented by the number of seconds elapsed since now.
309+
* If, for example, you want to enable publish video privilege for 10 minutes, set pub_video_privilege_expire as 600(seconds).
310+
* @param pub_data_stream_privilege_expire represented by the number of seconds elapsed since now.
311+
* If, for example, you want to enable publish data stream privilege for 10 minutes, set pub_data_stream_privilege_expire as 600(seconds).
312+
* @param rtm_user_id The RTM user's account, max length is 255 Bytes.
313+
* @param rtm_token_expire represented by the number of seconds elapsed since now.
314+
* If, for example, you want to access the Agora Service within 10 minutes after the token is generated, set rtm_token_expire as 600(seconds).
315+
@return The RTC and RTM token.
316+
*/
317+
static std::string BuildTokenWithRtm2(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
318+
const std::string& rtc_account, UserRole rtc_role, uint32_t rtc_token_expire, uint32_t join_channel_privilege_expire,
319+
uint32_t pub_audio_privilege_expire, uint32_t pub_video_privilege_expire, uint32_t pub_data_stream_privilege_expire,
320+
const std::string& rtm_user_id, uint32_t rtm_token_expire);
276321
};
277322

278323
inline std::string RtcTokenBuilder2::BuildTokenWithUid(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
@@ -349,5 +394,29 @@ inline std::string RtcTokenBuilder2::BuildTokenWithRtm(const std::string& app_id
349394

350395
return token.Build();
351396
}
397+
398+
inline std::string RtcTokenBuilder2::BuildTokenWithRtm2(const std::string& app_id, const std::string& app_certificate, const std::string& channel_name,
399+
const std::string& rtc_account, UserRole rtc_role, uint32_t rtc_token_expire,
400+
uint32_t join_channel_privilege_expire, uint32_t pub_audio_privilege_expire,
401+
uint32_t pub_video_privilege_expire, uint32_t pub_data_stream_privilege_expire,
402+
const std::string& rtm_user_id, uint32_t rtm_token_expire) {
403+
std::unique_ptr<Service> rtc_service(new ServiceRtc(channel_name, rtc_account));
404+
AccessToken2 token(app_id, app_certificate, 0, rtc_token_expire);
405+
406+
rtc_service->AddPrivilege(ServiceRtc::kPrivilegeJoinChannel, join_channel_privilege_expire);
407+
if (rtc_role == UserRole::kRolePublisher) {
408+
rtc_service->AddPrivilege(ServiceRtc::kPrivilegePublishAudioStream, pub_audio_privilege_expire);
409+
rtc_service->AddPrivilege(ServiceRtc::kPrivilegePublishVideoStream, pub_video_privilege_expire);
410+
rtc_service->AddPrivilege(ServiceRtc::kPrivilegePublishDataStream, pub_data_stream_privilege_expire);
411+
}
412+
token.AddService(std::move(rtc_service));
413+
414+
std::unique_ptr<Service> rtm_service(new ServiceRtm(rtm_user_id));
415+
rtm_service->AddPrivilege(ServiceRtm::kPrivilegeLogin, rtm_token_expire);
416+
417+
token.AddService(std::move(rtm_service));
418+
419+
return token.Build();
420+
}
352421
} // namespace tools
353422
} // namespace agora

0 commit comments

Comments
 (0)