Worker_old.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\act\socket;
  14. use think\worker\Server;
  15. class Worker extends Server
  16. {
  17. protected $clients = []; //保存客户端信息
  18. protected $socket = 'websocket://0.0.0.0:2346';
  19. /**
  20. * 收到信息
  21. * @param $connection
  22. * @param $data
  23. */
  24. public function onMessage($connection, $data)
  25. {
  26. if (preg_match('/^login:(\w{3,20})/i', $data, $result)) {
  27. $ip = $connection->getRemoteIp(); //获取当前客户端IP
  28. $port = $connection->getRemotePort(); //获取当前客户端端口
  29. if (!array_key_exists($ip, $this->clients)) {
  30. $this->clients[$ip.':'.$port] = ['ipp'=>$ip.':'.$port,'name'=>$result[1],'conn'=>$connection]; //广播的话,不止保存昵称,还要保存ip和当前和服务器连接的那个客户端
  31. //将处理完的信息返回给客户端(给客户端发送任意消息)
  32. $connection->send('notice:success');
  33. $connection->send('msg:你好'.$result[1]);
  34. echo $ip . ':'.$port . '--------' .$result[1] . 'login' . PHP_EOL; //打印看结果
  35. //一旦有用户登录,就把保存的客户端信息发过去(显示出所有用户)
  36. //$connection->send('users:'.json_encode($clients));
  37. //广播(群聊)
  38. $users = 'users:'.json_encode(array_column($this->clients,'name','ipp')); //返回数组中指定的列
  39. foreach($this->clients as $ip=>$client){
  40. //拿当前和服务器连接的那个客户端,发送消息
  41. $client['conn']->send($users);
  42. }
  43. }
  44. } elseif(preg_match('/^msg:(.*?)/isU',$data,$megset)) {
  45. //2.2、处理发来的普通消息
  46. if(array_key_exists($connection->getRemoteIp(),$this->clients)) { //判断该ip是否存在,存在就是已经登录的
  47. echo '用户:' . $connection->getRemoteIp() . '发的消息是' . $megset[1] . PHP_EOL;
  48. if($megset[1] == 'nihao'){
  49. $connection->send('msg:nihao'.$this->clients[$connection->getRemoteIp()]);
  50. }
  51. //我认为广播应该在这些,将用户A说的话,显示到页面上,让所有用户都能看见
  52. }
  53. } elseif(preg_match('/^dian:\<(.*?)\>:(.*?)/isU',$data,$meg)) {
  54. //单播,点对点发消息
  55. $ipp = $meg[1]; //接收消息用户的ip
  56. $msg = $meg[2]; //发送的数据
  57. $name = $this->clients[$ipp]['name'];
  58. echo "<pre>";
  59. var_dump($name);
  60. if(array_key_exists($ipp,$this->clients)){ //接收的ip也登录了,也就是有这个用户
  61. $this->clients[$ipp]['conn']->send('dian:'.$msg);
  62. echo $ipp.'==>'.$msg.PHP_EOL;
  63. }
  64. }
  65. }
  66. /**
  67. * 当连接建立时触发的回调函数
  68. * @param $connection
  69. */
  70. public function onConnect($connection)
  71. {
  72. }
  73. /**
  74. * 当连接断开时触发的回调函数
  75. * @param $connection
  76. */
  77. public function onClose($connection)
  78. {
  79. unset($this->clients[$connection->getRemoteIp()]);
  80. }
  81. /**
  82. * 当客户端的连接上发生错误时触发
  83. * @param $connection
  84. * @param $code
  85. * @param $msg
  86. */
  87. public function onError($connection, $code, $msg)
  88. {
  89. echo "error $code $msg\n";
  90. }
  91. /**
  92. * 每个进程启动
  93. * @param $worker
  94. */
  95. public function onWorkerStart($worker)
  96. {
  97. }
  98. }