下面把TP5官方给出的示例代码:
<?php
namespace 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功能。