RoomClient.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Qiniu\Pili;
  3. class RoomClient
  4. {
  5. private $_transport;
  6. private $_mac;
  7. private $_baseURL;
  8. public function __construct($mac)
  9. {
  10. $this->_mac = $mac;
  11. $this->_transport = new Transport($mac);
  12. $cfg = Config::getInstance();
  13. $this->_baseURL = sprintf("%s/%s/rooms", $cfg->RTCAPI_HOST, $cfg->RTCAPI_VERSION);
  14. }
  15. /*
  16. * ownerId: 要创建房间的所有者
  17. * roomName: 房间名称
  18. */
  19. public function createRoom($ownerId, $roomName = null)
  20. {
  21. $params['owner_id'] = $ownerId;
  22. if (!empty($roomName)) {
  23. $params['room_name'] = $roomName;
  24. }
  25. $body = json_encode($params);
  26. try {
  27. $ret = $this->_transport->send(HttpRequest::POST, $this->_baseURL, $body);
  28. } catch (\Exception $e) {
  29. return $e;
  30. }
  31. return $ret;
  32. }
  33. /*
  34. * roomName: 房间名称
  35. */
  36. public function getRoom($roomName)
  37. {
  38. $url = $this->_baseURL . '/' . $roomName;
  39. try {
  40. $ret = $this->_transport->send(HttpRequest::GET, $url);
  41. } catch (\Exception $e) {
  42. return $e;
  43. }
  44. return $ret;
  45. }
  46. /*
  47. * roomName: 房间名称
  48. */
  49. public function deleteRoom($roomName)
  50. {
  51. $url = $this->_baseURL . '/' . $roomName;
  52. try {
  53. $ret = $this->_transport->send(HttpRequest::DELETE, $url);
  54. } catch (\Exception $e) {
  55. return $e;
  56. }
  57. return $ret;
  58. }
  59. /*
  60. * 获取房间的人数
  61. * roomName: 房间名称
  62. */
  63. public function getRoomUserNum($roomName)
  64. {
  65. $url = sprintf("%s/%s/users", $this->_baseURL, $roomName);
  66. try {
  67. $ret = $this->_transport->send(HttpRequest::GET, $url);
  68. } catch (\Exception $e) {
  69. return $e;
  70. }
  71. return $ret;
  72. }
  73. /*
  74. * 踢出玩家
  75. * roomName: 房间名称
  76. * userId: 请求加入房间的用户ID
  77. */
  78. public function kickingPlayer($roomName, $UserId)
  79. {
  80. $url = sprintf("%s/%s/users/%s", $this->_baseURL, $roomName, $UserId);
  81. try {
  82. $ret = $this->_transport->send(HttpRequest::DELETE, $url);
  83. } catch (\Exception $e) {
  84. return $e;
  85. }
  86. return $ret;
  87. }
  88. /*
  89. * roomName: 房间名称
  90. * userId: 请求加入房间的用户ID
  91. * perm: 该用户的房间管理权限,"admin"或"user",房间主播为"admin",拥有将其他用户移除出房间等特权。
  92. * expireAt: int64类型,鉴权的有效时间,传入秒为单位的64位Unix时间,token将在该时间后失效。
  93. */
  94. public function roomToken($roomName, $userId, $perm, $expireAt)
  95. {
  96. $ver = Config::getInstance()->RTCAPI_VERSION;
  97. if ($ver === 'v2') {
  98. $params['version']="2.0";
  99. }
  100. $params['room_name'] = $roomName;
  101. $params['user_id'] = $userId;
  102. $params['perm'] = $perm;
  103. $params['expire_at'] = $expireAt;
  104. $roomAccessString = json_encode($params);
  105. $encodedRoomAccess = Utils::base64UrlEncode($roomAccessString);
  106. $sign = hash_hmac('sha1', $encodedRoomAccess, $this->_mac->_secretKey, true);
  107. $encodedSign = Utils::base64UrlEncode($sign);
  108. return $this->_mac->_accessKey . ":" . $encodedSign . ":" . $encodedRoomAccess;
  109. }
  110. }