Swoole主要功能: PHP語言的異步、並行、高性能網絡通訊框架,使用純C語言編寫,提供了PHP語言的異步多線程服務器,異步TCP/UDP網絡客戶端,異步MySQL,數據庫鏈接池,AsyncTask,消息隊列,毫秒定時器,異步文件讀寫,異步DNS查詢。php
$serv = new swoole_websocket_server("127.0.0.1", 9502); $serv->on('Open', function($server, $req) { echo "connection open: ".$req->fd; }); $serv->on('Message', function($server, $frame) { echo "message: ".$frame->data; $server->push($frame->fd, json_encode(["hello", "world"])); }); $serv->on('Close', function($server, $fd) { echo "connection close: ".$fd; }); $serv->start();
$serv = new swoole_server("127.0.0.1", 9501); $serv->set(array( 'worker_num' => 8, //工做進程數量 'daemonize' => true, //是否做爲守護進程 )); $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 = 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){ echo "Received: ".$data."\n"; }); $client->on("error", function($cli){ echo "Connect failed\n"; }); $client->on("close", function($cli){ echo "Connection close\n"; }); //發起網絡鏈接 $client->connect('127.0.0.1', 9501, 0.5);
本身工做中用到的就這三個,具體的配置和更多的功能,能夠訪問swoole網站: http://www.swoole.com/
web
註明: Apache 配置swoole的時候,記得在兩個配置文件php.ini 中,加入 swoole.so 不然會致使部分訪問無效
數據庫