![]() |
VOOZH | about |
Build high-performance, scalable, concurrent TCP, UDP, Unix Socket, HTTP, WebSocket services with PHP and easy to use coroutine, fibers API.
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
run(function() {
sleep(1);
});
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
run(function() {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->keys('*');
var_dump($result);
});
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
run(function() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://httpbin.org/get');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
});
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
run(function () {
go(function () {
$pdo = new PDO('mysql:host=10.66.110.163;dbname=coding_test;charset=utf8', 'coding_test', 'coding_test');
$statement = $pdo->prepare('SELECT * FROM `coding_test`');
$statement->execute();
var_dump($statement->fetchAll());
});
go(function () {
$mysqli = new mysqli('10.66.110.163', 'coding_test', 'coding_test', 'coding_test');
$statement = $mysqli->prepare('SELECT `id` FROM `coding_test`');
$statement->bind_result($id);
$statement->execute();
$statement->fetch();
var_dump($id);
});
});
use Swoole\Coroutine;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
run(function() {
$result = [];
Coroutine::join([
go(function () use (&$result) {
$result['baidu'] = file_get_contents("https://www.baidu.com/");
}),
go(function () use (&$result) {
$result['taobao'] = file_get_contents("https://www.taobao.com/");
})
]);
echo "all done\n";
});
$http = new Swoole\Http\Server('127.0.0.1', 9501);
$http->on('start', function ($server) {
echo "Swoole http server is started at http://127.0.0.1:9501\n";
});
$http->on('request', function ($request, $response) {
$response->header('Content-Type', 'text/plain');
$response->end('Hello World');
});
$http->start();
$server = new Swoole\Server('127.0.0.1', 9503);
$server->on('start', function ($server) {
echo "TCP Server is started at tcp://127.0.0.1:9503\n";
});
$server->on('connect', function ($server, $fd){
echo "connection open: {$fd}\n";
});
$server->on('receive', function ($server, $fd, $reactor_id, $data) {
$server->send($fd, "Swoole: {$data}");
});
$server->on('close', function ($server, $fd) {
echo "connection close: {$fd}\n";
});
$server->start();
$server = new Swoole\Server('127.0.0.1', 9504, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
$server->on('start', function ($server) {
echo "UDP Server is started at udp://127.0.0.1:9504\n";
});
$server->on('packet', function ($server, $data, $clientInfo) {
$server->sendTo($clientInfo['address'], $clientInfo['port'], "Server:{$data}");
});
$server->start();
$server = new Swoole\Websocket\Server('127.0.0.1', 9502);
$server->on('start', function ($server) {
echo "Websocket Server is started at ws://127.0.0.1:9502\n";
});
$server->on('open', function($server, $req) {
echo "connection open: {$req->fd}\n";
});
$server->on('message', function($server, $frame) {
echo "received message: {$frame->data}\n";
$server->push($frame->fd, json_encode(['hello', 'world']));
});
$server->on('close', function($server, $fd) {
echo "connection close: {$fd}\n";
});
$server->start();
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
run(function() {
$id = go(function(){
$id = Co::getUid();
echo "start coro $id\n";
Co::suspend($id);
echo "resume coro $id @1\n";
Co::suspend($id);
echo "resume coro $id @2\n";
});
echo "start to resume $id @1\n";
Co::resume($id);
echo "start to resume $id @2\n";
Co::resume($id);
echo "main\n";
});
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
run(function(){
$channel = new Channel(1);
go(function () use ($channel) {
for($i = 0; $i < 10; $i++) {
Coroutine::sleep(1.0);
$channel->push(['rand' => rand(1000, 9999), 'index' => $i]);
echo "{$i}\n";
}
});
go(function () use ($channel) {
while(true) {
$data = $channel->pop(2.0);
if ($data) {
var_dump($data);
} else {
assert($channel->errCode === SWOOLE_CHANNEL_TIMEOUT);
break;
}
}
});
});
use Swoole\Coroutine\Socket;
use function Swoole\Coroutine\run;
use function Swoole\Coroutine\go;
run(function() {
go(function () {
$socket = new Socket(AF_INET, SOCK_DGRAM, 0);
$socket->bind('127.0.0.1', 9503);
$client_map = [];
for ($c = 128; $c--;) {
for ($n = 0; $n < 100; $n++) {
$recv = $socket->recvfrom($peer);
$client_uid = "{$peer['address']}:{$peer['port']}";
$id = $client_map[$client_uid] = ($client_map[$client_uid] ?? -1) + 1;
assert($recv === "Client: Hello #{$id}!");
$socket->sendto($peer['address'], $peer['port'], "Server: Hello #{$id}!");
}
}
$socket->close();
});
for ($c = 128; $c--;) {
go(function () {
$fp = stream_socket_client('udp://127.0.0.1:9503', $errno, $errstr, 1);
if (!$fp) {
echo "{$errstr} ({$errno})\n";
return;
}
for ($n = 0; $n < 100; $n++) {
fwrite($fp, "Client: Hello #{$n}!");
$recv = fread($fp, 1024);
list($address, $port) = explode(':', (stream_socket_get_name($fp, true)));
assert($address === '127.0.0.1' && (int)$port === 9503);
assert($recv === "Server: Hello #{$n}!");
}
fclose($fp);
});
}
echo 'Done', PHP_EOL;
});
use function Swoole\Coroutine\run;
run(function(){
$fp1 = stream_socket_client('tcp://www.baidu.com:80', $errno, $errstr, 30);
$fp2 = stream_socket_client('tcp://www.qq.com:80', $errno, $errstr, 30);
if (!$fp1) {
echo "$errstr ($errno) \n";
} else {
fwrite($fp1, "GET / HTTP/1.0\r\nHost: www.baidu.com\r\nUser-Agent: curl/7.58.0\r\nAccept: */*\r\n\r\n");
$r_array = [$fp1, $fp2];
$w_array = $e_array = null;
$n = stream_select($r_array, $w_array, $e_array, 10);
$html = '';
while (!feof($fp1)) {
$html .= fgets($fp1, 1024);
}
fclose($fp1);
echo strlen($html);
}
});
$server = new Swoole\Server('127.0.0.1', 9503);
$server->set(['task_worker_num' => 4]);
$server->on('start', function ($server) {
echo "TCP Server is started at tcp://127.0.0.1:9503\n";
});
$server->on('receive', function($server, $fd, $reactor_id, $data) {
$task_id = $server->task('Async');
$server->send($fd, "Dispatch AsyncTask: [id={$task_id}]\n");
});
$server->on('task', function ($server, $task_id, $reactor_id, $data) {
echo "New AsyncTask[id={$task_id}]\n";
$server->finish("{$data} -> OK");
});
$server->on('finish', function ($server, $task_id, $data) {
echo "AsyncTask[{$task_id}] finished: {$data}\n";
});
$server->start();
If you have excellent projects based on Swoole and want to show them on the official website, please scan WeChat QR-Code and contact us
Swoole 使用 C/C++ 语言编写,提供了 PHP 语言的异步多线程服务器、异步 TCP/UDP 网络客户端、异步 MySQL、异步 Redis、数据库连接池、AsyncTask、消息队列、毫秒定时器、异步文件读写、异步 DNS 查询。 Swoole 内置了 Http/WebSocket 服务器端/客户端、Http2.0 服务器端。
除了异步 IO 的支持之外,Swoole 为 PHP 多进程的模式设计了多个并发数据结构和 IPC 通信机制,可以大大 简化多进程并发编程的工作。其中包括了原子计数器、Table、Channel、Lock、进程间通信 IPC 等丰富的功能特性。
Swoole4.0 支持了类似 Go 语言的协程,可以使用完全同步的代码实现异步程序。PHP 代码无需额外增加任何 关键词,底层自动进行协程调度,实现异步 IO。