client.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Demo的客户连接类
  4. */
  5. class Client extends Client_Base
  6. {
  7. /**
  8. * 是否登录
  9. */
  10. protected $logined = false;
  11. /**
  12. * 定时器的ID
  13. */
  14. protected $timerId;
  15. /**
  16. * 连接后的回调
  17. */
  18. public function onConnect()
  19. {
  20. //为了方便测试,注释掉验证登录的逻辑
  21. //$this->timerId = GET_TIMER()->add(3000, [$this, 'checkLogin']);
  22. }
  23. /**
  24. * 连接断开前的回调
  25. */
  26. public function onClose()
  27. {
  28. if ($this->logined) {
  29. unset($GLOBALS['users'][$this->id]);
  30. //广播离线通知给其他客户端
  31. $msg = new Message();
  32. $msg->set('cmd', 'leave');
  33. $msg->set('id', $this->id);
  34. $msg->set('name', $this->name);
  35. BC($msg);
  36. }
  37. }
  38. /**
  39. * 检查登录状态
  40. */
  41. public function checkLogin()
  42. {
  43. if (!$this->logined)
  44. {
  45. $msg = new Message();
  46. $msg->set('cmd', 'logout');
  47. $msg->set('data', 'byebye!');
  48. $this->response($msg, [$this, 'close']);
  49. }
  50. }
  51. /**
  52. * 实现的分发请求消息的接口
  53. */
  54. public function onRequest($msg)
  55. {
  56. $cmd = $msg->get('cmd');
  57. if (empty($cmd)) {
  58. return;
  59. }
  60. switch ($cmd) {
  61. case 'time': //请求服务器时间
  62. $this->onTime();
  63. break;
  64. case 'test':
  65. $this->onTest();
  66. break;
  67. case 'dl': //下载图片
  68. $this->onDownload();
  69. break;
  70. case 'login': //登录
  71. $this->onLogin($msg);
  72. break;
  73. case 'bc': //广播
  74. $this->onBC($msg);
  75. break;
  76. case 'talk': //私聊
  77. $this->onTalk($msg);
  78. break;
  79. }
  80. }
  81. /**
  82. * 返回服务器时间
  83. */
  84. protected function onTime()
  85. {
  86. $msg = new Message();
  87. $msg->set('cmd', 'time');
  88. $msg->set('data', time());
  89. $this->response($msg);
  90. }
  91. /**
  92. * 测试接口
  93. */
  94. protected function onTest()
  95. {
  96. $msg = new Message();
  97. $msg->set('cmd', 'time');
  98. $msg->set('data', 'Hello!');
  99. $this->response($msg);
  100. }
  101. protected $cache = '';
  102. /**
  103. * 测试接口
  104. */
  105. protected function onDownload()
  106. {
  107. if (empty($this->cache)) {
  108. $this->cache = base64_encode(file_get_contents(__DIR__ . '/test.jpg'));
  109. }
  110. $msg = new Message();
  111. $msg->set('cmd', 'download');
  112. $msg->set('data', $this->cache);
  113. $this->response($msg);
  114. }
  115. /**
  116. * 当前客户的名称
  117. * @var string
  118. */
  119. protected $name;
  120. /**
  121. * 返回当前客户的名称
  122. * @return string 客户的名称
  123. */
  124. public function getName()
  125. {
  126. return $this->name;
  127. }
  128. /**
  129. * 客户端登录处理
  130. * @param Message $msg 请求消息
  131. */
  132. protected function onLogin($msg)
  133. {
  134. $name = $msg->get('name');
  135. if (empty($name)) {
  136. return;
  137. }
  138. $this->name = $name;
  139. $this->logined = true;
  140. if ($this->timerId) {
  141. GET_TIMER()->clear($this->timerId);
  142. $this->timerId = 0;
  143. }
  144. $GLOBALS['users'][$this->id] = $this;
  145. //---- login ----
  146. $data = [];
  147. foreach ($GLOBALS['users'] as $id => $cli) {
  148. $data[$id] = $cli->getName();
  149. }
  150. $msg = new Message();
  151. $msg->set('cmd', 'login');
  152. $msg->set('id', $this->id);
  153. $msg->set('data', $data);
  154. $this->response($msg);
  155. //---- enter ----
  156. $bcMsg = new Message();
  157. $bcMsg->set('cmd', 'enter');
  158. $bcMsg->set('id', $this->id);
  159. $bcMsg->set('name', $name);
  160. BC($bcMsg);
  161. }
  162. /**
  163. * 客户端广播处理
  164. * @param Message $msg 请求消息
  165. */
  166. protected function onBC($msg)
  167. {
  168. $msg->set('id', $this->id);
  169. $msg->set('name', $this->name);
  170. BC($msg);
  171. }
  172. /**
  173. * 客户端私聊处理
  174. * @param Message $msg 请求消息
  175. */
  176. protected function onTalk($msg)
  177. {
  178. $to = $msg->get('to');
  179. if (isset($GLOBALS['users'][$to])) {
  180. $msg->set('id', $this->id);
  181. $msg->set('name', $this->name);
  182. $msg->set('to_name', $GLOBALS['users'][$to]->getName());
  183. $this->response($msg);
  184. $GLOBALS['users'][$to]->response($msg);
  185. }
  186. }
  187. }