room_client.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/12/14
  6. * Time: 上午11:43
  7. */
  8. namespace room;
  9. use search\tcp_client;
  10. use algorithm;
  11. class room_client extends tcp_client
  12. {
  13. protected static $stInstance;
  14. private $mHost;
  15. private $mPort;
  16. private $mRooms;
  17. private $mBufid;
  18. public function __construct($host,$port,$socket = false,$auto_con = true)
  19. {
  20. $this->mHost = $host;
  21. $this->mPort = $port;
  22. $this->mRooms = [];
  23. $this->mBufid = 0;
  24. parent::__construct($socket,$auto_con);
  25. $this->mBodyType = tcp_client::JsonType;
  26. }
  27. public function __destruct()
  28. {
  29. parent::__destruct();
  30. }
  31. public function buffer_id() {
  32. return $this->mBufid;
  33. }
  34. public function init_rooms($bufid)
  35. {
  36. $this->mBufid = $bufid;
  37. $ret = $this->list_room();
  38. if($ret != false)
  39. {
  40. $rooms = $ret['rooms'];
  41. foreach ($rooms as $roomid) {
  42. $this->add_room($roomid);
  43. }
  44. }
  45. }
  46. public function contain_room($roomid)
  47. {
  48. $roomid = intval($roomid);
  49. return algorithm::binary_search($this->mRooms,$roomid);
  50. }
  51. public function add_room($roomid)
  52. {
  53. $room = intval($roomid);
  54. if(algorithm::binary_search($this->mRooms,$room) == false) {
  55. $pos = algorithm::lower_bonud($this->mRooms,$room);
  56. algorithm::array_insert($this->mRooms,$pos,$room);
  57. }
  58. }
  59. private function list_room()
  60. {
  61. $param = ["act" => 'factory','op' => 'list_room'];
  62. $result = $this->request($param);
  63. if(empty($result)) return false;
  64. $code = intval($result['code']);
  65. if($code != 200) {
  66. return false;
  67. }
  68. else {
  69. return $result['data'];
  70. }
  71. }
  72. public function remote_addr() {
  73. return "tcp://{$this->mHost}:{$this->mPort}";
  74. }
  75. public function host_port() {
  76. return ['host' => $this->mHost,'port' => $this->mPort];
  77. }
  78. public function build($roomid,$newroom = false)
  79. {
  80. $param = ["act" => 'factory','op' => 'build','room' => $roomid,'newroom' => $newroom];
  81. $result = $this->request($param);
  82. if(empty($result)) return false;
  83. $code = intval($result['code']);
  84. if($code != 200) {
  85. return false;
  86. }
  87. else {
  88. return $result['data'];
  89. }
  90. }
  91. public function change($roomid)
  92. {
  93. $param = ["act" => 'factory','op' => 'change','room' => $roomid];
  94. $result = $this->request($param);
  95. if(empty($result)) return false;
  96. $code = intval($result['code']);
  97. if($code != 200) {
  98. return false;
  99. }
  100. else {
  101. return $result['data'];
  102. }
  103. }
  104. public function invite($roomid, $inviter,$invitees)
  105. {
  106. $param = ["act" => 'factory','op' => 'invite','room' => $roomid,'inviter' => $inviter,'invitees' => $invitees];
  107. $result = $this->request($param);
  108. if(empty($result)) return false;
  109. $code = intval($result['code']);
  110. if($code != 200) {
  111. return false;
  112. }
  113. else {
  114. return $result['data'];
  115. }
  116. }
  117. public function leave($roomid, $user)
  118. {
  119. $param = ["act" => 'factory','op' => 'leave','room' => $roomid,'user' => $user];
  120. $result = $this->request($param);
  121. if(empty($result)) return false;
  122. $code = intval($result['code']);
  123. if($code != 200) {
  124. return false;
  125. }
  126. else {
  127. return $result['data'];
  128. }
  129. }
  130. public function kickout($roomid, $user,$kicks)
  131. {
  132. $param = ["act" => 'factory','op' => 'kickout','room' => $roomid,'user' => $user,'kicks' => $kicks];
  133. $result = $this->request($param);
  134. if(empty($result)) return false;
  135. $code = intval($result['code']);
  136. if($code != 200) {
  137. return false;
  138. }
  139. else {
  140. return $result['data'];
  141. }
  142. }
  143. public function notice_room($room,$type,$content,$user)
  144. {
  145. $param = ["act" => 'factory','op' => 'notice_room','room' => $room,'type' => $type,'content' => $content,'user' => $user];
  146. $result = $this->request($param);
  147. if(empty($result)) return false;
  148. $code = intval($result['code']);
  149. if($code != 200) {
  150. return false;
  151. }
  152. else {
  153. return true;
  154. }
  155. }
  156. public function notice_all($type,$content,$msg)
  157. {
  158. $param = ["act" => 'factory','op' => 'notice_all','type' => $type,'content' => $content,'msg' => $msg];
  159. $result = $this->request($param);
  160. if(empty($result)) return false;
  161. $code = intval($result['code']);
  162. if($code != 200) {
  163. return false;
  164. } else {
  165. return true;
  166. }
  167. }
  168. public function notice_users($users,$type,$content,$msg)
  169. {
  170. $param = ["act" => 'factory','op' => 'notice_users','users' => $users,'type' => $type,'content' => $content,'msg' => $msg];
  171. $result = $this->request($param);
  172. if(empty($result)) return false;
  173. $code = intval($result['code']);
  174. if($code != 200) {
  175. return false;
  176. } else {
  177. return true;
  178. }
  179. }
  180. public function message($room,$user,$type,$content)
  181. {
  182. $param = ["act" => 'room','op' => 'message','room' => $room,'user' => $user,'type' => $type,'content' => $content];
  183. $result = $this->request($param);
  184. if(empty($result)) return false;
  185. $code = intval($result['code']);
  186. if($code != 200) {
  187. return false;
  188. }
  189. else {
  190. return true;
  191. }
  192. }
  193. public function push($content)
  194. {
  195. $param = ["act" => 'factory','op' => 'push','content' => $content];
  196. $result = $this->request($param);
  197. if(empty($result)) return false;
  198. $code = intval($result['code']);
  199. if($code != 200) {
  200. return false;
  201. }
  202. else {
  203. return true;
  204. }
  205. }
  206. }