## PHP
TCP 服務是基于 Socket 開發的,因此客戶端我們能直接使用 PHP 的 Socket 操作方法連接,得益于 [Swoole 的 Hook](https://wiki.swoole.com/wiki/page/p-runtime.html) 技術 (mix 默認已經開啟 Hook) ,PHP 的原生 Socket 操作可在協程中使用。
- 將下面代碼保存為 `client.php`:
> 其中 EOF 要與 TCP 服務器的 [StartCommand:class](https://github.com/mix-php/mix-skeleton/tree/v2.1/app/Tcp/Commands/StartCommand.php#L40) 中一至
~~~
<?php
/**
* Class JsonRpcTcpClient
* @author liu,jian <coder.keda@gmail.com>
*/
class JsonRpcTcpClient
{
/**
* @var resource
*/
public $socket;
/**
* @var string
*/
public $eof = "\n";
/**
* @var int
*/
public $timeout = 5;
/**
* @var int
*/
public $lineMaxLength = 4096;
/**
* JsonRpcTcpClient constructor.
* @param string $host
* @param int $port
*/
public function __construct(string $host, int $port)
{
$socket = stream_socket_client("tcp://{$host}:{$port}", $errno, $errstr, $this->timeout);
if (!$socket) {
throw new \RuntimeException("JSON-RPC tcp client connection failed, [$errno] {$errstr}");
}
$this->socket = $socket;
}
/**
* 執行方法
* @param string $method
* @param array $params
* @param int $id
* @return array
*/
public function call(string $method, array $params, int $id = 0)
{
$ret = fwrite($this->socket, json_encode([
'method' => $method,
'params' => $params,
'id' => $id,
]) . $this->eof);
if ($ret === false) {
throw new \RuntimeException('JSON-RPC tcp client write failed.');
}
stream_set_timeout($this->socket, $this->timeout);
$data = stream_get_line($this->socket, $this->lineMaxLength, $this->eof);
if ($data === false) {
throw new \RuntimeException('JSON-RPC tcp client read failed.');
}
return json_decode($data, true);
}
/**
* Destruct
*/
public function __destruct()
{
// TODO: Implement __destruct() method.
fclose($this->socket);
}
}
// 執行RPC
$client = new JsonRpcTcpClient('127.0.0.1', 9503);
$result = $client->call('foo.bar', [], 123);
var_dump($result);
~~~
**1. 啟動服務器**
~~~
php bin/mix.php tcp:start
~~~
**2. 執行 `client.php`**
~~~
php client.php
~~~
接收到加入成功的消息:
~~~
[root@localhost data]# php client.php
array(4) {
["jsonrpc"]=>
string(3) "2.0"
["error"]=>
NULL
["result"]=>
array(1) {
[0]=>
string(13) "Hello, World!"
}
["id"]=>
int(123)
}
~~~
- 歡迎使用 MixPHP
- 安裝說明
- 全棧安裝
- Phar 開發安裝
- 新手教程
- 命令行常識
- 進程管理
- 熱更新
- 全局變量
- 入門須知
- 命名空間
- 自動加載
- 入口文件
- 增改應用
- 核心功能
- 配置 (manifest.php)
- 協程
- 什么是協程
- 開啟協程
- PHP Stream Hook
- xgo + Channel
- WaitGroup + xdefer
- 連接池
- 協程池
- 定時器
- 依賴注入
- 事件調度
- 命令行
- 簡介
- Application
- 創建命令
- 命令參數
- 打印與顏色
- 守護進程
- 后臺運行
- Web/API 應用
- 簡介
- 服務器
- 路由
- 中間件
- 請求
- 文件上傳
- 響應
- 控制器
- 視圖
- Auth
- Session
- 客戶端
- GuzzleHttp
- 調試與錯誤
- 安全建議
- WebSocket 應用
- 簡介
- 服務器
- 客戶端
- JavaScript
- Swoole
- nginx代理
- 60s無消息斷線
- TCP 應用
- 簡介
- 服務器
- 客戶端
- Telnet
- PHP
- Swoole
- UDP 應用
- 簡介
- 服務器
- 客戶端
- NC
- Swoole
- Sync Invoke 同步調用
- 簡介
- 服務器
- 客戶端
- 公共組件
- 驗證器
- 驗證器定義
- 驗證規則
- 靜態調用
- 日志
- 緩存
- 數據庫
- Database
- ConnectionPool
- Connection
- QueryBuilder
- ExecutedEvent
- Redis
- ConnectionPool
- Connection
- CalledEvent
- 常見問題
- 如何利用CPU多核
- 連接多個數據庫
- 使用主從數據庫
- 如何設置跨域
- form-data 上傳文件失敗
- 輸出大于2M的文件失敗 (xlsx)
- 如何接入EasyWeChat
- 升級指導
- 不兼容修改-001
- 不兼容修改-002
- 不兼容修改-003