PHP code example of tourze / workerman-dns-client
1. Go to this page and download the library: Download tourze/workerman-dns-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tourze / workerman-dns-client example snippets
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Tourze\Workerman\DnsClient\DnsAsyncTcpConnection;
use Workerman\Worker;
he(new ArrayAdapter());
// 创建连接
$connection = new DnsAsyncTcpConnection('ws://example.com:8080');
// 设置DNS服务器 (可选)
$connection->setDnsServer('1.1.1.1', 53);
// 连接事件
$connection->onConnect = function($connection) {
echo "连接成功\n";
$connection->send('hello');
};
$connection->onMessage = function($connection, $data) {
echo "收到消息: $data\n";
};
$connection->onError = function($connection, $code, $msg) {
echo "错误: $code $msg\n";
};
$connection->onClose = function($connection) {
echo "连接关闭\n";
};
// 建立连接
$connection->connect();
};
Worker::runAll();
$connection = new DnsAsyncTcpConnection('ws://192.168.1.1:8080');
$connection->forceDnsLookup(true);
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
// 使用文件缓存
$cache = new FilesystemAdapter('dns', 300, '/tmp/dns-cache');
DnsAsyncTcpConnection::initDnsCache($cache);