# 官方文檔:
https://www.workerman.net/doc/workerman/components/channel.html
## 發布訂閱,類似于廣播的效果所有鏈接的人都能看到
~~~
<?php
require_once 'workerman/Autoloader.php';
require_once 'channel-master/src/Server.php';
require_once 'channel-master/src/Client.php';
use Workerman\Worker;
use Channel\Client;
use Channel\Server;
//服務端與客戶端合并到一起用
//------------服務端------------------//
$channel_server = new Server('0.0.0.0',6666);
//------------客戶端------------------//
$worker=new Worker('text://0.0.0.0:7777');
$worker->onWorkerStart=function($worker){
Client::connect('127.0.0.1',6666);
//以下為訂閱消息
Client::on('broadcast', function($data)use($worker){ //訂閱消息 ‘broadcast’
// 向當前worker進程的所有客戶端廣播消息
foreach($worker->connections as $connection)
{
$connection->send($data);
}
});
//以下為發布消息
$worker->onMessage=function ($connection,$data){ //發布消息
Client::publish('broadcast',$data);
};
};
Worker::runAll();
~~~
## 取消訂閱
```
Client::unsubscribe('broadcast')
```
## 注意要連接客戶端的鏈接: IP:7777

- 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里使用