room_client.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. public function __construct($host,$port)
  18. {
  19. $this->mHost = $host;
  20. $this->mPort = $port;
  21. $this->mRooms = [];
  22. parent::__construct();
  23. $this->mBodyType = tcp_client::JsonType;
  24. $this->init();
  25. }
  26. public function __destruct()
  27. {
  28. parent::__destruct();
  29. }
  30. public function init()
  31. {
  32. $ret = $this->list_room();
  33. if($ret != false)
  34. {
  35. $rooms = $ret['rooms'];
  36. foreach ($rooms as $roomid) {
  37. $this->add_room($roomid);
  38. }
  39. }
  40. }
  41. public function contain_room($roomid)
  42. {
  43. $roomid = intval($roomid);
  44. return algorithm::binary_search($this->mRooms,$roomid);
  45. }
  46. public function add_room($roomid)
  47. {
  48. $room = intval($roomid);
  49. if(algorithm::binary_search($this->mRooms,$room) == false) {
  50. $pos = algorithm::lower_bonud($this->mRooms,$room);
  51. algorithm::array_insert($this->mRooms,$pos,$room);
  52. }
  53. }
  54. private function list_room()
  55. {
  56. $param = ["act" => 'factory','op' => 'list_room'];
  57. $result = $this->request($param);
  58. if(empty($result)) return false;
  59. $code = intval($result['code']);
  60. if($code != 200) {
  61. return false;
  62. }
  63. else {
  64. return $result['data'];
  65. }
  66. }
  67. public function remote_addr() {
  68. return "{$this->mHost}:{$this->mPort}";
  69. }
  70. public function invite($roomid,$user)
  71. {
  72. $param = ["act" => 'factory','op' => 'invite','room' => $roomid,'user' => $user];
  73. $result = $this->request($param);
  74. if(empty($result)) return false;
  75. $code = intval($result['code']);
  76. if($code != 200) {
  77. return false;
  78. }
  79. else {
  80. return $result['data'];
  81. }
  82. }
  83. }