# 心跳
注意:長鏈接應用必須加心跳,否則鏈接可能由于長時間未通訊被路由節點強行斷開。
心跳作用主要有兩個:
1、客戶端定時給服務端發送點數據,防止連接由于長時間沒有通訊而被某些節點的防火墻關閉導致連接斷開的情況。
2、服務端可以通過心跳來判斷客戶端是否在線,如果客戶端在規定時間內沒有發來任何數據,就認為客戶端下線。這樣可以檢測到客戶端由于極端情況(斷電、斷網等)下線的事件。
使用Timer定時器功能可以實現發送心跳包的功能。事實上,Swoole已經內置了心跳檢測功能,能自動close掉長時間沒有數據來往的連接。而開啟心跳檢測功能,只需要設置heartbeat_check_interval和heartbeat_idle_time即可。如下:
~~~
$this->serv->set(
array(
'heartbeat_check_interval' => 60,
'heartbeat_idle_time' => 600,
)
);
~~~
* heartbeat_check_interval:啟用心跳檢測,此選項表示每隔多久輪循一次,單位為秒。如 heartbeat_check_interval => 60,表示每60秒,遍歷所有連接,如果該連接在60秒內,沒有向服務器發送任何數據,此連接將被強制關閉。
* heartbeat_idle_time:與heartbeat_check_interval配合使用。表示連接最大允許空閑的時間。
如
~~~
array(
'heartbeat_idle_time' => 600,
'heartbeat_check_interval' => 60,
);
~~~
表示每60秒遍歷一次,一個連接如果600秒內未向服務器發送任何數據,此連接將被強制關閉
* 啟用heartbeat_idle_time后,服務器并不會主動向客戶端發送數據包
* 如果只設置了heartbeat_idle_time未設置heartbeat_check_interval底層將不會創建心跳檢測線程,PHP代碼中可以調用heartbeat方法手工處理超時的連接
* 在設置這兩個選項后,swoole會在內部啟動一個線程,每隔heartbeat_check_interval秒后遍歷一次全部連接,檢查最近一次發送數據的時間和當前時間的差,如果這個差值大于heartbeat_idle_time,則會強制關閉這個連接,并通過回調onClose通知Server進程。
## 高級用法:
使用swoole_server::heartbeat()函數手工檢測心跳是否到期。此函數會返回閑置時間超過heartbeat_idle_time的所有TCP連接。程序中可以將這些連接做一些操作,如發送數據或關閉連接。
- swoole簡介
- swoole功能概述
- 序章
- 開發必讀
- 1 環境搭建
- 1.1 環境搭建
- 1.2 搭建Echo服務器
- 2 初識Swoole
- 2.1 Worker進程
- 2.2 TaskWorker進程
- 2.3 Timer定時器
- 2.4 Process進程
- 2.5 Table內存表
- 2.6 多端口監聽
- 2.7 sendfile文件支持
- 2.8 SSL支持
- 2.9 熱重啟
- 2.10 http_server
- 附錄*server配置
- 附錄*server函數
- 附錄*server屬性
- 附錄*server回調函數
- 附錄*server高級特性
- 心跳檢測
- 3 Swoole協議
- 3.1 EOF協議
- 3.2 固定包頭協議
- 3.3 Http協議
- 3.4 WebSocket協議
- 3.5 MTQQ協議
- 內置http_server
- 內置websocket_server
- Swoole\Redis\Server
- 4 Swoole異步IO
- 4.1 AsyncIO
- 異步文件系統IO
- swoole_async_readfile
- swoole_async_writefile
- swoole_async_read
- swoole_async_write
- 5 swoole異步客戶端
- ws_client
- http_client
- mysql_client
- redis_client
- tcp_client
- http2_client
- 6 swoole協程
- Swoole\Coroutine\Http\Client
- Swoole\Coroutine\MySQL
- Swoole\Coroutine\Redis
- Coroutine\PostgreSQL
- Swoole\Coroutine\Client
- Swoole\Coroutine\Socket
- Swoole\Coroutine\Channel
- Coroutine
- Swoole\Coroutine::create
- Swoole\Coroutine::resume
- Swoole\Coroutine::suspend
- Swoole\Coroutine::sleep
- Coroutine::getaddrinfo
- Coroutine::gethostbyname
- swoole_async_dns_lookup_coro
- Swoole\Coroutine::getuid
- getDefer
- setDefer
- recv
- Coroutine::stats
- Coroutine::fread
- Coroutine::fget
- Coroutine::fwrite
- Coroutine::readFIle
- Coroutine::writeFIle
- Coroutine::exec
- 7 swoole_process
- process::construct
- process::start
- process::name
- process::signal
- process::setaffinity
- process::exit
- process::kill
- process::daemon
- process->exec
- process::wait
- process::alarm
- 8 swoole定時器
- swoole_timer_tick
- swoole_timer_after
- swoole_timer_clear
- 9 swoole_event
- swoole_event_add
- swoole_event_set
- swoole_event_del
- swoole_event_wait
- swoole_event_defer
- swoole_event_write
- swoole_event_exit
- swoole提供的function
- 常見問題
- 客戶端鏈接失敗原因
- 如何設置進程數
- 如何實現異步任務
- 如何選擇swoole三種模式
- php中哪些函數是阻塞的
- 是否可以共用1個redis或mysql連接
- 如何在回調函數中訪問外部的變量
- 為什么不要send完后立即close
- 不同的Server程序實例間如何通信
- MySQL的連接池、異步、斷線重連
- 在php-fpm或apache中使用swoole
- 學習Swoole需要掌握哪些基礎知識
- 在phpinfo中有在php-m中沒有
- 同步阻塞與異步非阻塞選擇
- CURL發送POST請求服務器端超時
- 附錄
- 預定義常量
- 內核參數調優
- php四種回調寫法
- 守護進程程序常用數據結構
- swoole生命周期
- swoole_server中內存管理機制
- 使用jemalloc優化swoole內存分配性能
- Reactor、Worker、Task的關系
- Manager進程
- Swoole的實現
- Reactor線程
- 安裝擴展
- swoole-worker手冊
- swoole相關開源項目
- 寫在后面的話
- 版本更新記錄
- 4.0