PHP's asynchronous & concurrent & distributed networking framework.
- Event-driven
- Full asynchronous non-blocking
- Multi-Thread or Multi-Process
- Millisecond timer
- Asynchronous MySQL
- AsyncTask workers
pecl install swoole
Server
$serv = new swoole_server("127.0.0.1", 9501);
$serv->on('connect', function ($serv, $fd){
echo "Client:Connect.\n";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
$serv->send($fd, 'Swoole: '.$data);
$serv->close($fd);
});
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
$serv->start();
Client
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
$cli->send("hello world\n");
});
$client->on("receive", function($cli){
$data = $cli->recv();
echo "Receive: $data\n";
});
$client->on("error", function($cli){
echo "connect fail\n";
});
$client->on("close", function($cli){
echo "close\n";
$cli->close();
});
$client->connect('127.0.0.1', 9501, 0.5);
Event
$fp = stream_socket_client("tcp://127.0.0.1:9501", $errno, $errstr, 30);
if (!$fp) {
exit("$errstr ($errno)\n");
}
fwrite($fp, "HELLO world");
swoole_event_add($fp, function($fp){
echo fgets($fp, 1024);
swoole_event_del($fp);
fclose($fp);
});
Task
$serv = new swoole_server("127.0.0.1", 9502);
$serv->on('Receive', function($serv, $fd, $from_id, $data) {
$task_id = $serv->task("Async");
echo "Dispath AsyncTask: id=$task_id\n";
});
$serv->on('Task', function ($serv, $task_id, $from_id, $data) {
echo "New AsyncTask[id=$task_id]".PHP_EOL;
$serv->finish("$data -> OK");
});
$serv->on('Finish', function ($serv, $task_id, $data) {
echo "AsyncTask[$task_id] Finish: $data".PHP_EOL;
});
$serv->start();
Files
- PHP: examples/server.php
- C/C++: examples/server.c
- Client: examples/client.php
- Google Group: https://groups.google.com/forum/#!forum/swoole
- Email: [email protected]
cd swoole/
phpize
./configure
make && make install
cd swoole/
cmake .
make && make install
https://github.com/matyhtf/swoole_framework
- Document 中文
- Document English. Wait moment.
Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.html