1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/12/14
- * Time: 上午11:43
- */
- namespace room;
- use search\tcp_client;
- use algorithm;
- class room_client extends tcp_client
- {
- protected static $stInstance;
- private $mHost;
- private $mPort;
- private $mRooms;
- public function __construct($host,$port)
- {
- $this->mHost = $host;
- $this->mPort = $port;
- $this->mRooms = [];
- parent::__construct();
- $this->mBodyType = tcp_client::JsonType;
- $this->init();
- }
- public function __destruct()
- {
- parent::__destruct();
- }
- public function init()
- {
- $ret = $this->list_room();
- if($ret != false)
- {
- $rooms = $ret['rooms'];
- foreach ($rooms as $roomid) {
- $this->add_room($roomid);
- }
- }
- }
- public function contain_room($roomid)
- {
- $roomid = intval($roomid);
- return algorithm::binary_search($this->mRooms,$roomid);
- }
- public function add_room($roomid)
- {
- $room = intval($roomid);
- if(algorithm::binary_search($this->mRooms,$room) == false) {
- $pos = algorithm::lower_bonud($this->mRooms,$room);
- algorithm::array_insert($this->mRooms,$pos,$room);
- }
- }
- private function list_room()
- {
- $param = ["act" => 'factory','op' => 'list_room'];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data'];
- }
- }
- public function remote_addr() {
- return "{$this->mHost}:{$this->mPort}";
- }
- public function invite($roomid,$user)
- {
- $param = ["act" => 'factory','op' => 'invite','room' => $roomid,'user' => $user];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data'];
- }
- }
- }
|