RtcTokenBuilder.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace app\common\library\agora\token;
  3. class RtcTokenBuilder
  4. {
  5. const RoleAttendee = 0;
  6. const RolePublisher = 1;
  7. const RoleSubscriber = 2;
  8. const RoleAdmin = 101;
  9. # appID: The App ID issued to you by Agora. Apply for a new App ID from
  10. # Agora Dashboard if it is missing from your kit. See Get an App ID.
  11. # appCertificate: Certificate of the application that you registered in
  12. # the Agora Dashboard. See Get an App Certificate.
  13. # channelName:Unique channel name for the AgoraRTC session in the string format
  14. # uid: User ID. A 32-bit unsigned integer with a value ranging from
  15. # 1 to (232-1). optionalUid must be unique.
  16. # role: Role_Publisher = 1: A broadcaster (host) in a live-broadcast profile.
  17. # Role_Subscriber = 2: (Default) A audience in a live-broadcast profile.
  18. # privilegeExpireTs: represented by the number of seconds elapsed since
  19. # 1/1/1970. If, for example, you want to access the
  20. # Agora Service within 10 minutes after the token is
  21. # generated, set expireTimestamp as the current
  22. # timestamp + 600 (seconds)./
  23. public static function buildTokenWithUid($appID, $appCertificate, $channelName, $uid, $role, $privilegeExpireTs){
  24. return RtcTokenBuilder::buildTokenWithUserAccount($appID, $appCertificate, $channelName, $uid, $role, $privilegeExpireTs);
  25. }
  26. # appID: The App ID issued to you by Agora. Apply for a new App ID from
  27. # Agora Dashboard if it is missing from your kit. See Get an App ID.
  28. # appCertificate: Certificate of the application that you registered in
  29. # the Agora Dashboard. See Get an App Certificate.
  30. # channelName:Unique channel name for the AgoraRTC session in the string format
  31. # userAccount: The user account.
  32. # role: Role_Publisher = 1: A broadcaster (host) in a live-broadcast profile.
  33. # Role_Subscriber = 2: (Default) A audience in a live-broadcast profile.
  34. # privilegeExpireTs: represented by the number of seconds elapsed since
  35. # 1/1/1970. If, for example, you want to access the
  36. # Agora Service within 10 minutes after the token is
  37. # generated, set expireTimestamp as the current
  38. public static function buildTokenWithUserAccount($appID, $appCertificate, $channelName, $userAccount, $role, $privilegeExpireTs){
  39. $token = AccessToken::init($appID, $appCertificate, $channelName, $userAccount);
  40. $Privileges = AccessToken::Privileges;
  41. $token->addPrivilege($Privileges["kJoinChannel"], $privilegeExpireTs);
  42. if(($role == RtcTokenBuilder::RoleAttendee) ||
  43. ($role == RtcTokenBuilder::RolePublisher) ||
  44. ($role == RtcTokenBuilder::RoleAdmin))
  45. {
  46. $token->addPrivilege($Privileges["kPublishVideoStream"], $privilegeExpireTs);
  47. $token->addPrivilege($Privileges["kPublishAudioStream"], $privilegeExpireTs);
  48. $token->addPrivilege($Privileges["kPublishDataStream"], $privilegeExpireTs);
  49. }
  50. return $token->build();
  51. }
  52. }
  53. ?>