client
tcp_client.php
复制代码
- <?php
-
- $client = new swoole_client(SWOOLE_SOCK_TCP);
- if(!$client->connect("127.0.0.1", 9501)) {
- echo "连接失败";
- exit;
- }
-
- fwrite(STDOUT, "请输入消息:");
- $msg = trim(fgets(STDIN));
-
- $client->send($msg);
-
- $result = $client->recv();
- echo $result;
coroutine
redis.php
复制代码
- <?php
- $http = new swoole_http_server('0.0.0.0', 8001);
- $http->on('request', function($request, $response) {
-
- $redis = new Swoole\Coroutine\Redis();
- $redis->connect('127.0.0.1', 6379);
- $value = $redis->get($request->get['a']);
- $response->header("Content-Type", "text/plain");
- $response->end($value);
- });
- $http->start();
io
1.log
复制代码
- 20180306 18:08:4320180306 18:10:0720180306 18:10:0920180306 18:10:0920180306 18:10:39
- 20180306 18:10:40
1.txt
复制代码
- test-io
mysql.php
复制代码
- <?php
- class AysMysql {
-
-
- public $dbSource = "";
-
-
- public $dbConfig = [];
- public function __construct() {
-
- $this->dbSource = new Swoole\Mysql;
- $this->dbConfig = [
- 'host' => '127.0.0.1',
- 'port' => 5123,
- 'user' => 'root',
- 'password' => 123456,
- 'database' => 'swoole',
- 'charset' => 'utf8',
- ];
- }
- public function update() {
- }
- public function add() {
- }
-
-
- public function execute($id, $username) {
-
- $this->dbSource->connect($this->dbConfig, function($db, $result) use($id, $username) {
- echo "mysql-connect".PHP_EOL;
- if($result === false) {
- var_dump($db->connect_error);
-
- }
-
- $sql = "update test set `username` = '".$username."' where id=".$id;
-
-
- $db->query($sql, function($db, $result){
-
- if($result === false) {
-
- var_dump($db->error);
- }elseif($result === true) {
-
- var_dump($db->affected_rows);
- }else {
- print_r($result);
- }
- $db->close();
- });
- });
- return true;
- }
- }
- $obj = new AysMysql();
- $flag = $obj->execute(1, 'singwa-111112');
- var_dump($flag).PHP_EOL;
- echo "start".PHP_EOL;
- for($i=0; $i<900000;$i++) {
- echo $i.PHP_EOL;
- }
-
read.php
复制代码
- <?php
-
- $result = Swoole\Async::readfile(__DIR__."/1.txt", function($filename, $fileContent) {
- echo "filename:".$filename.PHP_EOL;
- echo "content:".$fileContent.PHP_EOL;
- });
- var_dump($result);
- echo "start".PHP_EOL;
redis.php
复制代码
- <?php
- $redisClient = new swoole_redis;
- $redisClient->connect('127.0.0.1', 6379, function(swoole_redis $redisClient, $result) {
- echo "connect".PHP_EOL;
- var_dump($result);
-
-
-
-
-
- $redisClient->keys('*gw*', function(swoole_redis $redisClient, $result) {
- var_dump($result);
- $redisClient->close();
- });
- });
- echo "start".PHP_EOL;
write.php
复制代码
- <?php
- $content = date("Ymd H:i:s").PHP_EOL;
- swoole_async_writefile(__DIR__."/1.log", $content, function($filename){
-
- echo "success".PHP_EOL;
- }, FILE_APPEND);
-
- echo "start".PHP_EOL;
memory
table.php
复制代码
- <?php
-
- $table = new swoole_table(1024);
-
- $table->column('id', $table::TYPE_INT, 4);
- $table->column('name', $table::TYPE_STRING, 64);
- $table->column('age', $table::TYPE_INT, 3);
- $table->create();
- $table->set('singwa_imooc', ['id' => 1, 'name'=> 'singwa', 'age' => 30]);
-
- $table['singwa_imooc_2'] = [
- 'id' => 2,
- 'name' => 'singwa2',
- 'age' => 31,
- ];
- $table->decr('singwa_imooc_2', 'age', 2);
- print_r($table['singwa_imooc_2']);
- echo "delete start:".PHP_EOL;
- $table->del('singwa_imooc_2');
- print_r($table['singwa_imooc_2']);
process
curl.php
复制代码
- <?php
- echo "process-start-time:".date("Ymd H:i:s");
- $workers = [];
- $urls = [
- 'http://baidu.com',
- 'http://sina.com.cn',
- 'http://qq.com',
- 'http://baidu.com?search=singwa',
- 'http://baidu.com?search=singwa2',
- 'http://baidu.com?search=imooc',
- ];
- for($i = 0; $i < 6; $i++) {
-
- $process = new swoole_process(function(swoole_process $worker) use($i, $urls) {
-
- $content = curlData($urls[$i]);
-
- $worker->write($content.PHP_EOL);
- }, true);
- $pid = $process->start();
- $workers[$pid] = $process;
- }
- foreach($workers as $process) {
- echo $process->read();
- }
-
- function curlData($url) {
-
- sleep(1);
- return $url . "success".PHP_EOL;
- }
- echo "process-end-time:".date("Ymd H:i:s");
process.php
复制代码
- <?php
- $process = new swoole_process(function(swoole_process $pro) {
-
-
- $pro->exec("/home/work/study/soft/php/bin/php", [__DIR__.'/../server/http_server.php']);
- }, false);
- $pid = $process->start();
- echo $pid . PHP_EOL;
- swoole_process::wait();
server
access.log
http_server.php
复制代码
- <?php
- $http = new swoole_http_server("0.0.0.0", 8811);
- $http->set(
- [
- 'enable_static_handler' => true,
- 'document_root' => "/home/work/hdtocs/swoole_mooc/data",
- ]
- );
- $http->on('request', function($request, $response) {
-
- $content = [
- 'date:' => date("Ymd H:i:s"),
- 'get:' => $request->get,
- 'post:' => $request->post,
- 'header:' => $request->header,
- ];
- swoole_async_writefile(__DIR__."/access.log", json_encode($content).PHP_EOL, function($filename){
-
- }, FILE_APPEND);
- $response->cookie("singwa", "xsssss", time() + 1800);
- $response->end("sss". json_encode($request->get));
- });
- $http->start();
tcp.php
复制代码
- <?php
-
- $serv = new swoole_server("127.0.0.1", 9501);
- $serv->set([
- 'worker_num' => 6 ,
- 'max_request' => 10000,
- ]);
-
-
- $serv->on('connect', function ($serv, $fd, $reactor_id) {
- echo "Client: {$reactor_id} - {$fd}-Connect.\n";
- });
-
- $serv->on('receive', function ($serv, $fd, $reactor_id, $data) {
- $serv->send($fd, "Server: {$reactor_id} - {$fd}".$data);
- });
-
- $serv->on('close', function ($serv, $fd) {
- echo "Client: Close.\n";
- });
-
- $serv->start();
ws.php
复制代码
- <?php
- class Ws {
- CONST HOST = "0.0.0.0";
- CONST PORT = 8812;
- public $ws = null;
- public function __construct() {
- $this->ws = new swoole_websocket_server("0.0.0.0", 8812);
- $this->ws->set(
- [
- 'worker_num' => 2,
- 'task_worker_num' => 2,
- ]
- );
- $this->ws->on("open", [$this, 'onOpen']);
- $this->ws->on("message", [$this, 'onMessage']);
- $this->ws->on("task", [$this, 'onTask']);
- $this->ws->on("finish", [$this, 'onFinish']);
- $this->ws->on("close", [$this, 'onClose']);
- $this->ws->start();
- }
-
-
- public function onOpen($ws, $request) {
- var_dump($request->fd);
- if($request->fd == 1) {
-
- swoole_timer_tick(2000, function($timer_id){
- echo "2s: timerId:{$timer_id}\n";
- });
- }
- }
-
-
- public function onMessage($ws, $frame) {
- echo "ser-push-message:{$frame->data}\n";
-
- $data = [
- 'task' => 1,
- 'fd' => $frame->fd,
- ];
-
- swoole_timer_after(5000, function() use($ws, $frame) {
- echo "5s-after\n";
- $ws->push($frame->fd, "server-time-after:");
- });
- $ws->push($frame->fd, "server-push:".date("Y-m-d H:i:s"));
- }
-
-
- public function onTask($serv, $taskId, $workerId, $data) {
- print_r($data);
-
- sleep(10);
- return "on task finish";
- }
-
-
- public function onFinish($serv, $taskId, $data) {
- echo "taskId:{$taskId}\n";
- echo "finish-data-sucess:{$data}\n";
- }
-
-
- public function onClose($ws, $fd) {
- echo "clientid:{$fd}\n";
- }
- }
- $obj = new Ws();
ws_server.php
复制代码
- <?php
- $server = new swoole_websocket_server("0.0.0.0", 8812);
-
- $server->set(
- [
- 'enable_static_handler' => true,
- 'document_root' => "/home/work/hdtocs/swoole_mooc/data",
- ]
- );
-
- $server->on('open', 'onOpen');
- function onOpen($server, $request) {
- print_r($request->fd);
- }
-
- $server->on('message', function (swoole_websocket_server $server, $frame) {
- echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
- $server->push($frame->fd, "singwa-push-secesss");
- });
- $server->on('close', function ($ser, $fd) {
- echo "client {$fd} closed\n";
- });
- $server->start();
目录
复制代码
- └── client
- ├── tcp_client.php
- └── coroutine
- ├── redis.php
- └── io
- ├── 1.log
- ├── 1.txt
- ├── mysql.php
- ├── read.php
- ├── redis.php
- ├── write.php
- └── memory
- ├── table.php
- └── process
- ├── curl.php
- ├── process.php
- └── server
- ├── access.log
- ├── http_server.php
- ├── tcp.php
- ├── ws.php
- ├── ws_server.php
评论已关闭