123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- // +----------------------------------------------------------------------
- // | framework
- // +----------------------------------------------------------------------
- // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
- // +----------------------------------------------------------------------
- // | 官方网站: http://framework.thinkadmin.top
- // +----------------------------------------------------------------------
- // | 开源协议 ( https://mit-license.org )
- // +----------------------------------------------------------------------
- // | github开源项目:https://github.com/zoujingli/framework
- // +----------------------------------------------------------------------
- namespace app\act\socket;
- use think\worker\Server;
- class Worker extends Server
- {
- protected $clients = []; //保存客户端信息
- protected $socket = 'websocket://0.0.0.0:2346';
- /**
- * 收到信息
- * @param $connection
- * @param $data
- */
- public function onMessage($connection, $data)
- {
- if (preg_match('/^login:(\w{3,20})/i', $data, $result)) {
- $ip = $connection->getRemoteIp(); //获取当前客户端IP
- $port = $connection->getRemotePort(); //获取当前客户端端口
- if (!array_key_exists($ip, $this->clients)) {
- $this->clients[$ip.':'.$port] = ['ipp'=>$ip.':'.$port,'name'=>$result[1],'conn'=>$connection]; //广播的话,不止保存昵称,还要保存ip和当前和服务器连接的那个客户端
- //将处理完的信息返回给客户端(给客户端发送任意消息)
- $connection->send('notice:success');
- $connection->send('msg:你好'.$result[1]);
- echo $ip . ':'.$port . '--------' .$result[1] . 'login' . PHP_EOL; //打印看结果
-
- //一旦有用户登录,就把保存的客户端信息发过去(显示出所有用户)
- //$connection->send('users:'.json_encode($clients));
- //广播(群聊)
- $users = 'users:'.json_encode(array_column($this->clients,'name','ipp')); //返回数组中指定的列
- foreach($this->clients as $ip=>$client){
- //拿当前和服务器连接的那个客户端,发送消息
- $client['conn']->send($users);
- }
- }
- } elseif(preg_match('/^msg:(.*?)/isU',$data,$megset)) {
- //2.2、处理发来的普通消息
- if(array_key_exists($connection->getRemoteIp(),$this->clients)) { //判断该ip是否存在,存在就是已经登录的
- echo '用户:' . $connection->getRemoteIp() . '发的消息是' . $megset[1] . PHP_EOL;
- if($megset[1] == 'nihao'){
- $connection->send('msg:nihao'.$this->clients[$connection->getRemoteIp()]);
- }
- //我认为广播应该在这些,将用户A说的话,显示到页面上,让所有用户都能看见
- }
- } elseif(preg_match('/^dian:\<(.*?)\>:(.*?)/isU',$data,$meg)) {
- //单播,点对点发消息
- $ipp = $meg[1]; //接收消息用户的ip
- $msg = $meg[2]; //发送的数据
- $name = $this->clients[$ipp]['name'];
- echo "<pre>";
- var_dump($name);
- if(array_key_exists($ipp,$this->clients)){ //接收的ip也登录了,也就是有这个用户
- $this->clients[$ipp]['conn']->send('dian:'.$msg);
- echo $ipp.'==>'.$msg.PHP_EOL;
- }
- }
- }
- /**
- * 当连接建立时触发的回调函数
- * @param $connection
- */
- public function onConnect($connection)
- {
- }
- /**
- * 当连接断开时触发的回调函数
- * @param $connection
- */
- public function onClose($connection)
- {
- unset($this->clients[$connection->getRemoteIp()]);
- }
- /**
- * 当客户端的连接上发生错误时触发
- * @param $connection
- * @param $code
- * @param $msg
- */
- public function onError($connection, $code, $msg)
- {
- echo "error $code $msg\n";
- }
- /**
- * 每个进程启动
- * @param $worker
- */
- public function onWorkerStart($worker)
- {
- }
- }
|