vali 6 лет назад
Родитель
Сommit
fda7c2d48b

+ 177 - 0
websockServ/app/lib/DB_pool.php

@@ -0,0 +1,177 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/27
+ * Time: 8:56
+ */
+
+namespace app\lib;
+
+
+/**
+ *  swoole 数据库连接池 BY 凌晨
+ * 'worker_num' => 20, //worker进程数量
+ * 'task_worker_num' => 10, //task进程数量 即为维持的MySQL连接的数量
+ * 'daemonize'=> 1,          //设置守护进程
+ * 'max_request' => 10000, //最大请求数,超过了进程重启
+ * 'dispatch_mode' => 2,/
+ */
+class DB_pool
+{    //swoole set params
+    protected $task_worker_num;
+    protected $work_num;
+    protected $max_request;
+    protected $dispatch_mode;
+    protected $daemonize;
+    protected $server_port;
+    protected $log_file;
+    //db params
+    protected $db_type;
+    protected $db_host;
+    protected $db_user;
+    protected $db_pwd;
+    protected $db_name;
+    protected $db_port;
+
+    public function __construct($config = [])
+    {
+        $this->host = $config['poole_host'] ? $config['poole_host'] : "127.0.0.1"; // server监听的端口
+        $this->server_port = $config['poole_port'] ? $config['poole_port'] : 9091;  // server监听的端口
+        $this->worker_num = 5;
+        $this->task_worker_num = 5;
+        $this->dispatch_mode = 2;
+        $this->daemonize = 0;
+        $this->max_request = 10000;
+        $filename = date("Y-m-d", time());
+        $this->log_file = "../logs/mysqlpoole_" . $filename . '.log';
+        $this->serv = new \swoole\server($this->host, $this->server_port);
+        $this->serv->set(array(
+            'worker_num' => $this->worker_num,
+            'task_worker_num' => $this->task_worker_num,
+            'max_request' => $this->max_request,
+            'daemonize' => $this->daemonize,
+            'log_file' => $this->log_file,
+            'dispatch_mode' => $this->dispatch_mode,
+            'package_max_length'=> 4 * 1024 *1024,
+            'buffer_output_size'=> 4 * 1024 *1024,
+            'socket_buffer_size' => 8 * 1024 *1024
+        ));
+
+        $this->db_type = $config['db_type'] ? $config['db_type'] : 'mysql';
+        $this->db_host = $config['db_host'] ? $config['db_host'] : '127.0.0.1';
+        $this->db_port = $config['db_port'] ? $config['db_port'] : 3306;
+        $this->db_name = $config['db_name'] ? $config['db_name'] : 'test';
+        $this->db_user = $config['db_user'] ? $config['db_user'] : 'test';
+        $this->db_pwd = $config['db_pwd'] ? $config['db_pwd'] : 'test';
+    }
+
+    public function run()
+    {
+        $this->serv->on('Receive', array($this, 'onReceive'));
+        // Task 回调的2个必须函数
+        $this->serv->on('Task', array($this, 'onTask'));
+        $this->serv->on('Connect', array($this, 'onConnect'));
+        $this->serv->on('Finish', array($this, 'onFinish'));
+        //$this->serv->on('Error', array($this, 'onError'));
+
+        $this->serv->start();
+    }
+
+    public function onConnect(\swoole\server $server, int $fd, int $reactorId)
+    {
+        //$clientinfo = $server->getClientInfo($fd) ;
+        //$cinfo = $clientinfo['remote_ip'].':'. $clientinfo['remote_port'] ;
+        //echo "有客户接入:reactorId:$reactorId --> fd:$fd  --> $cinfo  \n";
+    }
+
+
+    public function onReceive($serv, $fd, $from_id, $data)
+    {
+        $resultStr = $this->serv->taskwait($data);
+        if ($resultStr !== false) {
+            $this->serv->send($fd, $resultStr );
+            return ;
+            $result = json_decode($resultStr, true);
+            if (isset($result['status']) && $result['status'] == 'success') {
+                $this->serv->send($fd, $resultStr );
+            } else {
+                $this->serv->send($fd, $resultStr);
+            }
+            return;
+        } else {
+            $this->serv->send($fd, json_encode(['status' => 'false', 'data' => "Error. Task timeout"]));
+        }
+    }
+
+    public function onTask($serv, $task_id, $from_id, $sql)
+    {
+        static $link = null;
+        HELL:
+        if ($link == null) {
+            try {
+                $conStr = $this->db_type . ":host=$this->db_host;dbname=$this->db_name;port=$this->db_port";
+                $link = new \PDO($conStr, $this->db_user, $this->db_pwd);
+                $link->exec("set names 'utf8'");
+                echo "新建连接成功:Taskid=$task_id  Fromid=$from_id \n";
+            } catch (\PDOException $e) {
+                $link = null;
+                $this->serv->finish(json_encode(['status' => 'false', 'data' => $e->getMessage()], 256));
+                echo "新建连接失败:Taskid=$task_id  Fromid=$from_id " . $e->getMessage() . "\n";
+                return;
+            }
+        }
+
+        $sql = trim($sql);
+        if (preg_match("/^select/i", $sql)){
+            $result = $link->query($sql);
+        }else{
+            $result = $link->exec($sql);
+        }
+
+        $data = ['status'=>false] ;
+        if (!$result) { //如果查询失败了
+            if (in_array($link->errorCode(), [2013, 2006])) {//错误码为2013,或者2006,则重连数据库,重新执行sql
+                $link = null;
+                goto HELL;
+            } else {
+                $data['status'] = 'false';
+                $data['data'] = $link->errorInfo();
+                $this->serv->finish(json_encode($data, 256));
+                return;
+            }
+        }
+        if (preg_match("/^select/i", $sql)) { //如果是select操作,就返回关联数组
+            $dataRet = $result->fetchAll(\PDO::FETCH_CLASS);
+            $data['data'] = $dataRet ;
+        } else {//否则直接返回结果
+            $data['data'] = $result;
+        }
+        $data['status'] = "success";
+        $this->serv->finish(json_encode($data, 256));
+    }
+
+    public function onFinish($serv, $task_id, $data)
+    {
+        echo "任务完成";//taskwait  不触发这个函数。。
+        return $data;
+    }
+}
+
+/**服务启动
+ * $serv=new DB_pool();
+ * $serv->run();
+ */
+
+/**客户端
+ * $client    = new \swoole_client(SWOOLE_SOCK_TCP);
+ * $num=rand(111111,999999);
+ * $rts=$client->connect('127.0.0.1', 9508, 10) or die("连接失败");//链接mysql客户端
+ * $sql =("select  * from zdk_test");
+ * $client->send($sql);
+ * $resdata = $client->recv();
+ *
+ * $resda=json_decode($resdata,true);
+ * $client->close();
+ * return json_encode($resda);
+ */

+ 73 - 0
websockServ/app/lib/DataPack.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace app\lib;
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/20
+ * Time: 13:33
+ */
+
+//服务端下发数据包标准格式;
+
+class DataPack
+{
+    private static $Datas = [];
+    private static $Instance = null ;
+
+    public function getInstance($cache = true)
+    {
+        if ($cache && self::$Instance) {
+            return self::$Instance;
+        }
+        self::$Instance = new self();
+        return self::$Instance;
+    }
+
+    public static function Create($array)
+    {
+        self::$Datas = $array;
+    }
+
+    public static function toJson($array = [])
+    {
+        if (!empty($array)) {
+            self::Create($array);
+        }
+        $ret = [
+            'id' => self::UUID(),
+            'fr' => isset(self::$Datas['from']) ? self::$Datas['from'] : 0,
+            'to' => isset(self::$Datas['to']) ? self::$Datas['to'] : 0,
+            'ct' => time(),
+            'mt' => isset(self::$Datas['mtype']) ? self::$Datas['mtype'] : '',
+            'st' => isset(self::$Datas['stype']) ? self::$Datas['stype'] : '',
+            'cf' => isset(self::$Datas['confirm']) ? self::$Datas['confirm'] : 0,
+            'data' => isset(self::$Datas['data']) ? self::$Datas['data'] : 0,
+        ];
+        return json_encode($ret, JSON_UNESCAPED_UNICODE);
+    }
+
+    public static function setKey($key, $val)
+    {
+        self::$Datas[$key] = $val;
+    }
+
+    public static function getKey($key, $default = '')
+    {
+        if (isset(self::$Datas[$key])) {
+            return self::$Datas[$key];
+        }
+        return $default;
+    }
+
+    public static function reset()
+    {
+        self::$Datas = [];
+    }
+
+    public static function UUID()
+    {
+        return md5(microtime() . uniqid('pack_') . rand(1, 999999));
+    }
+
+}

+ 53 - 0
websockServ/app/lib/GlobConfigs.php

@@ -0,0 +1,53 @@
+<?php
+namespace  app\lib;
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/20
+ * Time: 9:17
+ */
+
+
+class GlobConfigs {
+    public static $file = '';
+    public static $configs = [] ;
+    public static $isload = false ;
+    private static $instance = null ;
+
+    public static function  getInstance(){
+        if (self::$instance){
+            return self::$instance;
+        }
+        self::$instance = new  GlobConfigs();
+        return   self::$instance ;
+    }
+
+    public static function  getKey($key,$default=''){
+         if (!self::$isload) { self::load();  }
+         if (isset(self::$configs[$key])){
+             return self::$configs[$key];
+         }
+         return $default ;
+    }
+    public static  function  setKey($key,$val){
+        if (!self::$isload) { self::load();  }
+        self::$configs[$key] = $val;
+    }
+
+    public static function getAll(){
+        if (!self::$isload) { self::load();  }
+        return self::$configs ;
+    }
+
+    public static function  load($file=''){
+        if ( self::$isload  ){            return true ;       }
+
+        if (empty($file)){
+            $file =  CONFIG_PATH.DS.'configs.php';
+        }
+        self::$configs = require_once ($file);
+        self::$isload = true ;
+    }
+
+
+}

+ 36 - 0
websockServ/app/lib/Mconsts.php

@@ -0,0 +1,36 @@
+<?php
+//
+defined('DS') or define("DS", DIRECTORY_SEPARATOR);
+defined('BASE_PATH') or define("BASE_PATH", realpath(dirname(dirname(dirname(__FILE__)))));
+defined('APP_PATH') or define("APP_PATH", BASE_PATH . DS . 'app');
+defined('CONFIG_PATH') or define("CONFIG_PATH", BASE_PATH . DS . 'configs');
+defined('LOG_PATH') or define("LOG_PATH", BASE_PATH . DS . 'logs');
+defined('LOGIC_PATH') or define("LOGIC_PATH", APP_PATH . DS . 'logic');
+defined('CMDPRO_PATH') or define("CMDPRO_PATH", LOGIC_PATH . DS . 'cmdpro');
+
+defined('ENV') or define("ENV", 'dev');
+
+//业务相关 redis Keys
+defined('DOWN_MSG') or define("DOWN_MSG", 'DOWN_MSG');                                     //需要下发的消息队列名
+defined('DOWN_MSG_SN') or define("DOWN_MSG_SN", 'DOWN_MSG_SN');                            //需要下发的消息队列名 计数器(1-1000切换)
+defined('DOWN_MSG_SN') or define("DOWN_MSG_WRITEFLAG", 'DOWN_MSG_WRITEFLAG');              //需要下发的写锁
+defined('DOWN_MSG_TMP') or define("DOWN_MSG_TMP", 'DOWN_MSG_TMP_');                        //需要下发的消息队列名 前缀(临时)
+
+defined('RUN_LOGS_OVTIME_KEY') or define("RUN_LOGS_OVTIME_KEY", 'RUN_LOGS_OVTIME_KEY');                //自动过期日志        has
+defined('RUN_LOGS_OVTIME') or define("RUN_LOGS_OVTIME", 180);                                          //自动过期日志超时时间
+defined('RUN_LOGS_QUEUE_KEY') or define("RUN_LOGS_QUEUE_KEY", 'RUN_LOGS_QUEUE_KEY');                   //重要日志,不会过期  队列
+
+defined('MAPS_UID_FID') or define("MAPS_UID_FID", 'MAPS_UID_FID');                         //登陆用户UID-FID MAP  HAS表
+defined('MAPS_FID_UID') or define("MAPS_FID_UID", 'MAPS_FID_UID');                         //登陆用户FID-UID MAP  HAS表
+defined('MAPS_USER_TOKEN') or define("MAPS_USER_TOKEN", 'MAPS_USER_TOKEN');                //登陆用户TOKEN-UID MAP  HAS表
+defined('ON_OPEN') or define("ON_OPEN", 'ON_OPEN');                                         //接入时 HAS表
+
+defined('TIME_RSYNC_KEY') or define("TIME_RSYNC_KEY", 'TIME_RSYNC_KEY');                     //时间同步的写获取者
+
+defined('MSG_REDIS_SUBSCRIBE') or define("MSG_REDIS_SUBSCRIBE", 'MSG_REDIS_SUBSCRIBE');      //redis 消息订阅key
+
+////
+
+
+
+

+ 130 - 0
websockServ/app/lib/Wlog.php

@@ -0,0 +1,130 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/22
+ * Time: 11:07
+ */
+
+namespace app\lib;
+
+use app\lib\GlobConfigs as GlobConfigs;
+use \Swoole\Async ;
+
+class Wlog
+{
+    private static $inCrVel = 0;
+    private static $Instance = null;
+    private $redis = null;
+    private $redis_ctime = 0;
+
+    public static function getInstance($cache = true)
+    {
+        if ($cache && self::$Instance) {
+            return self::$Instance;
+        }
+        self::$Instance = new self();
+        return self::$Instance;
+    }
+
+    public function GetRedis()
+    {
+        $now = time();
+        if ($now - $this->redis_ctime > 60 * 15) {
+            if ($this->redis) {
+                $this->redis->close();
+                $this->redis = false;
+                $this->redis_ctime = 0 ;
+            }
+        }
+
+        if ($this->redis) {
+            return $this->redis;
+        }
+
+        echo "新建一个Wlog->Coroutine_redis ".date("Y-m-d H:i:s")."\n";
+        $config = GlobConfigs::getKey('redis');
+        $redis = new \Swoole\Coroutine\Redis();
+        $ret = $redis->connect($config['host'], $config['port']);
+        if ($ret) {
+            $this->redis = $redis;
+            $this->redis_ctime = time();
+        } else {
+            $this->redis = false;
+        }
+        return $redis;
+    }
+
+    public function WriteLog($msg, $type = 3, $worker_id = 0)
+    {
+        $mself= $this;
+        go(function()use ($msg,$type,$worker_id,$mself){
+            $msg = json_encode([date("Y-m-d H:i:s"), $msg], 256);
+
+            if (in_array($type, [1, 2])) {
+                $redis = $mself->GetRedis();
+                if ($redis) {
+                    if ($type == 1) {
+                        $minit = date("i");
+                        $ktype = RUN_LOGS_OVTIME_KEY . '_' . $minit;
+                        $key = 'Log' . ":" . $worker_id . '_' . $this->IncrKey();
+                        $otime = RUN_LOGS_OVTIME;
+                        $redis->hset($ktype, $key, $msg);
+                        $redis->expire($ktype, $otime);
+                    } else {
+                        $ktype = RUN_LOGS_OVTIME_KEY;
+                        $redis->lpush($ktype, $msg);
+                    }
+                    return true;
+                }
+            }
+
+            //redis 失败时,写到日志文件中
+            $fileName = date("Y-m-d") . '.log';
+            $fullName = LOG_PATH . DS . $fileName;
+            //\swoole_async_writefile($fullName, $msg . "\r\n", null, FILE_APPEND);
+            //\Swoole\Async::writeFile($fullName, $msg . "\r\n", null, FILE_APPEND);
+            file_put_contents($fullName,$msg."\r\n",FILE_APPEND);
+            return true;
+        });
+        /*
+        $msg = json_encode([date("Y-m-d H:i:s"), $msg], 256);
+
+        if (in_array($type, [1, 2])) {
+            $redis = $this->GetRedis();
+            if ($redis) {
+                if ($type == 1) {
+                    $minit = date("i");
+                    $ktype = RUN_LOGS_OVTIME_KEY . '_' . $minit;
+                    $key = $ktype . ":" . $worker_id . '_' . $this->IncrKey();
+                    $otime = RUN_LOGS_OVTIME;
+                    $redis->hset($ktype, $key, $msg);
+                    $redis->expire($ktype, $otime);
+                } else {
+                    $ktype = RUN_LOGS_OVTIME_KEY;
+                    $redis->lpush($ktype, $msg);
+                }
+                return true;
+            }
+        }
+
+        //redis 失败时,写到日志文件中
+        $fileName = date("Y-m-d") . '.log';
+        $fullName = LOG_PATH . DS . $fileName;
+        swoole_async_writefile($fullName, $msg . "\r\n", null, FILE_APPEND);
+
+        return true;
+        */
+    }
+
+    public function IncrKey()
+    {
+        self::$inCrVel++;
+        if (self::$inCrVel >= 99999999) {
+            self::$inCrVel = 1;
+        }
+        return self::$inCrVel;
+    }
+
+
+}

+ 23 - 0
websockServ/app/lib/boot.php

@@ -0,0 +1,23 @@
+<?php
+namespace  app\lib;
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/20
+ * Time: 9:19
+ */
+
+class boot
+{
+    public static  function init(){
+        self::loadConsts();
+
+    }
+
+    public static function  loadConsts(){
+        require_once (realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'Mconsts.php');
+    }
+
+
+}
+

+ 136 - 0
websockServ/app/lib/wclient/PacketHandler.php

@@ -0,0 +1,136 @@
+<?php
+namespace  app\lib\wclient;
+/**
+ * Created by PhpStorm.
+ * User: marsnowxiao
+ * Date: 2017/6/15
+ * Time: 下午5:12
+ */
+use app\lib\wclient\WebSocketFrame as WebSocketFrame;
+
+class PacketHandler {
+    const GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
+    const TOKEN_LENGHT = 16;
+    const maxPacketSize = 2000000;
+    private $key = "";
+
+    private static function generateToken($length)
+    {
+        $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"§$%&/()=[]{}';
+
+        $useChars = array();
+        // select some random chars:
+        for ($i = 0; $i < $length; $i++) {
+            $useChars[] = $characters[mt_rand(0, strlen($characters) - 1)];
+        }
+        // Add numbers
+        array_push($useChars, rand(0, 9), rand(0, 9), rand(0, 9));
+        shuffle($useChars);
+        $randomString = trim(implode('', $useChars));
+        $randomString = substr($randomString, 0, self::TOKEN_LENGHT);
+
+        return base64_encode($randomString);
+    }
+
+    public function buildHandShakeRequest($host, $port)
+    {
+        $this->key = static::generateToken(self::TOKEN_LENGHT);
+
+        return "GET / HTTP/1.1" . "\r\n" .
+            "Origin: null" . "\r\n" .
+            "Host: {$host}:{$port}" . "\r\n" .
+            "Sec-WebSocket-Key: {$this->key}" . "\r\n" .
+            "User-Agent: SwooleWebsocketClient"."/0.1.4" . "\r\n" .
+            "Upgrade: Websocket" . "\r\n" .
+            "Connection: Upgrade" . "\r\n" .
+            "Sec-WebSocket-Protocol: wamp" . "\r\n" .
+            "Sec-WebSocket-Version: 13" . "\r\n" . "\r\n";
+    }
+
+    public function verifyUpgrade($packet)
+    {
+        $headers = explode("\r\n", $packet);
+        unset($headers[0]);
+        $headerInfo = [];
+        foreach ($headers as $header) {
+            $arr = explode(":", $header);
+            if (count($arr) == 2) {
+                list($field, $value) = $arr;
+                $headerInfo[trim($field)] = trim($value);
+            }
+        }
+
+        return (isset($headerInfo['Sec-WebSocket-Accept']) && $headerInfo['Sec-WebSocket-Accept'] == base64_encode(pack('H*', sha1($this->key.self::GUID))));
+    }
+
+    public function processDataFrame(&$packet)
+    {
+        if (strlen($packet) < 2)
+            return null;
+        $header = substr($packet, 0, 2);
+        $index = 0;
+
+        //fin:1 rsv1:1 rsv2:1 rsv3:1 opcode:4
+        $handle = ord($packet[$index]);
+        $finish = ($handle >> 7) & 0x1;
+        $rsv1 = ($handle >> 6) & 0x1;
+        $rsv2 = ($handle >> 5) & 0x1;
+        $rsv3 = ($handle >> 4) & 0x1;
+        $opcode = $handle & 0xf;
+        $index++;
+
+        //mask:1 length:7
+        $handle = ord($packet[$index]);
+        $mask = ($handle >> 7) & 0x1;
+
+        //0-125
+        $length = $handle & 0x7f;
+        $index++;
+        //126 short
+        if ($length == 0x7e)
+        {
+            if (strlen($packet) < $index + 2)
+                return null;
+            //2 byte
+            $handle = unpack('nl', substr($packet, $index, 2));
+            $index += 2;
+            $length = $handle['l'];
+        }
+        //127 int64
+        elseif ($length > 0x7e)
+        {
+            if (strlen($packet) < $index + 8)
+                return null;
+            //8 byte
+            $handle = unpack('Nh/Nl', substr($packet, $index, 8));
+            $index += 8;
+            $length = $handle['l'];
+            if ($length > static::maxPacketSize)
+            {
+                throw new \Exception("frame length is too big.\n");
+            }
+        }
+
+        //mask-key: int32
+        if ($mask)
+        {
+            if (strlen($packet) < $index + 4)
+                return null;
+            $mask = array_map('ord', str_split(substr($packet, $index, 4)));
+            $index += 4;
+        }
+
+        if (strlen($packet) < $index + $length)
+            return null;
+        $data = substr($packet, $index, $length);
+        $index += $length;
+
+        $packet = substr($packet, $index);
+
+        $frame = new WebSocketFrame;
+        $frame->finish = $finish;
+        $frame->opcode = $opcode;
+        $frame->data = $data;
+        return $frame;
+    }
+}

+ 160 - 0
websockServ/app/lib/wclient/WebSocketClient.php

@@ -0,0 +1,160 @@
+<?php
+
+namespace app\lib\wclient;
+
+use app\lib\wclient\PacketHandler as PacketHandler;
+
+
+class WebSocketClient
+{
+    private $client;
+    private $state;
+    private $host;
+    private $port;
+    private $handler;
+    private $buffer;
+    private $openCb;
+    private $messageCb;
+    private $closeCb;
+
+    const HANDSHAKING = 1;
+    const HANDSHAKED = 2;
+
+
+    const WEBSOCKET_OPCODE_CONTINUATION_FRAME = 0x0;
+    const WEBSOCKET_OPCODE_TEXT_FRAME = 0x1;
+    const WEBSOCKET_OPCODE_BINARY_FRAME = 0x2;
+    const WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8;
+    const WEBSOCKET_OPCODE_PING = 0x9;
+    const WEBSOCKET_OPCODE_PONG = 0xa;
+
+    const TOKEN_LENGHT = 16;
+
+    public function __construct()
+    {
+        $this->client = new \swoole\client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
+        $this->client->on("connect", [$this, "onConnect"]);
+        $this->client->on("receive", [$this, "onReceive"]);
+        $this->client->on("close", [$this, "onClose"]);
+        $this->client->on("error", [$this, "onError"]);
+        $this->handler = new PacketHandler();
+        $this->buffer = "";
+    }
+
+    public function connect($host, $port)
+    {
+        $this->host = $host;
+        $this->port = $port;
+        $this->client->connect($host, $port);
+    }
+
+    public function sendHandShake()
+    {
+        $this->state = static::HANDSHAKING;
+        $request = $this->handler->buildHandShakeRequest($this->host, $this->port);
+        $this->client->send($request);
+    }
+
+    public function onConnect($cli)
+    {
+        $this->sendHandShake();
+    }
+
+    public function onReceive($cli, $data)
+    {
+        if ($this->state == static::HANDSHAKING) {
+            $this->buffer .= $data;
+            $pos = strpos($this->buffer, "\r\n\r\n", true);
+
+            if ($pos != false) {
+                $header = substr($this->buffer, 0, $pos + 4);
+                $this->buffer = substr($this->buffer, $pos + 4);
+
+                if (true == $this->handler->verifyUpgrade($header)) {
+                    $this->state = static::HANDSHAKED;
+                    if (isset($this->openCb))
+                        call_user_func($this->openCb, $this);
+                } else {
+                    echo "handshake failed\n";
+                }
+            }
+        } else if ($this->state == static::HANDSHAKED) {
+            $this->buffer .= $data;
+        }
+        if ($this->state == static::HANDSHAKED) {
+            try {
+                $frame = $this->handler->processDataFrame($this->buffer);
+            } catch (\Exception $e) {
+                $cli->close();
+                return;
+            }
+            if ($frame != null) {
+                if (isset($this->messageCb))
+                    call_user_func($this->messageCb, $this, $frame);
+            }
+        }
+
+    }
+
+    public function onClose($cli)
+    {
+        if (isset($this->closeCb))
+            call_user_func($this->closeCb, $this);
+    }
+
+    public function onError($cli)
+    {
+        echo "error occurred\n";
+    }
+
+    public function on($event, $callback)
+    {
+        if (strcasecmp($event, "open") === 0) {
+            $this->openCb = $callback;
+        } else if (strcasecmp($event, "message") === 0) {
+            $this->messageCb = $callback;
+        } else if (strcasecmp($event, "close") === 0) {
+            $this->closeCb = $callback;
+        } else {
+            echo "$event is not supported\n";
+        }
+    }
+
+    public function send($data, $type = 'text')
+    {
+        switch ($type) {
+            case 'text':
+                $_type = self::WEBSOCKET_OPCODE_TEXT_FRAME;
+                break;
+            case 'binary':
+            case 'bin':
+                $_type = self::WEBSOCKET_OPCODE_BINARY_FRAME;
+                break;
+            case 'ping':
+                $_type = self::WEBSOCKET_OPCODE_PING;
+                break;
+            case 'close':
+                $_type = self::WEBSOCKET_OPCODE_CONNECTION_CLOSE;
+                break;
+
+            case 'ping':
+                $_type = self::WEBSOCKET_OPCODE_PING;
+                break;
+
+            case 'pong':
+                $_type = self::WEBSOCKET_OPCODE_PONG;
+                break;
+
+            default:
+                echo "$type is not supported\n";
+                return;
+        }
+        $data = \swoole_websocket_server::pack($data, $_type);
+        $this->client->send($data);
+    }
+
+    public function getTcpClient()
+    {
+        return $this->client;
+    }
+}

+ 17 - 0
websockServ/app/lib/wclient/WebSocketFrame.php

@@ -0,0 +1,17 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/24
+ * Time: 11:26
+ */
+
+namespace app\lib\wclient;
+
+
+class WebSocketFrame
+{
+    public $finish;
+    public $opcode;
+    public $data;
+}

+ 95 - 0
websockServ/app/logic/CmdProxy.php

@@ -0,0 +1,95 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/21
+ * Time: 16:03
+ */
+
+namespace app\logic;
+
+///composer update或者composer install或者composer dump-autoload.
+///
+
+use app\lib\DataPack;
+
+use  app\logic\cmdpro\CmdTest as CmdTest;
+use  app\logic\cmdpro\CmdQuery as CmdQuery;
+use  app\logic\cmdpro\CmdLogin as CmdLogin;
+
+class CmdProxy
+{
+    private static $Instance = null;
+
+    public static function getInstance($cache = true)
+    {
+        if ($cache && self::$Instance) {
+            return self::$Instance;
+        }
+        self::$Instance = new self();
+        return self::$Instance;
+    }
+
+    public function ParaCMD($serv, \Swoole\Server\Task $task)
+    {
+
+        $fd = $task->data->fd;
+        $dataPack = json_decode(trim($task->data->data),true);
+        $cmd = isset($dataPack['cmd']) ? $dataPack['cmd'] : '';
+        $cmdcls = $this->getParaCmd($cmd);
+
+        if (!$cmdcls) {
+            $serv->push($fd, DataPack::toJson(['mt' => 'errorMsg', 'data' => 'error cmd!']));
+            $serv->disconnect($fd);
+            return;
+        }
+        $cmdObj = $this->getClsObj($cmdcls);
+        try{
+            $cmdObj->paraCmd($serv, $task);
+        }catch (\Exception $e){
+
+        }
+        unset($cmdObj) ;
+    }
+
+    public function  getClsObj($cmd)
+    {
+        $obj = null ;
+        switch ($cmd){
+            case "CmdTest":
+                $obj = new CmdTest();
+                break ;
+            case "CmdQuery":
+                $obj = new CmdQuery();
+                break;
+            case "CmdLogin":
+                $obj = new CmdLogin();
+                break;
+            default:
+                break;
+        }
+        return $obj ;
+    }
+
+
+    public function allCmdMaps()
+    {
+        $arrs = [
+            'login' => 'CmdLogin',
+            'query' => 'CmdQuery',
+            'test' => 'CmdTest',
+        ];
+        return $arrs;
+    }
+
+    public function getParaCmd($cmd)
+    {
+        $arr = $this->allCmdMaps();
+        if (isset($arr[$cmd])) {
+           return $arr[$cmd];
+        }
+       return false;
+    }
+
+
+}

+ 16 - 0
websockServ/app/logic/MsgRedisSend.php

@@ -0,0 +1,16 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/22
+ * Time: 11:51
+ */
+
+namespace app\logic;
+
+
+//把redis里要下发的数据下发给客户,只发在线用户,不在线不处理,也不写日志
+class MsgRedisSend
+{
+
+}

+ 49 - 0
websockServ/app/logic/MyPgsql.php

@@ -0,0 +1,49 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/24
+ * Time: 17:40
+ */
+
+namespace app\logic;
+
+use  app\pplus\Instance;
+use Illuminate\Database\Capsule\Manager as Capsule;
+use app\lib\GlobConfigs;
+
+class MyPgsql
+{
+    use Instance;
+    private $pgsql = null;
+    private $ctime = 0;
+
+    public function getDb()
+    {
+        if ((time() - $this->ctime) <= 60 * 5) {
+            if ($this->pgsql) {
+                return $this->pgsql;
+            }
+        }
+
+        if ($this->pgsql) {
+            return $this->pgsql;
+        }
+        return $this->Init();
+    }
+
+    public function Init()
+    {
+        echo "MyPgsql--Init" . "\n";
+        $conf = GlobConfigs::getKey('pgsql');
+        $pgsql = new Capsule();
+        $pgsql->addConnection($conf);
+        $pgsql->setAsGlobal();
+        $pgsql->bootEloquent();
+        $this->pgsql = $pgsql;
+        $this->ctime = time();
+        return $pgsql;
+    }
+
+
+}

+ 334 - 0
websockServ/app/logic/MyServerV2.php

@@ -0,0 +1,334 @@
+<?php
+
+namespace app\logic;
+
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/20
+ * Time: 11:22
+ */
+
+use app\lib\GlobConfigs;
+use Illuminate\Database\Capsule\Manager as Capsule;
+use Illuminate\Database\Capsule\Manager as DB;
+use  app\lib\Wlog;
+use app\logic\MyPgsql as MyPgsql;
+
+class MyServerV2
+{
+    public $serv;
+
+    //线程连接实例
+    private $workRedis = null;
+    private $workMysql = null;
+    private $workPgsql = null ;
+
+    public function __construct()
+    {
+        $this->serv = new \swoole_websocket_server("0.0.0.0", 9090);
+        $this->serv->set(GlobConfigs::getKey('swoolev2'));
+
+        $this->serv->on('Start', array($this, 'onStart'));
+        $this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
+        $this->serv->on('message', array($this, 'onMessage'));
+        $this->serv->on('close', array($this, 'onClose'));
+        $this->serv->on('open', array($this, 'onOpen'));
+        $this->serv->on('task', array($this, 'onTask'));
+        $this->serv->on('Finish', array($this, 'onFinish'));
+
+        return $this->serv->start();
+    }
+
+    public function onStart($serv)
+    {
+        echo "Main on Start " . "\n";
+        $this->startInit();
+    }
+
+    public function onWorkerStart($serv, $worker_id)
+    {
+        $type = $serv->taskworker ? 'Task' : 'Work';
+        echo "onWorkerStart.. " . $type . '-' . $worker_id . "\n";
+
+        if (!$serv->taskworker) {
+            $this->PingRedis($serv, $worker_id);
+            $serv->tick(8000, function () use ($serv, $worker_id) {
+                $this->PingRedis($serv, $worker_id);
+            });
+
+            /*
+            $this->PingMysql($serv, $worker_id);
+            $serv->tick(5000, function () use ($serv, $worker_id) {
+                $this->PingMysql($serv, $worker_id);
+            });
+            */
+            $this->PingPgsql($serv, $worker_id);
+            $serv->tick(5000, function () use ($serv, $worker_id) {
+                $this->PingPgsql($serv, $worker_id);
+            });
+
+
+            $nowRedis = $this->workRedis;
+            TimeRsync::getInstance()->DoTimePublic($serv, $worker_id, $this->workRedis);
+            $serv->tick(60000, function () use ($serv, $worker_id, $nowRedis) {
+                TimeRsync::getInstance()->DoTimePublic($serv, $worker_id, $this->workRedis);
+            });
+
+            MyPgsql::getInstance();
+
+            //消息订阅
+            if ($worker_id == 0){
+
+                if ( !$worker_id ){      return ;   }
+
+                go(function() use ($serv,$worker_id){
+                    $config = GlobConfigs::getKey('redis');
+                    $redis = new \Swoole\Coroutine\Redis();
+                    $redis->setOptions(['compatibility_mode' => true]);
+                    $redis->connect($config['host'], $config['port']);
+                    $r = $redis->SUBSCRIBE([MSG_REDIS_SUBSCRIBE,'TEST']);
+                    if ($r){
+                        while ($msg = $redis->recv()) {
+                            print_r($msg);
+                            // msg是一个数组, 包含以下信息
+                            // $type # 返回值的类型:显示订阅成功
+                            // $name # 订阅的频道名字 或 来源频道名字
+                            // $info  # 目前已订阅的频道数量 或 信息内容
+                            list($type, $name, $info) = $msg;
+                            if ($type == 'subscribe') // 或psubscribe
+                            {
+                                // 频道订阅成功消息,订阅几个频道就有几条
+                            }
+                            else if ($type == 'unsubscribe' && $info == 0) // 或punsubscribe
+                            {
+                                break; // 收到取消订阅消息,并且剩余订阅的频道数为0,不再接收,结束循环
+                            }
+                            else if ($type == 'message') // 若为psubscribe,此处为pmessage
+                            {
+                                /*
+                                if ($need_unsubscribe) // 某个情况下需要退订
+                                {
+                                    $redis->unsubscribe(); // 继续recv等待退订完成
+                                }
+                                */
+                            }
+                        }
+
+
+                    }
+                });
+            }
+        }
+    }
+
+    function onWorkerStop(swoole_server $server, int $worker_id)
+    {
+        echo "onWorkerStop.. " . $worker_id . ' ' . "\n";
+        if ($this->workRedis) {
+            $this->workRedis->close();
+        }
+        if ($this->workMysql) {
+            $this->workMysql->close();
+        }
+    }
+
+
+    public function onMessage($serv, $frame)
+    {
+        Wlog::getInstance()->WriteLog($frame);
+        $serv->task($frame);
+    }
+
+    public function onOpen($serv, $request)
+    {
+
+        $token = isset($request->get['token']) ? $request->get['token'] : '';
+        $uid = isset($request->get['uid']) ? $request->get['uid'] : '';
+        $fd = $request->fd;
+        $redis = $this->workRedis;
+        if (!$redis) {
+            return;
+        }
+
+        $redis->hset(ON_OPEN, $fd, json_encode(['fd' => $fd, 'token' => $token, 'uid' => $uid], JSON_UNESCAPED_UNICODE));
+        $str = '你好 !  fd=' . $fd . ' ,ip=' . $request->server['remote_addr'] . ' ,port=' . $request->server['remote_port'] . ' ,time=' . date('Y-m-d H:i:s');
+
+        echo $str ."\n" ;
+
+        $serv->push($fd, $str);
+
+        /*
+        $serv->after(120000,function() use($serv,$fd,$redis){
+            if ($serv->exist($fd)){
+                $serv->disconnect($fd,1000,json_encode('未验证用户关闭',JSON_UNESCAPED_UNICODE));
+                $redis->hdel('onOpen',$fd);
+            }
+        });
+        */
+    }
+
+    public function onTask($serv, \Swoole\Server\Task $task)
+    {
+        try {
+            Wlog::getInstance()->WriteLog($task, 1, $serv->worker_id);
+            CmdProxy::getInstance()->ParaCMD($serv, $task);
+        } catch (\Exception $e) {
+            echo "发生异常." . $e->getCode() . ' ' . $e->getMessage() . "\n";
+        }
+    }
+
+    public function onFinish($serv, $task_id, $data)
+    {
+    }
+
+    public function onClose($serv, $fd, $from_id)
+    {
+        $this->workRedis->hdel(ON_OPEN, $fd);
+        echo "Client {$fd} close connection!\n";
+    }
+
+
+    //redis Ping检测
+    private function PingRedis($serv, $worker_id)
+    {
+        if (empty($this->workRedis)) {
+            $this->ConectToRedis($serv, $worker_id);
+            return true;
+        }
+        $redis = $this->workRedis;
+        if (!$redis) {
+            $ping_ret = false;
+        } else {
+            $ping_ret_s = $redis->ping();
+            $ping_ret = "+pong" === strtolower($ping_ret_s) ? true : false;
+        }
+
+        if (!$ping_ret) {
+            return $this->ConectToRedis($serv, $worker_id);
+        }
+        return true;
+    }
+
+    //工作线程 同步阻塞 redis客户端
+    private function ConectToRedis($serv, $worker_id)
+    {
+        $conf = GlobConfigs::getKey('redis');
+        $redis = new \Redis();
+        try {
+            $ret = $redis->connect($conf['host'], $conf['port'], $conf['overtime']);
+        } catch (\Exception $e) {
+            $ret = false;
+        }
+
+        if ($ret) {
+            Wlog::getInstance()->WriteLog("success:成功建立redis连接 " . date("Y-m-d H:i:s") . ' ' . $worker_id);
+            $this->workRedis = $redis;
+            return $redis;
+        } else {
+            Wlog::getInstance()->WriteLog("error:建立redis连接 " . date("Y-m-d H:i:s") . ' ' . $worker_id);
+            $this->workRedis = null;
+        }
+        return false;
+    }
+
+    //Mysql Ping检测
+    private function PingMysql($serv, $worker_id)
+    {
+        if (empty($this->workMysql)) {
+            $this->ConectToMysql($serv, $worker_id);
+            return true;
+        }
+        $mysql = $this->workMysql;
+        if (!$mysql) {
+            $ping_ret = false;
+        } else {
+            $ping_ret_s = DB::select("select version() as v");
+            $ping_ret = !empty($ping_ret_s) ? true : false;
+        }
+
+        if (!$ping_ret) {
+            return $this->ConectToMysql($serv, $worker_id);
+        }
+        return true;
+    }
+
+    //工作线程 同步阻塞 Mysql 客户端
+    private function ConectToMysql($serv, $worker_id)
+    {
+        $conf = GlobConfigs::getKey('mysql');
+        $mysql = new Capsule();
+        $mysql->addConnection($conf);
+        $mysql->setAsGlobal();
+        $mysql->bootEloquent();
+
+        Wlog::getInstance()->WriteLog("success:成功建立Mysql连接 " . date("Y-m-d H:i:s") . ' ' . $worker_id);
+        $this->workMysql = $mysql;
+        return $mysql;
+
+    }
+
+
+
+    //Mysql Ping检测
+    private function PingPgsql($serv, $worker_id)
+    {
+        if (empty($this->workPgsql)) {
+            $this->ConectToPgsql($serv, $worker_id);
+            return true;
+        }
+        $pgsql = $this->workPgsql;
+        if (!$pgsql) {
+            $ping_ret = false;
+        } else {
+            $ping_ret_s = DB::select("select version() as v");
+            $ping_ret = !empty($ping_ret_s) ? true : false;
+        }
+
+        if (!$ping_ret) {
+            return $this->ConectToPgsql($serv, $worker_id);
+        }
+        return true;
+    }
+
+    //工作线程 同步阻塞 Mysql 客户端
+    private function ConectToPgsql($serv, $worker_id)
+    {
+        $conf = GlobConfigs::getKey('pgsql');
+        $pgsql = new Capsule();
+        $pgsql->addConnection($conf);
+        $pgsql->setAsGlobal();
+        $pgsql->bootEloquent();
+
+        Wlog::getInstance()->WriteLog("success:成功建立Pgsql连接 " . date("Y-m-d H:i:s") . ' ' . $worker_id);
+        $this->workPgsql = $pgsql;
+        return $pgsql;
+
+    }
+
+
+    //服务启动作做一些初始化操作
+    private function startInit()
+    {
+        $conf = GlobConfigs::getKey('redis');
+        $redis = new \Redis();
+        $ret = $redis->connect($conf['host'], $conf['port'], $conf['overtime']);
+        if ($ret) {
+            $redis->pipeline();
+            $redis->del(MAPS_UID_FID);
+            $redis->del(MAPS_FID_UID);
+            $redis->del(MAPS_USER_TOKEN);
+            $redis->del(ON_OPEN);
+            $redis->exec();
+            $redis->close();
+        }
+    }
+
+    //redis推送消息
+    private function MsgRedisList($serv, $worker_id)
+    {
+
+    }
+
+
+}

+ 29 - 0
websockServ/app/logic/Mypack.php

@@ -0,0 +1,29 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/22
+ * Time: 17:05
+ */
+
+namespace app\logic;
+
+use app\pplus\Instance;
+class Mypack
+{
+    use Instance ;
+
+    public $fd;
+    public $data;
+    public $opcode=1;
+    public $finish=1;
+
+    public function  makeData($data,$fd=0,$opcode=1,$finish=1){
+        $this->fd= $fd;
+        $this->data = $data;
+        $this->opcode = $opcode;
+        $this->finish = $finish ;
+        return  $this;
+    }
+
+}

+ 40 - 0
websockServ/app/logic/TimeRsync.php

@@ -0,0 +1,40 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/22
+ * Time: 15:20
+ */
+
+namespace app\logic;
+
+
+use app\pplus\Instance;
+use app\lib\GlobConfigs;
+use app\logic\Mypack;
+
+class TimeRsync
+{
+    use  Instance;
+
+    public function DoTimePublic($serv, $worker_id, $redis)
+    {
+        $key = TIME_RSYNC_KEY;
+        $val = intval($redis->get($key));
+        //有其它进程正在操作,不用重复
+        if (!empty($val)) {   return;     }
+
+        $selfval = $worker_id + 1;
+        $redis->SETEX($key, 55 , $selfval);
+        $newvel = $redis->get($key);
+        if ($selfval != intval($newvel)){   return;   }
+
+        $time = time();
+        $data = json_encode(['cmd'=>'test','act'=>'timeRsync','data'=>['t'=>$time,'s'=>date('Y-m-d H:i:s',$time)]]);
+        $serv->task(Mypack::getInstance()->makeData($data));
+        return ;
+
+    }
+
+
+}

+ 75 - 0
websockServ/app/logic/cmdpro/CmdInf.php

@@ -0,0 +1,75 @@
+<?php
+namespace  app\logic\cmdpro;
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/21
+ * Time: 16:48
+ */
+
+/*
+ Swoole\Server\Task Object
+(
+    [data] => Swoole\WebSocket\Frame Object
+        (
+            [fd] => 1
+            [data] => {'cmd':'xxx','act'=>'xxx','data':'xxxx','time':xxxx,'token':xxx}
+            [opcode] => 1
+            [finish] => 1
+        )
+
+    [id] => 0
+    [worker_id] => 2
+    [flags] => 6
+)
+
+data:
+{'cmd':'xxx','act'=>'xxx','data':'xxxx','time':xxxx,'token':xxx}
+ **/
+
+use app\lib\DataPack;
+use app\lib\Wlog;
+
+class   CmdInf
+{
+    public function paraCmd($serv, \Swoole\Server\Task $task){
+        $fd = $task->data->fd;
+        $dataPack = json_decode($task->data->data, true);
+        $method = isset($dataPack['act']) ? $dataPack['act'] : '';
+
+        if (empty($method) || !(method_exists($this, $method))) {
+            $this->sendTo($serv, $fd, DataPack::toJson(['mtype' => 'error', 'data' => '无效的参数']), true);
+            return;
+        }
+
+        try{
+            return $this->$method($serv,$task);
+        }catch (\Exception $e){
+            echo "发生异常或错误:".$e->getFile().' '.$e->getLine().' '.$e->getCode().' '.$e->getMessage()."\r\n";
+            return ;
+        }
+
+    }
+
+    //单发
+    public function sendTo($serv,$fd,$dataString,$close=false)
+    {
+        $serv->push($fd,$dataString);
+        if ($close){
+            $serv->disconnect($fd);
+        }
+        return true;
+    }
+
+    //在线广播
+    public function onlineBroad($serv,$dataString){
+        foreach($serv->connections as $fd)
+        {
+            $serv->push($fd,$dataString);
+        }
+    }
+
+
+
+
+}

+ 21 - 0
websockServ/app/logic/cmdpro/CmdLogin.php

@@ -0,0 +1,21 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/21
+ * Time: 16:57
+ */
+namespace app\logic\cmdpro;
+
+use app\logic\cmdpro\CmdInf as CmdInf;
+
+class CmdLogin  extends CmdInf
+{
+    public function   paraCmd($serv, \Swoole\Server\Task $task){
+
+    }
+
+
+
+
+}

+ 19 - 0
websockServ/app/logic/cmdpro/CmdQuery.php

@@ -0,0 +1,19 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/21
+ * Time: 16:57
+ */
+
+namespace app\logic\cmdpro;
+
+use app\logic\cmdpro\CmdInf as CmdInf;
+
+class CmdQuery  extends CmdInf
+{
+    public function paraCmd($serv, \Swoole\Server\Task $task){
+
+
+    }
+}

+ 161 - 0
websockServ/app/logic/cmdpro/CmdTest.php

@@ -0,0 +1,161 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/21
+ * Time: 16:57
+ */
+
+namespace app\logic\cmdpro;
+
+use app\lib\DataPack;
+use app\lib\GlobConfigs;
+use app\logic\cmdpro\CmdInf as CmdInf;
+
+
+use app\logic\MyPgsql as MyPgsql;
+
+use swoole ;
+
+class CmdTest extends CmdInf
+{
+
+    //异步回调实现 mysql
+    public function sqlold($serv, $task)
+    {
+        $db = new \swoole_mysql();
+        $config = GlobConfigs::getKey('mysql');
+        $db->connect($config, function (\swoole_mysql $db, $r) use ($serv, $task) {
+            if ($r === false) {
+                $serv->push($task->data->fd, json_encode(['data' => $task->data, 'err' => [$db->connect_errno, $db->connect_error], 'fd' => $task->data->fd], 256));
+                return false;
+            }
+            $packdata = json_decode($task->data->data, true);
+            $sql = 'select *  from bm_admin where admin_id=' . intval($packdata['data']);
+            $db->query($sql, function (\swoole_mysql $db, $r2) use ($serv, $task) {
+                if ($r2) {
+                    $serv->push($task->data->fd, json_encode(['record' => $r2, 'fd' => $task->data->fd], 256));
+                }
+                $db->close();
+            });
+        });
+    }
+
+    //协程实现mysql
+    public function mysqlgo($serv, $task)
+    {
+        $swoole_mysql = new \Swoole\Coroutine\MySQL();
+        $config = GlobConfigs::getKey('mysql');
+        $swoole_mysql->connect($config);
+
+        $Pack = json_decode($task->data->data, true);
+        $DataArr = $Pack['data'];
+        $table = isset($DataArr['table']) ? $DataArr['table'] : '';
+        $field = isset($DataArr['field']) ? $DataArr['field'] : '';
+        $value = isset($DataArr['value']) ? $DataArr['value'] : '';
+        if (empty($task) || empty($field)) {
+            return;
+        }
+
+        try {
+            $ret = $swoole_mysql->query("select  *  from   $table where  $field='$value' ");
+            if ($ret) {
+                $this->sendTo($serv, $task->data->fd, DataPack::toJson(['data' => $ret]));
+            }
+        } catch (\Exception $e) {
+            echo "Exception:" . $e->getCode() . ' - ' . $e->getMessage();
+        }
+        $swoole_mysql->close();
+    }
+
+
+    //协程redis
+    public function redis($serv, $task)
+    {
+        $config = GlobConfigs::getKey('redis');
+        go(function () use ($serv, $task, $config) {
+            $redis = new \Swoole\Coroutine\Redis();
+            $redis->connect($config['host'], $config['port']);
+            $redis->set('test', date('Y-m-d H:i:s'));
+            $val = $redis->get('test');
+            $this->sendTo($serv, $task->data->fd, DataPack::toJson(['data' => $val]));
+        });
+    }
+
+    public function broad($serv, $task)
+    {
+        $pack = json_decode($task->data->data, true);
+        $msg = $pack['data'];
+        $this->onlineBroad($serv, $msg . ' ' . $this->dotest());
+    }
+
+    public function dotick($serv, $task)
+    {
+        $pack = json_decode($task->data->data, true);
+        $data = $pack['data'];
+        $fid = isset($data['fid']) ? intval($data['fid']) : 0;
+        $msg = isset($data['msg']) ? $data['msg'] : '';
+        if (!$serv->exist($fid)) {
+            $this->sendTo($serv, $task->data->fd, '此用户不在线!' . $fid);
+            return;
+        }
+        if ($fid == $task->data->fd) {
+            $this->sendTo($serv, $task->data->fd, '自己不能踢自己!');
+            return;
+        }
+        $this->sendTo($serv, $fid, $msg);
+        $serv->disconnect($fid);
+        return;
+    }
+
+
+    //时间同步用
+    public function timeRsync($serv,$task){
+        $dataArr = json_decode($task->data->data,true);
+        $msg = DataPack::toJson(['mtype'=>'system','stype'=>'TimeSync','data'=>$dataArr['data']]);
+        $this->onlineBroad($serv,$msg);
+        return ;
+    }
+
+
+    public function pgsql($serv,$task){
+        $dataArr = json_decode($task->data->data,true);
+        $config = GlobConfigs::getKey('pgsql');
+        go(function () use ($serv, $task, $config,$dataArr) {
+            $conf = $config ;
+            $mysql = new Capsule();
+            $mysql->addConnection($conf);
+            $mysql->setAsGlobal();
+            $mysql->bootEloquent();
+
+            $ret = DB::table('account')->where('id',83859)->first();
+            $this->sendTo($serv, $task->data->fd, DataPack::toJson(['mtype'=>'system','data'=>$ret]));
+
+        });
+
+    }
+
+
+    public function pgsqlquery($serv,$task){
+        $dataArr = json_decode($task->data->data,true);
+        $ret = MyPgsql::getInstance()->getDb()->table('members')->where('id',intval($dataArr['data']))->first();
+        $this->sendTo($serv, $task->data->fd, DataPack::toJson(['mtype'=>'system','data'=>$ret]));
+    }
+
+    public function pgsqlexec($serv,$task){
+        $dataArr = json_decode($task->data->data,true);
+        $ret = MyPgsql::getInstance()->getDb()->table('members')->where('id',57)->update(['name'=>'12-'.$dataArr['data']]);
+        $this->sendTo($serv, $task->data->fd, DataPack::toJson(['mtype'=>'system','data'=>$ret]));
+    }
+
+
+    private function dotest()
+    {
+        return " hellow,baby,at " . date("Y-m-d H:i:s");
+    }
+
+
+
+
+
+}

+ 26 - 0
websockServ/app/pplus/Instance.php

@@ -0,0 +1,26 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/22
+ * Time: 15:24
+ */
+
+namespace app\pplus;
+
+
+trait  Instance
+{
+    private static $Instance = null;
+
+    public static function getInstance($cache = true)
+    {
+        if ($cache && self::$Instance) {
+            return self::$Instance;
+        }
+        self::$Instance = new self();
+        return self::$Instance;
+    }
+
+
+}

+ 42 - 0
websockServ/commands/clientws1.php

@@ -0,0 +1,42 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/23
+ * Time: 17:42
+ */
+
+define('START_TIME', microtime(true));
+require __DIR__ . '/../vendor/autoload.php';
+\app\lib\boot::init();
+
+use  app\lib\wclient\WebSocketClient ;
+
+
+$client = new WebSocketClient();
+
+$client->on("open",function ($client) {
+    $fd = $client->getTcpClient()->sock;
+    echo "fd: $fd is open\n";
+    $msg = '{"cmd":"test","act":"broad","data":"你好,朋友们!","time":1558492565,"token":"aasasfa"}';
+    $client->send($msg);
+});
+
+$client->on("message", function ($client, $frame) {
+    $fd = $client->getTcpClient()->sock;
+    echo "fd: $fd received: {$frame->data}\n";
+});
+
+
+$client->on("close", function ($client) {
+    $fd = $client->getTcpClient()->sock;
+    echo "fd: $fd is closed\n";
+});
+
+$client->connect("192.168.2.200", 9090);
+
+
+
+
+
+

+ 45 - 0
websockServ/commands/clientws2.php

@@ -0,0 +1,45 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/23
+ * Time: 17:42
+ */
+
+
+define('START_TIME', microtime(true));
+require __DIR__.'/../vendor/autoload.php';
+\app\lib\boot::init();
+
+
+
+use WebSocket\Client as Client;
+
+$client = new Client("ws://192.168.2.200:9090");
+
+
+$str = '{"cmd":"test","act":"broad","data":"你好,朋友们!","time":1558492565,"token":"aasasfa"}';
+
+$r = $client->send($str);
+$msg = $client->receive();
+echo "Recive: ".$msg."\n\n\n";
+$msg = $client->receive();
+echo "Recive: ".$msg."\n\n\n";
+sleep(1);
+
+$r = $client->send($str);
+$msg = $client->receive();
+echo "Recive: ".$msg."\n\n\n";
+sleep(1);
+
+$r = $client->send($str);
+$msg = $client->receive();
+echo "Recive: ".$msg."\n\n\n";
+
+
+$client->close();
+
+
+
+
+

+ 63 - 0
websockServ/commands/mysql_poole_client_test.php

@@ -0,0 +1,63 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/27
+ * Time: 9:28
+ */
+
+define('START_TIME', microtime(true));
+require __DIR__.'/../vendor/autoload.php';
+\app\lib\boot::init();
+
+
+
+$config  = \app\lib\GlobConfigs::getKey('pgsqlpoole');
+
+$client    = new swoole_client(SWOOLE_SOCK_TCP);
+$client->set(array(
+    'socket_buffer_size'     => 1024*1024*8, //2M缓存区
+));
+$rts= $client->connect('192.168.2.200', $config['poole_port'], 10) or die("连接失败");//链接mysql客户端
+
+
+
+$sql =("select  * from members where id=57");
+$client->send($sql);
+$resdata = $client->recv();
+print_r(json_decode($resdata,true)) ;
+
+
+$sql ="update  members  set  name='12-".rand(100,500)."'  where id=57";
+$client->send($sql);
+$resdata = $client->recv();
+print_r(json_decode($resdata,true)) ;
+
+$sql ="select  * from \"districts\" where id=1";
+$client->send($sql);
+$resdata = $client->recv();
+print_r(json_decode($resdata,true)) ;
+
+
+/*
+$sql ="select  * from \"districts\" where id>=1 and id<=300" ;
+$client->send($sql);
+$resdata = $client->recv();
+print_r(json_decode($resdata,true)) ;
+*/
+
+
+
+
+
+$client->close();
+
+
+
+
+/*
+echo "收到: ".$resdata."\n" ;
+$resda=json_decode($resdata,true);
+$client->close();
+return json_encode($resda);
+*/

+ 15 - 0
websockServ/commands/mysql_poole_server.php

@@ -0,0 +1,15 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/27
+ * Time: 9:16
+ */
+
+define('START_TIME', microtime(true));
+require __DIR__.'/../vendor/autoload.php';
+\app\lib\boot::init();
+
+$config  = \app\lib\GlobConfigs::getKey('pgsqlpoole');
+$ser = new app\lib\DB_pool($config);
+$ser->run();

+ 15 - 0
websockServ/commands/test1.php

@@ -0,0 +1,15 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/20
+ * Time: 9:36
+ */
+
+define('START_TIME', microtime(true));
+require __DIR__.'/../vendor/autoload.php';
+\app\lib\boot::init();
+
+$ser = new app\logic\MyServer();
+//$ser->start();
+

+ 15 - 0
websockServ/commands/test2.php

@@ -0,0 +1,15 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/20
+ * Time: 9:36
+ */
+
+define('START_TIME', microtime(true));
+require __DIR__.'/../vendor/autoload.php';
+\app\lib\boot::init();
+
+$ser = new app\logic\MyServerV2();
+//$ser->start();
+

+ 28 - 0
websockServ/composer.json

@@ -0,0 +1,28 @@
+{
+    "name": "swoole/servers",
+    "description": "swoole user ORM",
+    "keywords": ["swole", "ROM"],
+    "license": "MIT",
+    "type": "project",
+    "require": {
+        "php": ">=7.0.0",
+        "illuminate/database": "*",
+        "textalk/websocket":"1.2.*"
+    },
+    "autoload": {
+        "classmap": [
+            "app/lib",
+            "app/logic",
+            "app/logic/cmdpro"
+        ],
+        "psr-4": {
+            "app\\": "app/"
+        }
+    },
+    "repositories": {
+        "packagist": {
+            "type": "composer",
+            "url": "https://packagist.laravel-china.org"
+        }
+    }
+}

+ 770 - 0
websockServ/composer.lock

@@ -0,0 +1,770 @@
+{
+    "_readme": [
+        "This file locks the dependencies of your project to a known state",
+        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
+        "This file is @generated automatically"
+    ],
+    "content-hash": "a7bc80d4b7f1c019b3006b02e3589bc3",
+    "packages": [
+        {
+            "name": "doctrine/inflector",
+            "version": "v1.3.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/doctrine/inflector.git",
+                "reference": "5527a48b7313d15261292c149e55e26eae771b0a"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/doctrine/inflector/zipball/5527a48b7313d15261292c149e55e26eae771b0a",
+                "reference": "5527a48b7313d15261292c149e55e26eae771b0a",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": "^7.1"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "^6.2"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.3.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Doctrine\\Common\\Inflector\\": "lib/Doctrine/Common/Inflector"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Roman Borschel",
+                    "email": "roman@code-factory.org"
+                },
+                {
+                    "name": "Benjamin Eberlei",
+                    "email": "kontakt@beberlei.de"
+                },
+                {
+                    "name": "Guilherme Blanco",
+                    "email": "guilhermeblanco@gmail.com"
+                },
+                {
+                    "name": "Jonathan Wage",
+                    "email": "jonwage@gmail.com"
+                },
+                {
+                    "name": "Johannes Schmitt",
+                    "email": "schmittjoh@gmail.com"
+                }
+            ],
+            "description": "Common String Manipulations with regard to casing and singular/plural rules.",
+            "homepage": "http://www.doctrine-project.org",
+            "keywords": [
+                "inflection",
+                "pluralize",
+                "singularize",
+                "string"
+            ],
+            "time": "2018-01-09T20:05:19+00:00"
+        },
+        {
+            "name": "illuminate/container",
+            "version": "v5.8.18",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/illuminate/container.git",
+                "reference": "9405989993a48c2cd50ad1e5b2b08a33383c3807"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/illuminate/container/zipball/9405989993a48c2cd50ad1e5b2b08a33383c3807",
+                "reference": "9405989993a48c2cd50ad1e5b2b08a33383c3807",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "illuminate/contracts": "5.8.*",
+                "illuminate/support": "5.8.*",
+                "php": "^7.1.3",
+                "psr/container": "^1.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "5.8-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Illuminate\\Container\\": ""
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Taylor Otwell",
+                    "email": "taylor@laravel.com"
+                }
+            ],
+            "description": "The Illuminate Container package.",
+            "homepage": "https://laravel.com",
+            "time": "2019-04-22T13:12:35+00:00"
+        },
+        {
+            "name": "illuminate/contracts",
+            "version": "v5.8.18",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/illuminate/contracts.git",
+                "reference": "8e2466437516edaa30201a8b5d4eaa56fa99eacd"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/illuminate/contracts/zipball/8e2466437516edaa30201a8b5d4eaa56fa99eacd",
+                "reference": "8e2466437516edaa30201a8b5d4eaa56fa99eacd",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": "^7.1.3",
+                "psr/container": "^1.0",
+                "psr/simple-cache": "^1.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "5.8-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Illuminate\\Contracts\\": ""
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Taylor Otwell",
+                    "email": "taylor@laravel.com"
+                }
+            ],
+            "description": "The Illuminate Contracts package.",
+            "homepage": "https://laravel.com",
+            "time": "2019-05-15T00:29:13+00:00"
+        },
+        {
+            "name": "illuminate/database",
+            "version": "v5.8.18",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/illuminate/database.git",
+                "reference": "421138e05c149e2e82edb0a6a86dca59a79a65a0"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/illuminate/database/zipball/421138e05c149e2e82edb0a6a86dca59a79a65a0",
+                "reference": "421138e05c149e2e82edb0a6a86dca59a79a65a0",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "ext-json": "*",
+                "illuminate/container": "5.8.*",
+                "illuminate/contracts": "5.8.*",
+                "illuminate/support": "5.8.*",
+                "php": "^7.1.3"
+            },
+            "suggest": {
+                "doctrine/dbal": "Required to rename columns and drop SQLite columns (^2.6).",
+                "fzaninotto/faker": "Required to use the eloquent factory builder (^1.4).",
+                "illuminate/console": "Required to use the database commands (5.8.*).",
+                "illuminate/events": "Required to use the observers with Eloquent (5.8.*).",
+                "illuminate/filesystem": "Required to use the migrations (5.8.*).",
+                "illuminate/pagination": "Required to paginate the result set (5.8.*)."
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "5.8-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Illuminate\\Database\\": ""
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Taylor Otwell",
+                    "email": "taylor@laravel.com"
+                }
+            ],
+            "description": "The Illuminate Database package.",
+            "homepage": "https://laravel.com",
+            "keywords": [
+                "database",
+                "laravel",
+                "orm",
+                "sql"
+            ],
+            "time": "2019-05-15T00:48:07+00:00"
+        },
+        {
+            "name": "illuminate/support",
+            "version": "v5.8.18",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/illuminate/support.git",
+                "reference": "eb6ad85fd80163fdaa5b97983e0e32d811128242"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/illuminate/support/zipball/eb6ad85fd80163fdaa5b97983e0e32d811128242",
+                "reference": "eb6ad85fd80163fdaa5b97983e0e32d811128242",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "doctrine/inflector": "^1.1",
+                "ext-json": "*",
+                "ext-mbstring": "*",
+                "illuminate/contracts": "5.8.*",
+                "nesbot/carbon": "^1.26.3 || ^2.0",
+                "php": "^7.1.3"
+            },
+            "conflict": {
+                "tightenco/collect": "<5.5.33"
+            },
+            "suggest": {
+                "illuminate/filesystem": "Required to use the composer class (5.8.*).",
+                "moontoast/math": "Required to use ordered UUIDs (^1.1).",
+                "ramsey/uuid": "Required to use Str::uuid() (^3.7).",
+                "symfony/process": "Required to use the composer class (^4.2).",
+                "symfony/var-dumper": "Required to use the dd function (^4.2).",
+                "vlucas/phpdotenv": "Required to use the env helper (^3.3)."
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "5.8-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Illuminate\\Support\\": ""
+                },
+                "files": [
+                    "helpers.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Taylor Otwell",
+                    "email": "taylor@laravel.com"
+                }
+            ],
+            "description": "The Illuminate Support package.",
+            "homepage": "https://laravel.com",
+            "time": "2019-05-17T18:49:15+00:00"
+        },
+        {
+            "name": "nesbot/carbon",
+            "version": "2.18.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/briannesbitt/Carbon.git",
+                "reference": "8322b7bd1805be31867c13bf3cdaab948a0dd406"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/8322b7bd1805be31867c13bf3cdaab948a0dd406",
+                "reference": "8322b7bd1805be31867c13bf3cdaab948a0dd406",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "ext-json": "*",
+                "php": "^7.1.8 || ^8.0",
+                "symfony/translation": "^3.4 || ^4.0"
+            },
+            "require-dev": {
+                "friendsofphp/php-cs-fixer": "^2.14 || ^3.0",
+                "kylekatarnls/multi-tester": "^1.1",
+                "phpmd/phpmd": "^2.6",
+                "phpstan/phpstan": "^0.11",
+                "phpunit/phpunit": "^7.5 || ^8.0",
+                "squizlabs/php_codesniffer": "^3.4"
+            },
+            "type": "library",
+            "extra": {
+                "laravel": {
+                    "providers": [
+                        "Carbon\\Laravel\\ServiceProvider"
+                    ]
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Carbon\\": "src/Carbon/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Brian Nesbitt",
+                    "email": "brian@nesbot.com",
+                    "homepage": "http://nesbot.com"
+                }
+            ],
+            "description": "A simple API extension for DateTime.",
+            "homepage": "http://carbon.nesbot.com",
+            "keywords": [
+                "date",
+                "datetime",
+                "time"
+            ],
+            "time": "2019-05-16T18:44:35+00:00"
+        },
+        {
+            "name": "psr/container",
+            "version": "1.0.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-fig/container.git",
+                "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f",
+                "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.0.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Psr\\Container\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "PHP-FIG",
+                    "homepage": "http://www.php-fig.org/"
+                }
+            ],
+            "description": "Common Container Interface (PHP FIG PSR-11)",
+            "homepage": "https://github.com/php-fig/container",
+            "keywords": [
+                "PSR-11",
+                "container",
+                "container-interface",
+                "container-interop",
+                "psr"
+            ],
+            "time": "2017-02-14T16:28:37+00:00"
+        },
+        {
+            "name": "psr/simple-cache",
+            "version": "1.0.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-fig/simple-cache.git",
+                "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/408d5eafb83c57f6365a3ca330ff23aa4a5fa39b",
+                "reference": "408d5eafb83c57f6365a3ca330ff23aa4a5fa39b",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.0.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Psr\\SimpleCache\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "PHP-FIG",
+                    "homepage": "http://www.php-fig.org/"
+                }
+            ],
+            "description": "Common interfaces for simple caching",
+            "keywords": [
+                "cache",
+                "caching",
+                "psr",
+                "psr-16",
+                "simple-cache"
+            ],
+            "time": "2017-10-23T01:57:42+00:00"
+        },
+        {
+            "name": "symfony/contracts",
+            "version": "v1.1.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/symfony/contracts.git",
+                "reference": "d3636025e8253c6144358ec0a62773cae588395b"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/symfony/contracts/zipball/d3636025e8253c6144358ec0a62773cae588395b",
+                "reference": "d3636025e8253c6144358ec0a62773cae588395b",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": "^7.1.3"
+            },
+            "require-dev": {
+                "psr/cache": "^1.0",
+                "psr/container": "^1.0",
+                "symfony/polyfill-intl-idn": "^1.10"
+            },
+            "suggest": {
+                "psr/cache": "When using the Cache contracts",
+                "psr/container": "When using the Service contracts",
+                "symfony/cache-contracts-implementation": "",
+                "symfony/event-dispatcher-implementation": "",
+                "symfony/http-client-contracts-implementation": "",
+                "symfony/service-contracts-implementation": "",
+                "symfony/translation-contracts-implementation": ""
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.1-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Symfony\\Contracts\\": ""
+                },
+                "exclude-from-classmap": [
+                    "**/Tests/"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Nicolas Grekas",
+                    "email": "p@tchwork.com"
+                },
+                {
+                    "name": "Symfony Community",
+                    "homepage": "https://symfony.com/contributors"
+                }
+            ],
+            "description": "A set of abstractions extracted out of the Symfony components",
+            "homepage": "https://symfony.com",
+            "keywords": [
+                "abstractions",
+                "contracts",
+                "decoupling",
+                "interfaces",
+                "interoperability",
+                "standards"
+            ],
+            "time": "2019-04-27T14:29:50+00:00"
+        },
+        {
+            "name": "symfony/polyfill-mbstring",
+            "version": "v1.11.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/symfony/polyfill-mbstring.git",
+                "reference": "fe5e94c604826c35a32fa832f35bd036b6799609"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/fe5e94c604826c35a32fa832f35bd036b6799609",
+                "reference": "fe5e94c604826c35a32fa832f35bd036b6799609",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": ">=5.3.3"
+            },
+            "suggest": {
+                "ext-mbstring": "For best performance"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.11-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Symfony\\Polyfill\\Mbstring\\": ""
+                },
+                "files": [
+                    "bootstrap.php"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Nicolas Grekas",
+                    "email": "p@tchwork.com"
+                },
+                {
+                    "name": "Symfony Community",
+                    "homepage": "https://symfony.com/contributors"
+                }
+            ],
+            "description": "Symfony polyfill for the Mbstring extension",
+            "homepage": "https://symfony.com",
+            "keywords": [
+                "compatibility",
+                "mbstring",
+                "polyfill",
+                "portable",
+                "shim"
+            ],
+            "time": "2019-02-06T07:57:58+00:00"
+        },
+        {
+            "name": "symfony/translation",
+            "version": "v4.2.8",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/symfony/translation.git",
+                "reference": "181a426dd129cb496f12d7e7555f6d0b37a7615b"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/symfony/translation/zipball/181a426dd129cb496f12d7e7555f6d0b37a7615b",
+                "reference": "181a426dd129cb496f12d7e7555f6d0b37a7615b",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "php": "^7.1.3",
+                "symfony/contracts": "^1.0.2",
+                "symfony/polyfill-mbstring": "~1.0"
+            },
+            "conflict": {
+                "symfony/config": "<3.4",
+                "symfony/dependency-injection": "<3.4",
+                "symfony/yaml": "<3.4"
+            },
+            "provide": {
+                "symfony/translation-contracts-implementation": "1.0"
+            },
+            "require-dev": {
+                "psr/log": "~1.0",
+                "symfony/config": "~3.4|~4.0",
+                "symfony/console": "~3.4|~4.0",
+                "symfony/dependency-injection": "~3.4|~4.0",
+                "symfony/finder": "~2.8|~3.0|~4.0",
+                "symfony/http-kernel": "~3.4|~4.0",
+                "symfony/intl": "~3.4|~4.0",
+                "symfony/var-dumper": "~3.4|~4.0",
+                "symfony/yaml": "~3.4|~4.0"
+            },
+            "suggest": {
+                "psr/log-implementation": "To use logging capability in translator",
+                "symfony/config": "",
+                "symfony/yaml": ""
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "4.2-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Symfony\\Component\\Translation\\": ""
+                },
+                "exclude-from-classmap": [
+                    "/Tests/"
+                ]
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Fabien Potencier",
+                    "email": "fabien@symfony.com"
+                },
+                {
+                    "name": "Symfony Community",
+                    "homepage": "https://symfony.com/contributors"
+                }
+            ],
+            "description": "Symfony Translation Component",
+            "homepage": "https://symfony.com",
+            "time": "2019-05-01T12:55:36+00:00"
+        },
+        {
+            "name": "textalk/websocket",
+            "version": "1.2.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/Textalk/websocket-php.git",
+                "reference": "bfa18bb6bf523680c7803f6b04694fbbf2f67bbf"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/Textalk/websocket-php/zipball/bfa18bb6bf523680c7803f6b04694fbbf2f67bbf",
+                "reference": "bfa18bb6bf523680c7803f6b04694fbbf2f67bbf",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://dl.laravel-china.org/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require-dev": {
+                "cboden/ratchet": "0.3.*",
+                "phpunit/phpunit": "4.1.*",
+                "phpunit/phpunit-selenium": "1.3.3",
+                "satooshi/php-coveralls": "dev-master"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-4": {
+                    "WebSocket\\": "lib"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Fredrik Liljegren",
+                    "email": "fredrik.liljegren@textalk.se"
+                }
+            ],
+            "description": "WebSocket client and server",
+            "time": "2015-10-09T07:32:42+00:00"
+        }
+    ],
+    "packages-dev": [],
+    "aliases": [],
+    "minimum-stability": "stable",
+    "stability-flags": [],
+    "prefer-stable": false,
+    "prefer-lowest": false,
+    "platform": {
+        "php": ">=7.0.0"
+    },
+    "platform-dev": []
+}

+ 108 - 0
websockServ/configs/configs.php

@@ -0,0 +1,108 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2019/5/20
+ * Time: 9:09
+ */
+
+$tmp_config = [
+    'common' => [
+        'mysql' => [
+            'driver' => 'mysql',
+            'host' => '192.168.2.200',
+            'port' => 3306,
+            'database' => 'ds_cms',
+            'username' => 'vali',
+            'user' => 'vali',
+            'password' => '1234',
+            'charset' => 'utf8mb4',
+            'collation' => 'utf8mb4_unicode_ci'
+        ],
+        'pgsql' => [
+            'driver' => 'pgsql',
+            'host' => '192.168.2.200',
+            'port' => 10432,
+            'database' => 'pai',
+            'username' => 'kaiyou',
+            'password' => '123456',
+            'charset' => 'utf8',
+            'collation' => 'utf8_unicode_ci',
+            'prefix' => ''
+        ],
+        'redis' => [
+            'host' => '192.168.2.200',
+            'port' => 26379,
+            'passwd' => '',
+            'db' => 0,
+            'overtime' => 1,
+        ],
+        'swoole' => [
+            'worker_num' => 4,
+            'daemonize' => true,
+            'max_request' => 50,
+            'task_enable_coroutine' => true,
+            'dispatch_mode' => 2,
+            'debug_mode' => 1,
+            'task_worker_num' => 20,
+            'log_file' => '../logs/err.log',
+        ],
+
+        'swoolev2' => [
+            'worker_num' => 2,
+            'daemonize' => false,
+            'max_request' => 5000,
+            'task_enable_coroutine' => true,
+            'dispatch_mode' => 2,
+            'debug_mode' => 1,
+            'task_worker_num' => 10,
+            'log_file' => '../logs/errv2.log',
+        ],
+        'pgsqlpoole'=>[
+            'poole_host' => '0.0.0.0' ,
+            'poole_port' => 9091,
+
+            'db_type' => 'pgsql',
+            'db_host' => '192.168.2.200',
+            'db_port' => 10432,
+            'db_name' => 'pai',
+            'db_user' => 'kaiyou',
+            'db_pwd' => '123456',
+        ],
+        'mysqlpoole'=>[
+            'poole_host' => '0.0.0.0' ,
+            'poole_port' => 9091,
+
+            'db_type' => 'mysql',
+            'db_host' => '192.168.2.200',
+            'db_port' => 3306,
+            'db_name' => 'ds_cms',
+            'db_user' => 'vali',
+            'db_pwd' => '1234',
+        ],
+        'commpoole'=>[
+            'poole_host' => '0.0.0.0' ,
+            'poole_port' => 9091,
+
+            'db_type' => 'mysql',
+            'db_host' => '192.168.2.200',
+            'db_port' => 3306,
+            'db_name' => 'ds_cms',
+            'db_user' => 'vali',
+            'db_pwd' => '1234',
+
+        ],
+
+
+    ],
+    'dev' => [
+
+    ],
+    'test' => [
+
+    ],
+    'prod' => [
+    ],
+];
+
+return array_merge($tmp_config['common'], $tmp_config[ENV]);