tp6+workerman简单实现通讯
首先通过 composer 安装
composer require topthink/think-worker
think-worker默认帮你安装了workman。
注意:tp5.1运行这句的话会报错,因为最新版的think-worker是要tp6框架的,tp5.1要找以前的版本
config worker_server.php 更改配置
return ['worker_class' => 'app\index\controller\Worker', // 自定义Workerman服务类名 支持数组定义多个服务
];
namespace app\index\controller;
use think\facade\Db;
use think\worker\Server;
use Workerman\Lib\Timer;
define('HEARTBEAT_TIME', 20);// 心跳间隔55秒
class Worker extends Server
{protected $socket = 'http://0.0.0.0:2345';public function __construct(){parent::__construct();$this->onMessage();// 或者这样调用$this->worker->onWorkerStart = function($worker){echo "Worker starting...\n";};}/*** 收到信息* @param $connection* @param $data*/public function onMessage(){$this->worker->onMessage = function($connection, $data){dump($data);$connection->send($data);};}}
php think worker:server 在命令行启动服务端
html:
WebSocket示例
参考地址:http://www.ainqs.com/article/s/33.html