php : tp框架swoole实现websocket,wss

class Swoole extends \think\swoole\Server
{
    protected static $token;
    protected $host = '0.0.0.0';
    protected $port = 9503;
    //protected $serverType = 'socket';
    protected $serverType = 'ssl';
    protected static $uid = '';
    protected $mode = SWOOLE_PROCESS;
    protected $sockType = SWOOLE_SOCK_TCP | SWOOLE_SSL;
    protected $option = [
        'worker_num' => 4, // 设置启动的Worker进程数
        'daemonize' => true, //守护进程化。
        'backlog' => 128, //Listen队列长度,
        'dispatch_mode' => 2,
        'heartbeat_check_interval' => 5, //5秒跟新
        'heartbeat_idle_time' => 100,    //连接超时
        'ssl_cert_file' => "/www/cert/wss.qhoo.top.pem",
        'ssl_key_file'  => "/www/cert/wss.qhoo.top.key"
    ];

    //连接
    public function onOpen( $server,  $req)
    {
        //fd:进入ws给的id每次进入都会变
        $fd = $req->fd;
        //token={{$token}}&appid={{$appid}}&nonce={{$nonce}}&timestamps={{$timestamps}}
        $token = $req->get['token'];
        $appid = $req->get['appid'];
        $nonce = $req->get['nonce'];
        $timestamps = $req->get['timestamps'];
        $tokens = md5($appid.'hudong233'.$nonce.$timestamps);
        if($token !== $tokens) {

            $server->push($fd, Tool::showAjax(['status' => 3, 'message' => '请先登录!']));
            $server->close($fd);
            return;
        }
      /*  //获取openid;
        $token = $req->get['token'] ?? '';    //拿到客户端用户ID
        if (!$token) {
            $server->push($fd, Tool::showAjax(['status' => 3, 'message' => '请先登录!']));
            $server->close($fd);
            return;
        }
        //可以存入登陆状态
        $userData = Cache::get($token);
        if (!$userData) {
            $server->push($fd, Tool::showAjax(['status' => 2, 'message' => '用户身份无效,请重新登录!']));
            $server->close($fd);
            return;
        }

        self::$uid = $req->get['friendId'];
        if (self::$uid == $userData['uid']) {
            //发送数据
            $server->push($fd, Tool::showAjax(['status' => 2, 'message' => '非法操作!']));
            $server->close($fd);
        }
        $server->bind($fd, $userData['uid']);
        $messages = Message::getMessage($userData['uid']);
        //$req->fd 服务器识别id(会变)
        (new UserModel())->where('id', $userData['uid'])->update(['fd' => $req->fd]);
        $arr = $messages->toArray();
        sort($arr);*/

        $server->push($fd, Tool::showAjax(['status' => 1, 'message' => 'success', 'data' => '888']));
    }


    //接受数据
    public function onMessage($server,  $frame)
    {
        //$frame->data 发送的数据
        //$frame->fd   发送人的服务器id
        $message = $frame->data;
        if (empty($message)) return;
        //$message = file_put_contents("./log.txt", "$message", FILE_APPEND);
        //给所有人发送
        db('user')->insert(['v'=>$message]);
        foreach ($server->connections as $key => $value) {
            $server->push($value, Tool::showAjax(['status' => 1, 'message' => 'success', 'data' => $message]));
            //$ws->push($value, $data1);
        }
        //$server->push($frame->fd, Tool::showAjax(['status' => 1, 'message' => 'success', 'data' => $arr]));
    }

    function onClose( $server, int $fd, int $reactorId)
    {
        /*(new UserModel())->where('fd', $fd)->update(['fd' => '']);*/
    }
    //
    private function open1($server,$req) {
        //fd:进入ws给的id每次进入都会变
        $fd = $req->fd;
        //获取openid;
        $token = $req->get['token'] ?? '';    //拿到客户端用户ID
        if (!$token) {
            $server->push($fd, Tool::showAjax(['status' => 3, 'message' => '请先登录!']));
            $server->close($fd);
            return;
        }
        //可以存入登陆状态
        $userData = Cache::get($token);
        if (!$userData) {
            $server->push($fd, Tool::showAjax(['status' => 2, 'message' => '用户身份无效,请重新登录!']));
            $server->close($fd);
            return;
        }

        self::$uid = $req->get['friendId'];
        if (self::$uid == $userData['uid']) {
            $server->push($fd, Tool::showAjax(['status' => 2, 'message' => '非法操作!']));
            $server->close($fd);
        }
        $server->bind($fd, $userData['uid']);
        $messages = Message::getMessage($userData['uid']);
        //$req->fd 服务器识别id(会变)
        (new UserModel())->where('id', $userData['uid'])->update(['fd' => $req->fd]);
        $arr = $messages->toArray();
        sort($arr);

        $server->push($fd, Tool::showAjax(['status' => 1, 'message' => 'success', 'data' => $arr]));
    }
}

 

上一篇:在laravel5.8中集成swoole组件


下一篇:(转) swoole之代码热更新实现