ThinkPHP框架不多说,强悍之处不容置疑。有疑问看官方手册
Workerman框架也是非常强悍的(开发框架人的背景不是很清楚),官方是这样介绍的:
Workerman,让你看到PHP不为人知的一面。
当然详细的介绍WM官方也给出了非常详细的手册。这里也并不打算长篇的介绍和吹嘘TP与WM的强悍的话。以前在使用TP3.2.3的时候,就接触使用了Workerman框架,但当时TP官方并没有把WM集成进来,也就分开独立使用两个框架了,各司其职。现在TP5中已经把WM作为扩展集成进来了。
集成步骤也请看TP5的手册:
https://www.kancloud.cn/manual/thinkphp5/235128
下面把TP5官方给出的示例代码:
<?phpnamespace app\index\controller;use think\worker\Server;use Workerman\Lib\Timer; // 引入WM框架的类库,class Worker extends Server{// protected $socket = 'http://0.0.0.0:2348'; protected $socket = 'tcp://0.0.0.0:2349'; /** * 收到信息 * @param $connection * @param $data */ public function onMessage($connection, $data) { // 给connection临时设置一个lastMessageTime属性,用来记录上次收到消息的时间 $connection->lastMessageTime = time(); $connection->send(json_encode($data)); $connection->send('我收到你的信息了'); } /** * 当连接建立时触发的回调函数 * @param $connection */ public function onConnect($connection) { echo $connection->getRemoteIP(); } /** * 当连接断开时触发的回调函数 * @param $connection */ public function onClose($connection) { echo $connection->id . 'disconnect \r\n'; } /** * 当客户端的连接上发生错误时触发 * @param $connection * @param $code * @param $msg */ public function onError($connection, $code, $msg) { echo "error $code $msg\n"; } /** * 每个进程启动 * @param $worker */ public function onWorkerStart($worker) { // 进程启动后设置一个每秒运行一次的定时器 Timer::add(1, function ()use($worker){ $time_now = time(); foreach ($worker->connections as $connection) { // 有可能该connection还没收到过消息,则lastMessageTime设置为当前时间 if (empty($connection->lastMessageTime)) { $connection->lastMessageTime = $time_now; continue; } // 上次通讯时间间隔大于心跳间隔,则认为客户端已经下线,关闭连接 if ($time_now - $connection->lastMessageTime > 10) { $connection->close(); } } }); echo $worker->id . "\r\n"; }}
这个示例是用来作为TCP协议开发的,看onMessage、onWorkerStart两个函数,是增加一个定时器,因为是做TCP服务端,必须要使用心跳来检测客户端是否因极端情况(断电、异常关机)而导致断开(这种状况服务端是无法立即得知客户端的断开状态的)。实现的逻辑是Worker类继承Server类,Worker类中给参数赋值,然后由父类的Server构造函数来实例化Worker对象,
------------------------------------------------------------
Server.php
<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]// +----------------------------------------------------------------------// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: liu21st <liu21st@gmail.com>// +----------------------------------------------------------------------namespace think\worker;use Workerman\Worker;use Workerman\Lib\Timer;/** * Worker控制器扩展类 */abstract class Server{ protected $worker; protected $worker2; protected $socket = ''; protected $protocol = 'http'; protected $host = '0.0.0.0'; protected $port = '2346'; protected $processes = 1; /** * 架构函数 * @access public */ public function __construct() { // 实例化 Websocket 服务 $this->worker = new Worker($this->socket ?: $this->protocol . '://' . $this->host . ':' . $this->port);// $this->worker2 = new Worker('tcp://0.0.0.0:2349'); // 设置进程数 $this->worker->count = $this->processes; // 设置进程名称 $this->worker->name = "bluetooth"; // 初始化 $this->init(); // 设置回调 foreach (['onWorkerStart', 'onConnect', 'onMessage', 'onClose', 'onError', 'onBufferFull', 'onBufferDrain', 'onWorkerStop', 'onWorkerReload'] as $event) { if (method_exists($this, $event)) { $this->worker->$event = [$this, $event]; } } // Run worker Worker::runAll(); } protected function init() { }}
虽然是集成进来,但依旧很灵活,可以在Server.php里面自定义你所需要的任意WM功能。
本文出自 “为了以后” 博客,谢绝转载!