connect Method

void AsyncTcpConnection::connect()

Executes an asynchronous connection operation. This method will return immediately.

Note: If you need to set the onError callback for the asynchronous connection, it should be set before executing connect; otherwise, the onError callback may not be triggered. For example, in the case below, the onError callback may not be triggered, making it impossible to capture asynchronous connection failure events.

$connection = new AsyncTcpConnection('tcp://baidu.com:81');
// The onError callback has not been set when executing the connection
$connection->connect();
$connection->onError = function($connection, $err_code, $err_msg)
{
    echo "$err_code, $err_msg";
};

Parameters

No parameters

Return value

No return value

Example MySQL Proxy

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

// The actual mysql address, assuming this is local port 3306
$REAL_MYSQL_ADDRESS = 'tcp://127.0.0.1:3306';

// Proxy listens on local port 4406
$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);
    // Forward data from the mysql connection to the corresponding client connection
    $connection_to_mysql->onMessage = function(AsyncTcpConnection $connection_to_mysql, $buffer) use ($connection)
    {
        $connection->send($buffer);
    };
    // Close the corresponding proxy to the client connection when the mysql connection is closed
    $connection_to_mysql->onClose = function(AsyncTcpConnection $connection_to_mysql) use ($connection)
    {
        $connection->close();
    };
    // Close the corresponding proxy to the client connection when there is an error on the mysql connection
    $connection_to_mysql->onError = function(AsyncTcpConnection $connection_to_mysql) use ($connection)
    {
        $connection->close();
    };
    // Execute asynchronous connection
    $connection_to_mysql->connect();

    // Forward data from the client to the corresponding mysql connection
    $connection->onMessage = function(TcpConnection $connection, $buffer) use ($connection_to_mysql)
    {
        $connection_to_mysql->send($buffer);
    };
    // Disconnect the corresponding mysql connection when the client connection is closed
    $connection->onClose = function(TcpConnection $connection) use ($connection_to_mysql)
    {
        $connection_to_mysql->close();
    };
    // Disconnect the corresponding mysql connection when there is an error on the client connection
    $connection->onError = function(TcpConnection $connection) use ($connection_to_mysql)
    {
        $connection_to_mysql->close();
    };

};
// Run the worker
Worker::runAll();

Test

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>