phương thức connect

void AsyncTcpConnection::connect()

Thực hiện thao tác kết nối bất đồng bộ. Phương thức này sẽ ngay lập tức trả về.

Lưu ý: Nếu cần đặt callback onError cho kết nối bất đồng bộ, bạn nên thiết lập nó trước khi gọi connect, nếu không callback onError có thể không được kích hoạt, ví dụ như trong trường hợp dưới đây, callback onError có thể không được kích hoạt, không thể bắt sự kiện kết nối thất bại.

$connection = new AsyncTcpConnection('tcp://baidu.com:81');
// Trong khi thực hiện kết nối, vẫn chưa thiết lập callback onError
$connection->connect();
$connection->onError = function($connection, $err_code, $err_msg)
{
    echo "$err_code, $err_msg";
};

Tham số

Không có tham số

Giá trị trả về

Không có giá trị trả về

Ví dụ Proxy Mysql

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

// Địa chỉ mysql thực tế, giả sử đây là cổng 3306 trên máy local
$REAL_MYSQL_ADDRESS = 'tcp://127.0.0.1:3306';

// Proxy lắng nghe cổng 4406 trên máy local
$proxy = new Worker('tcp://0.0.0.0:4406');

$proxy->onConnect = function(TcpConnection $connection)
{
    global $REAL_MYSQL_ADDRESS;
    // Asynchronously establish a connection to the actual MySQL server
    $connection_to_mysql = new AsyncTcpConnection($REAL_MYSQL_ADDRESS);
    // Khi kết nối mysql gửi dữ liệu, chuyển tiếp tới kết nối của client tương ứng
    $connection_to_mysql->onMessage = function(AsyncTcpConnection $connection_to_mysql, $buffer)use($connection)
    {
        $connection->send($buffer);
    };
    // Khi kết nối mysql đóng, đóng kết nối proxy tới client tương ứng
    $connection_to_mysql->onClose = function(AsyncTcpConnection $connection_to_mysql)use($connection)
    {
        $connection->close();
    };
    // Khi có lỗi xảy ra trên kết nối mysql, đóng kết nối proxy tới client tương ứng
    $connection_to_mysql->onError = function(AsyncTcpConnection $connection_to_mysql)use($connection)
    {
        $connection->close();
    };
    // Thực hiện kết nối bất đồng bộ
    $connection_to_mysql->connect();

    // Khi client gửi dữ liệu, chuyển tiếp tới kết nối mysql tương ứng
    $connection->onMessage = function(TcpConnection $connection, $buffer)use($connection_to_mysql)
    {
        $connection_to_mysql->send($buffer);
    };
    // Khi kết nối client ngắt, ngắt kết nối mysql tương ứng
    $connection->onClose = function(TcpConnection $connection)use($connection_to_mysql)
    {
        $connection_to_mysql->close();
    };
    // Khi có lỗi xảy ra trên kết nối client, ngắt kết nối mysql tương ứng
    $connection->onError = function(TcpConnection $connection)use($connection_to_mysql)
    {
        $connection_to_mysql->close();
    };

};
// Chạy worker
Worker::runAll();

Kiểm tra

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>