123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?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;
- private $mBufid;
- public function __construct($host,$port,$socket = false,$auto_con = true)
- {
- $this->mHost = $host;
- $this->mPort = $port;
- $this->mRooms = [];
- $this->mBufid = 0;
- parent::__construct($socket,$auto_con);
- $this->mBodyType = tcp_client::JsonType;
- }
- public function __destruct()
- {
- parent::__destruct();
- }
- public function buffer_id() {
- return $this->mBufid;
- }
- public function init_rooms($bufid)
- {
- $this->mBufid = $bufid;
- $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 "tcp://{$this->mHost}:{$this->mPort}";
- }
- public function host_port() {
- return ['host' => $this->mHost,'port' => $this->mPort];
- }
- public function build($roomid,$newroom = false)
- {
- $param = ["act" => 'factory','op' => 'build','room' => $roomid,'newroom' => $newroom];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data'];
- }
- }
- public function change($roomid)
- {
- $param = ["act" => 'factory','op' => 'change','room' => $roomid];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data'];
- }
- }
- public function invite($roomid, $inviter,$invitees)
- {
- $param = ["act" => 'factory','op' => 'invite','room' => $roomid,'inviter' => $inviter,'invitees' => $invitees];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data'];
- }
- }
- public function leave($roomid, $user)
- {
- $param = ["act" => 'factory','op' => 'leave','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'];
- }
- }
- public function kickout($roomid, $user,$kicks)
- {
- $param = ["act" => 'factory','op' => 'kickout','room' => $roomid,'user' => $user,'kicks' => $kicks];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return $result['data'];
- }
- }
- public function notice_room($room,$type,$content,$user)
- {
- $param = ["act" => 'factory','op' => 'notice_room','room' => $room,'type' => $type,'content' => $content,'user' => $user];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return true;
- }
- }
- public function notice_all($type,$content,$msg)
- {
- $param = ["act" => 'factory','op' => 'notice_all','type' => $type,'content' => $content,'msg' => $msg];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- } else {
- return true;
- }
- }
- public function notice_users($users,$type,$content,$msg)
- {
- $param = ["act" => 'factory','op' => 'notice_users','users' => $users,'type' => $type,'content' => $content,'msg' => $msg];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- } else {
- return true;
- }
- }
- public function message($room,$user,$type,$content)
- {
- $param = ["act" => 'room','op' => 'message','room' => $room,'user' => $user,'type' => $type,'content' => $content];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return true;
- }
- }
- public function push($content)
- {
- $param = ["act" => 'factory','op' => 'push','content' => $content];
- $result = $this->request($param);
- if(empty($result)) return false;
- $code = intval($result['code']);
- if($code != 200) {
- return false;
- }
- else {
- return true;
- }
- }
- }
|