{"id":542,"date":"2021-05-30T03:45:27","date_gmt":"2021-05-30T03:45:27","guid":{"rendered":"http:\/\/robinluo.top\/?p=542"},"modified":"2021-05-30T03:45:27","modified_gmt":"2021-05-30T03:45:27","slug":"php-reactive-socket","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=542","title":{"rendered":"php reactive socket"},"content":{"rendered":"\n<p>php socket \u7b2c\u4e09\u65b9\u5e93 composer require react\/socket<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8be5\u5e93\u9488\u5bf9 socket \/ \u6d41 \u505a\u4e8b\u4ef6\u76d1\u542c\uff08\u5e94\u8be5\u662fepoll \uff09\u4e3b\u7ebf\u7a0b\u505a\u4e8b\u4ef6\u5faa\u73af \uff08reactive\uff09\u6240\u4ee5\u8be5reactive \u6846\u67b6\u8c8c\u4f3c\u53ea\u80fd\u5728 linux\u4e0b\u4f7f\u7528<\/p>\n\n\n\n<p>server<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\nrequire '..\/vendor\/autoload.php';\r\n\r\necho \"enter script\\n\";\r\n\r\n$loop = React\\EventLoop\\Factory::create();\r\n$socket = new React\\Socket\\Server('127.0.0.1:8080', $loop);\r\n\r\n$socket->on('connection', function (React\\Socket\\ConnectionInterface $connection) {\r\n    echo \"\" . $connection->getRemoteAddress() . \"client connected\\n\";\r\n    $connection->write(\"Hello \" . $connection->getRemoteAddress() . \"!\\n\");\r\n    $connection->write(\"Welcome to this amazing server!\\n\");\r\n    $connection->write(\"Here's a tip: don't say anything.\\n\");\r\n\r\n    $connection->on('data', function ($data) use ($connection) {\r\n        echo \"\" . $data . \"\\n\";\r\n        \/\/$connection->close();\r\n    });\r\n});\r\n\r\necho \"server started at 8080\\n\";\r\n\r\n$loop->run();<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d$loop \u5c31\u662f\u4e8b\u4ef6\u5faa\u73af\u5bf9\u8c61<\/p>\n\n\n\n<p>client<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nrequire '..\/vendor\/autoload.php';\r\n\r\n\r\n$loop = React\\EventLoop\\Factory::create();\r\n$connector = new React\\Socket\\Connector($loop);\r\n\r\n$connector->connect('127.0.0.1:8080')->then(function (React\\Socket\\ConnectionInterface $connection) use ($loop) {\r\n    echo \"client connected\\n\";\r\n    $connection->write(\"Hello World!\\n\");\r\n    $connection->on('data', function ($data) use ($loop, $connection) {\r\n        echo \"\" . $data . \"\\n\";\r\n        \/\/$connection->close();\r\n        \/\/$output = new React\\Stream\\WritableResourceStream(fopen(\".\/client.txt\", \"w\"),$loop);\r\n        \/\/$output = fopen(\".\/client.txt\", \"w\");\r\n        \/\/fwrite($output,$data);\r\n        \/\/$output->write(\"\" . $data . \"\\n\");\r\n        while($input = fgets(STDIN, 10))\r\n        {\r\n            echo $input.\"\\n\";\r\n            $connection->write($input);\r\n\r\n        }\r\n    });\r\n    \/*\r\n    $input = new React\\Stream\\ReadableResourceStream(STDIN,$loop);\r\n    $input->on(\"data\",function ($data) use ($connection) {\r\n       $connection->write($data);\r\n    });*\/\r\n    \/\/$output = fopen(\".\/client.txt\", \"w\");\r\n    \/\/$steam = new React\\Stream\\WritableResourceStream($output,$loop);\r\n    \/\/$connection->pipe($steam);\r\n\r\n\r\n\r\n});\r\n\r\n$loop->run();\r\n\r\n<\/code><\/pre>\n\n\n\n<p>\u6700\u4eae\u773c\u7684\u662f \u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08\u6807\u51c6\u8f93\u51fa\u6d41 \u6587\u4ef6\u8f93\u51fa\u6d41\uff09fd \u5305\u88c5\u6210\u4e00\u4e2a\u54cd\u5e94\u5f0f\u6d41\u5bf9\u8c61 \u901a\u8fc7pipe\u5b9e\u73b0\u7ba1\u9053\u529f\u80fd \u800c\u4e14\u751f\u547d\u5468\u671f\u76f8\u540c \u4f1a\u540c\u65f6\u5173\u95ed\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php socket \u7b2c\u4e09\u65b9\u5e93 composer require react\/socket \u8be5\u5e93\u9488\u5bf9 socket \/ \u6d41 \u505a\u4e8b\u4ef6\u76d1\u542c\uff08\u5e94\u8be5\u662fepoll \uff09\u4e3b\u7ebf\u7a0b\u505a\u4e8b\u4ef6\u5faa\u73af \uff08reactive\uff09\u6240\u4ee5\u8be5reactive \u6846\u67b6\u8c8c\u4f3c\u53ea\u80fd\u5728 linux\u4e0b\u4f7f\u7528 server \u5176\u4e2d$loop \u5c31\u662f\u4e8b\u4ef6\u5faa\u73af\u5bf9\u8c61 client \u6700\u4eae\u773c\u7684\u662f \u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08\u6807\u51c6\u8f93\u51fa\u6d41 \u6587\u4ef6\u8f93\u51fa\u6d41\uff09fd \u5305\u88c5\u6210\u4e00\u4e2a\u54cd\u5e94\u5f0f\u6d41\u5bf9\u8c61 \u901a\u8fc7pipe\u5b9e\u73b0\u7ba1\u9053\u529f\u80fd \u800c\u4e14\u751f\u547d\u5468\u671f\u76f8\u540c \u4f1a\u540c\u65f6\u5173\u95ed\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[86,81],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/542"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=542"}],"version-history":[{"count":2,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions"}],"predecessor-version":[{"id":544,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/542\/revisions\/544"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}