# connect 方法
```php
void AsyncTcpConnection::connect()
```
執行異步連接操作。此方法會立刻返回。
注意:如果需要設置異步連接的onError回調,則應該在connect執行之前設置,否則onError回調可能無法被觸發,例如下面的例子onError回調可能無法觸發,無法捕獲異步連接失敗事件。
```php
$connection = new AsyncTcpConnection('tcp://baidu.com:81');
// 執行連接的時候還沒設置onError回調
$connection->connect();
$connection->onError = function($connection, $err_code, $err_msg)
{
echo "$err_code, $err_msg";
};
```
### 參數
無參數
### 返回值
無返回值
### 示例 Mysql代理
```php
use \Workerman\Worker;
use \Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/Workerman/Autoloader.php';
// 真實的mysql地址,假設這里是本機3306端口
$REAL_MYSQL_ADDRESS = 'tcp://127.0.0.1:3306';
// 代理監聽本地4406端口
$proxy = new Worker('tcp://0.0.0.0:4406');
$proxy->onConnect = function($connection)
{
global $REAL_MYSQL_ADDRESS;
// 異步建立一個到實際mysql服務器的連接
$connection_to_mysql = new AsyncTcpConnection($REAL_MYSQL_ADDRESS);
// mysql連接發來數據時,轉發給對應客戶端的連接
$connection_to_mysql->onMessage = function($connection_to_mysql, $buffer)use($connection)
{
$connection->send($buffer);
};
// mysql連接關閉時,關閉對應的代理到客戶端的連接
$connection_to_mysql->onClose = function($connection_to_mysql)use($connection)
{
$connection->close();
};
// mysql連接上發生錯誤時,關閉對應的代理到客戶端的連接
$connection_to_mysql->onError = function($connection_to_mysql)use($connection)
{
$connection->close();
};
// 執行異步連接
$connection_to_mysql->connect();
// 客戶端發來數據時,轉發給對應的mysql連接
$connection->onMessage = function($connection, $buffer)use($connection_to_mysql)
{
$connection_to_mysql->send($buffer);
};
// 客戶端連接斷開時,斷開對應的mysql連接
$connection->onClose = function($connection)use($connection_to_mysql)
{
$connection_to_mysql->close();
};
// 客戶端連接發生錯誤時,斷開對應的mysql連接
$connection->onError = function($connection)use($connection_to_mysql)
{
$connection_to_mysql->close();
};
};
// 運行worker
Worker::runAll();
```
**測試**
```php
mysql -uroot -P4406 -h127.0.0.1 -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 25004
Server version: 5.5.31-1~dotdeb.0 (Debian)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
```
- 序言
- 原理
- 開發必讀
- 入門指引
- 特性
- 簡單的開發示例
- 安裝
- 環境要求
- 下載安裝
- 啟動停止
- 開發流程
- 開發前必讀
- 目錄結構
- 開發規范
- 基本流程
- 通訊協議
- 通訊協議作用
- 定制通訊協議
- 一些例子
- Worker類
- 構造函數
- 屬性
- id
- count
- name
- protocol
- transport
- reusePort
- connections
- stdoutFile
- pidFile
- logFile
- user
- reloadable
- daemonize
- globalEvent
- 回調屬性
- onWorkerStart
- onWorkerReload
- onConnect
- onMessage
- onClose
- onBufferFull
- onBufferDrain
- onError
- 接口
- runAll
- stopAll
- listen
- TcpConnection類
- 屬性
- id
- protocol
- worker
- maxSendBufferSize
- defaultMaxSendBufferSize
- maxPackageSize
- 回調屬性
- onMessage
- onClose
- onBufferFull
- onBufferDrain
- onError
- 接口
- send
- getRemoteIp
- getRemotePort
- close
- destroy
- pauseRecv
- resumeRecv
- pipe
- AsyncTcpConnection類
- 構造函數
- connect
- reconnect
- transport
- Timer定時器類
- add
- del
- 定時器注意事項
- WebServer
- 調試
- 基本調試
- 查看運行狀態
- 網絡抓包
- 跟蹤系統調用
- 常用組件
- GlobalData數據共享組件
- GlobalDataServer
- GlobalDataClient
- Channel分布式通訊組件
- ChannelServer
- channelClient
- 例子-集群推送
- 例子-分組發送
- FileMonitor文件監控組件
- MySQL組件
- workerman/mysql
- swoole/mysql(異步)
- redis組件
- swoole/redis
- 異步http組件
- swoole/http-client
- 異步消息隊列組件
- react/zmq
- react/stomp
- 異步dns組件
- swoole/dns
- 常見問題
- 心跳
- 客戶端鏈接失敗原因
- 是否支持多線程
- 與其它框架整合
- 運行多個workerman
- 支持哪些協議
- 如何設置進程數
- 查看客戶端連接數
- 對象和資源的持久化
- 例子無法工作
- 啟動失敗
- 停止失敗
- 支持多少并發
- 更改代碼不生效
- 向指定客戶端發送數據
- 如何主動推送消息
- 在其它項目中推送
- 如何實現異步任務
- status里send_fail的原因
- Windows下開發Linux下部署
- 是否支持socket.io
- 終端關閉導致workerman關閉
- 與nginx apache的關系
- 禁用函數檢查
- 平滑重啟原理
- 為Flash開843端口
- 如何廣播數據
- 如何建立udp服務
- 監聽ipv6
- 關閉未認證的鏈接
- 傳輸加密-ssl/tsl
- 創建wss服務
- 創建https服務
- workerman作為客戶端
- 作為ws/wss客戶端
- PHP的幾種回調寫法
- 附錄
- Linux內核調優
- 壓力測試
- 安裝擴展
- websocket協議
- ws協議
- text協議
- frame協議
- 不支持的函數/特性
- 版權信息