# 心跳檢測程序
## 官方文檔
https://www.workerman.net/doc/workerman/faq/heartbeat.html
~~~
<?php
require_once 'workerman/Autoloader.php';
require_once 'workerman/Lib/Timer.php';
use Workerman\Worker;
use Workerman\Timer;
$worker = new Worker('text://0.0.0.0:2222',);
//當客戶端發來信息的時候 設置一個 lastMessageTime臨時屬性 記錄時間
$worker->onMessage=function ($connection,$data) {
$connection->lastMessageTime=time();
};
//子進程啟動時候記錄時間,也就是鏈接的時候
//$worker->connections 此屬性中存儲了當前進程的所有的客戶端連接對象,其中id為connection的id編號
$worker->onWorkerStart=function ($worker){
Timer::add(1,function ()use($worker){
$now_time=time();
//$conn 此屬性中存儲了當前進程的所有的客戶端連接對象
foreach ($worker->connections as $conn){
if(empty($conn->lastMessageTime)){
$conn->lastMessageTime=$now_time;
}
//判斷如果大于10秒未有信息將會斷開
if($now_time-$conn->lastMessageTime>10){
$conn->close("長時間未連接斷開");
}
}
});
};
Worker::runAll();
~~~
- WebSocket協議
- 構造函數(6種協議)
- count(進程設置)
- name(鏈接名稱)
- $daemonize(守護進程設置)
- logFile(日志路徑)
- stdoutFile(守護進程記錄文件)
- connections(獲取鏈接數組的)
- worker的回調屬性
- worker類的方法
- Connection類的方法
- getRemotePort獲取端口方法
- getRemoteIp獲取IP地址
- close 安全關閉連接
- 定時器
- Channel分布式通信組件
- 心跳檢測程序
- liunx優化配置
- thinkphp5.1使用worerman
- thinkphp5.1中用Channel實現廣播通信
- thinkphp5.1中使用定時器
- thinkphp5.1使用TcpConnection類
- Gateway類使用
- BusinessWorker使用
- Register類的使用
- Events類使用(業務邏輯層)
- Lib\Gateway 接口(經常用)
- webman中間件stomp
- Gateway在thinkphp5.1里使用