thinkphp5使用Workerman启动udp服务

1. 安装(composer)
composer require topthink/think-worker

如果是在window下还需要安装workerman/workerman-for-win
(然后删掉vendor\workerman\workerman)

composer require workerman/workerman-for-win
2. 新增启动文件在tp5根目录server.php内容如下

新增服务处理类在controller下创建\index\controller\Worker.php 代码如下

send($data);
    }

    /**
     * 当客户端的连接上发生错误时触发
     * @param $connection
     * @param $code
     * @param $msg
     */
    public function onError($connection, $code, $msg)
    {
        echo "error $code $msg\n";
    }

    /**
     * 每个进程启动
     * @param $worker
     */
    public function onWorkerStart($worker)
    {
        echo "onWorkerStart___";
    }
}

3. 添加.pid文件(启动时候报错file_get_contents(/data/project/tp5project/vendor/workerman/workerman/../_data_project_tp5project_server.php.pid): 需要操作此步骤)

在centos7下启动服务的时候没有自动创建.pid文件,固需要手动添加这个空文件
/data/project/tp5project/vendor/workerman/_data_project_bomei_server.php.pid

4. 修改php.ini(启动时报错:stream_socket_server() has been disabled for security reasons需要修改)

/usr/local/php/etc/php.ini
找到disable_functions 这行去掉stream_socket_server

disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru
5. 添加udp所需端口
firewall-cmd --zone=public --add-port=2346/udp --permanent
firewall-cmd --reload

如果用的阿里云服务器需要在安全策略添加开放2346端口

6. 启动
php server.php start
thinkphp5使用Workerman启动udp服务_第1张图片

你可能感兴趣的:(thinkphp5使用Workerman启动udp服务)