# 安裝
## thinkphp5.1中一定要帶上=1.*不然會報錯
```
composer require workerman/channel=1.*
```
## 錯誤解決:
```
一般在報版本錯誤的時候 就加上 = 版本號 * 就可以
```

# channel啟動服務器與客戶端
~~~
<?php
namespace app\http;
use Channel\Client;
use think\worker\Server;
use Channel\Server as Channel_Server;
class On extends Server
{
protected $socket = 'websocket://0.0.0.0:7777';
protected $option=['name'=>"學習",'count'=>2]; //修改名稱,啟動多少進程等配置
//啟動成功的回調
public function onConnect($connection){
//連接成功后發送給客戶端 worker_id 和connection_id用戶ID
$connection->send('connection_id='.$connection->id.'worker_id='.$this->worker->id);
echo $connection->getRemoteIp();
}
//設置Worker子進程啟動時的回調函數,每個子進程啟動時都會執行。
public function onWorkerStart($worker){
Client::connect('127.0.0.1',8881);
//單個用戶訂閱事件
Client::on($worker->id,function ($data)use($worker){
//沒有用戶ID的返回掉
if(!isset($data['connection_id'])){
return ;
}
//獲得用戶ID的對象然后發送消息
$connection=$worker->connections[$data['connection_id']]; //如果得知connection的編號為$id,可以很方便的通過$worker->connections[$id]獲得對應的connection對象,從而操作對應的socket連接,例如通過$worker->connections[$id]->send('...') 發送數據。
$connection->send($data['content']);//向對應的用戶編號發布消息
});
//所有網站鏈接用戶的訂閱事件
Client::on('broadcast',function ($data)use($worker){
foreach ($worker->connections as $conn){
$conn->send($data['content']);
}
});
}
}
//channel服務器端開啟
new Channel_Server('0.0.0.0',8881);
~~~
## 寫一個HTTP端發送數據
~~~
<?php
namespace app\http;
use Channel\Client;
use think\worker\Server;
class Publish extends Server
{
protected $socket = 'http://0.0.0.0:9999';
protected $option = ['name' => "http發送"]; //修改名稱,啟動多少進程等配置
public function onWorkerStart($worker)
{
Client::connect('127.0.0.1', 8881);
}
public function onMessage($connection, $data)
{
$connection->send('oook');
// http://111.67.202.126:9999?content=login success&connection_id=2&worker_id=0 //單個用戶發布
if(!isset($_GET['content'])){
return ;
}
if (isset($_GET['worker_id']) && isset($_GET['connection_id'])) {
$data = ['connection_id' => $_GET['connection_id'], 'content' => $_GET['content']];
Client::publish($_GET['worker_id'], $data);
} else {
Client::publish('broadcast',['content' => $_GET['content']]);
}
}
}
~~~
# 定時器端,定時發送
~~~
<?php
namespace app\http;
use Workerman\Lib\Timer as T;
use think\worker\Server;
class Timer extends Server
{
protected $option=['name'=>"定時器"]; //修改名稱,啟動多少進程等配置
public function onWorkerStart(){
$time_interval = 5;
T::add($time_interval, function()
{
$u='http://111.67.202.126:9999?content=虎贏'; //網站
$ch=curl_init(); //初始化curl會話返回curl句柄(資源)
curl_setopt($ch,CURLOPT_URL,$u); //設置獲取地址變量
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); //以字符串形式返回
$ma=curl_exec($ch);
curl_close($ch); //關閉句柄
echo $ma."\n";
});
}
}
~~~
## 啟動起來效果

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