__construct Method
void AsyncUdpConnection::__construct(string $remote_address)
Creates a UDP connection object.
AsyncUdpConnection allows Workerman to act as a client to transmit UDP data to a remote server.
Parameters
Parameter: remote_address
The address to connect to, for example:
udp://192.168.1.1:1234
frame://192.168.1.1:8080
text://192.168.1.1:8080
Example
use Workerman\Worker;
use Workerman\Connection\AsyncUdpConnection;
use Workerman\Timer;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('udp://0.0.0.0:1234');
$worker->onWorkerStart = function(){
// Start a UDP client after 1 second, connect to port 1234 and send the string hi
Timer::add(1, function(){
$udp_connection = new AsyncUdpConnection('udp://127.0.0.1:1234');
$udp_connection->onConnect = function($udp_connection){
$udp_connection->send('hi');
};
$udp_connection->onMessage = function($udp_connection, $data){
// Received data hello returned from the server
echo "recv $data\r\n";
// Close the connection
$udp_connection->close();
};
$udp_connection->connect();
}, null, false);
};
$worker->onMessage = function($connection, $data)
{
// Received data from AsyncUdpConnection client, return the string hello
$connection->send("hello");
};
Worker::runAll();
After execution, it will print something like:
recv hello