workman 初用之 demo 及 接口调用

<?php /** * Created by PhpStorm. * User: FMY * Date: 2019/3/18 * Time: 10:35 */ use Workerman\Worker; require_once __DIR__ . '\Workerman-master/Autoloader.php'; require_once __DIR__ . '\Workerman-master/Lib/MySQL.php'; $websocekt_worker = new Worker('websocket://127.0.0.1:9999'); $websocekt_worker->count = 1; $websocekt_worker->onWorkerStart = function ($worker) { global $db; $db = new \Workerman\MySQL\Connection('192.168.50.82', '3306', 'yooyun', 'yooyun', 'yooyun'); }; //客户端建立连接 $websocekt_worker->onConnect = function ($counnetion) { echo 'aa'; }; //客户端发送消息 $websocekt_worker->onMessage = function ($connection, $data) { global $db; $json_decode_data = json_decode($data, true); if (!isset($json_decode_data['cmd'])) { echo '必要参数缺失!'; } $cmd = $json_decode_data['cmd']; $cmd_array = explode('\\', $cmd); $controller = 'Php' . DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR . ucfirst($cmd_array[0]); $action = $cmd_array[1]; $class = new $controller($db); $class->$action($json_decode_data, $connection); }; //客户端连接关闭 $websocekt_worker->onClose = function ($connection) { }; Worker::runAll();

猜你喜欢

转载自blog.csdn.net/weixin_41612889/article/details/88824840