{"id":67061,"date":"2026-01-28T07:25:16","date_gmt":"2026-01-27T23:25:16","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/67061.html"},"modified":"2026-01-28T07:25:16","modified_gmt":"2026-01-27T23:25:16","slug":"cosethook_flags-%ef%bc%9e-swoole_hook_tcp%ef%bc%9b%e7%9a%84%e5%ba%96%e4%b8%81%e8%a7%a3%e7%89%9b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/67061.html","title":{"rendered":"Co::set([\u2018hook_flags\u2018 =\uff1e SWOOLE_HOOK_TCP])\uff1b\u7684\u5e96\u4e01\u89e3\u725b"},"content":{"rendered":"<p>Co::set([&#039;hook_flags&#039; &#061;&gt; SWOOLE_HOOK_TCP]); \u662f Swoole \u534f\u7a0b\u8fd0\u884c\u65f6\u7684\u5173\u952e\u914d\u7f6e\u6307\u4ee4&#xff0c;\u7528\u4e8e\u542f\u7528\u5bf9 TCP \u76f8\u5173\u51fd\u6570\u7684\u534f\u7a0b Hook&#xff08;\u5373\u81ea\u52a8\u534f\u7a0b\u5316&#xff09;\u3002\u5b83\u51b3\u5b9a\u4e86\u54ea\u4e9b\u963b\u585e I\/O \u64cd\u4f5c\u4f1a\u88ab\u900f\u660e\u5730\u66ff\u6362\u4e3a\u975e\u963b\u585e\u534f\u7a0b\u8c03\u5ea6&#xff0c;\u662f\u5b9e\u73b0\u9ad8\u6027\u80fd\u5e76\u53d1\u7684\u6838\u5fc3\u5f00\u5173\u3002<\/p>\n<hr \/>\n<h4>\u4e00\u3001\u6838\u5fc3\u539f\u7406&#xff1a;\u4ec0\u4e48\u662f Hook&#xff1f;<\/h4>\n<h5>\u25b6 1. Hook \u7684\u672c\u8d28<\/h5>\n<ul>\n<li>\u76ee\u7684&#xff1a;\u5c06 \u963b\u585e\u5f0f I\/O \u51fd\u6570&#xff08;\u5982 fsockopen, stream_socket_client&#xff09;<br \/>\n\u2192 \u81ea\u52a8\u66ff\u6362\u4e3a \u975e\u963b\u585e\u534f\u7a0b\u7248\u672c<\/li>\n<li>\u6548\u679c&#xff1a;\n<ul>\n<li>\u5f00\u53d1\u8005\u5199\u540c\u6b65\u4ee3\u7801 \u2192 Swoole \u81ea\u52a8\u8f6c\u4e3a\u5f02\u6b65\u6267\u884c<\/li>\n<li>\u65e0\u9700\u624b\u52a8\u4f7f\u7528 Swoole\\\\Coroutine\\\\Client<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>\u25b6 2. SWOOLE_HOOK_TCP \u7684\u4f5c\u7528\u8303\u56f4<\/h5>\n<ul>\n<li>\u8986\u76d6\u51fd\u6570&#xff1a;<span class=\"token function\">fsockopen<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">stream_socket_client<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token function\">stream_socket_server<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">stream_select<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n<span class=\"token function\">fread<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">fwrite<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">fgets<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">file_get_contents<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>&#xff08;\u5f53\u7528\u4e8e\u7f51\u7edc\u6d41&#xff09;<\/p>\n<\/li>\n<li>\u4e0d\u8986\u76d6&#xff1a;\n<ul>\n<li>\u672c\u5730\u6587\u4ef6\u64cd\u4f5c&#xff08;\u9700 SWOOLE_HOOK_FILE&#xff09;<\/li>\n<li>cURL&#xff08;\u9700 SWOOLE_HOOK_CURL&#xff09;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&#x1f4a1; \u6838\u5fc3\u8ba4\u77e5&#xff1a;<br \/>\nHook &#061; \u540c\u6b65\u4ee3\u7801\u7684\u201c\u534f\u7a0b\u7ffb\u8bd1\u5668\u201d<\/p>\n<hr \/>\n<h4>\u4e8c\u3001\u5e95\u5c42\u673a\u5236&#xff1a;\u5982\u4f55\u5b9e\u73b0\u900f\u660e\u66ff\u6362&#xff1f;<\/h4>\n<h5>\u25b6 1. \u51fd\u6570\u8868\u52ab\u6301&#xff08;Function Table Hooking&#xff09;<\/h5>\n<ul>\n<li>Swoole \u542f\u52a8\u65f6&#xff1a;\n<ul>\n<li>\u5907\u4efd\u539f\u59cb\u51fd\u6570\u6307\u9488&#xff08;\u5982 fsockopen&#xff09;<\/li>\n<li>\u5c06 PHP \u5185\u90e8\u51fd\u6570\u8868\u6307\u5411 Swoole \u7684\u534f\u7a0b\u5305\u88c5\u51fd\u6570<\/li>\n<\/ul>\n<\/li>\n<li>\u8c03\u7528\u65f6&#xff1a;EventLoop\u7cfb\u7edf\u5185\u6838Swoole Hook\u7528\u6237\u4ee3\u7801EventLoop\u7cfb\u7edf\u5185\u6838Swoole Hook\u7528\u6237\u4ee3\u7801#mermaid-svg-zDvp0fY1I0VvI7JZ{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-zDvp0fY1I0VvI7JZ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-zDvp0fY1I0VvI7JZ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-zDvp0fY1I0VvI7JZ .error-icon{fill:#552222;}#mermaid-svg-zDvp0fY1I0VvI7JZ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-zDvp0fY1I0VvI7JZ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-zDvp0fY1I0VvI7JZ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-zDvp0fY1I0VvI7JZ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-zDvp0fY1I0VvI7JZ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-zDvp0fY1I0VvI7JZ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-zDvp0fY1I0VvI7JZ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-zDvp0fY1I0VvI7JZ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-zDvp0fY1I0VvI7JZ .marker.cross{stroke:#333333;}#mermaid-svg-zDvp0fY1I0VvI7JZ svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-zDvp0fY1I0VvI7JZ p{margin:0;}#mermaid-svg-zDvp0fY1I0VvI7JZ .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-zDvp0fY1I0VvI7JZ text.actor&gt;tspan{fill:black;stroke:none;}#mermaid-svg-zDvp0fY1I0VvI7JZ .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-zDvp0fY1I0VvI7JZ .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-zDvp0fY1I0VvI7JZ .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-zDvp0fY1I0VvI7JZ .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-zDvp0fY1I0VvI7JZ #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-zDvp0fY1I0VvI7JZ .sequenceNumber{fill:white;}#mermaid-svg-zDvp0fY1I0VvI7JZ #sequencenumber{fill:#333;}#mermaid-svg-zDvp0fY1I0VvI7JZ #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-zDvp0fY1I0VvI7JZ .messageText{fill:#333;stroke:none;}#mermaid-svg-zDvp0fY1I0VvI7JZ .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-zDvp0fY1I0VvI7JZ .labelText,#mermaid-svg-zDvp0fY1I0VvI7JZ .labelText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-zDvp0fY1I0VvI7JZ .loopText,#mermaid-svg-zDvp0fY1I0VvI7JZ .loopText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-zDvp0fY1I0VvI7JZ .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-zDvp0fY1I0VvI7JZ .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-zDvp0fY1I0VvI7JZ .noteText,#mermaid-svg-zDvp0fY1I0VvI7JZ .noteText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-zDvp0fY1I0VvI7JZ .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-zDvp0fY1I0VvI7JZ .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-zDvp0fY1I0VvI7JZ .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-zDvp0fY1I0VvI7JZ .actorPopupMenu{position:absolute;}#mermaid-svg-zDvp0fY1I0VvI7JZ .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 \/ 0.4));}#mermaid-svg-zDvp0fY1I0VvI7JZ .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-zDvp0fY1I0VvI7JZ .actor-man circle,#mermaid-svg-zDvp0fY1I0VvI7JZ line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-zDvp0fY1I0VvI7JZ :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}fsockopen(&#039;tcp:\/\/example.com&#039;)\u975e\u963b\u585e connect()EAGAIN&#xff08;\u672a\u5c31\u7eea&#xff09;\u6ce8\u518c\u53ef\u5199\u4e8b\u4ef6\u4e8b\u4ef6\u89e6\u53d1\u8fd4\u56de socket \u8d44\u6e90\n<\/li>\n<\/ul>\n<h5>\u25b6 2. \u534f\u7a0b\u4e0a\u4e0b\u6587\u7ed1\u5b9a<\/h5>\n<ul>\n<li>\u6bcf\u4e2a\u534f\u7a0b\u72ec\u7acb&#xff1a;\n<ul>\n<li>Hook \u64cd\u4f5c\u7ed1\u5b9a\u5230\u5f53\u524d\u534f\u7a0b ID&#xff08;cid&#xff09;<\/li>\n<li>\u591a\u534f\u7a0b\u5e76\u53d1\u65f6\u4e92\u4e0d\u5e72\u6270<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>\u25b6 3. Hook Flags \u7ec4\u5408<\/h5>\n<ul>\n<li>\u5e38\u7528\u7ec4\u5408&#xff1a;<span class=\"token comment\">\/\/ \u542f\u7528\u6240\u6709 I\/O Hook&#xff08;\u63a8\u8350&#xff09;<\/span><br \/>\n<span class=\"token class-name static-context\">Co<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">set<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><br \/>\n    <span class=\"token string single-quoted-string\">&#039;hook_flags&#039;<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token constant\">SWOOLE_HOOK_ALL<\/span><br \/>\n<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4ec5\u542f\u7528 TCP &#043; \u6587\u4ef6<\/span><br \/>\n<span class=\"token class-name static-context\">Co<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">set<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><br \/>\n    <span class=\"token string single-quoted-string\">&#039;hook_flags&#039;<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token class-name\">SWOOLE_HOOK_TCP<\/span> <span class=\"token operator\">|<\/span> <span class=\"token class-name\">SWOOLE_HOOK_FILE<\/span><br \/>\n<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4>\u4e09\u3001\u5de5\u7a0b\u5b9e\u8df5&#xff1a;\u4f55\u65f6\u4f7f\u7528&#xff1f;\u5982\u4f55\u914d\u7f6e&#xff1f;<\/h4>\n<h5>\u25b6 1. \u5178\u578b\u573a\u666f<\/h5>\n<ul>\n<li>\u573a\u666f A&#xff1a;\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93&#xff08;\u5982 Guzzle\u3001Redis \u5ba2\u6237\u7aef&#xff09;\n<ul>\n<li>\u8fd9\u4e9b\u5e93\u5185\u90e8\u4f7f\u7528 stream_socket_client \u2192 \u9700 SWOOLE_HOOK_TCP<\/li>\n<\/ul>\n<\/li>\n<li>\u573a\u666f B&#xff1a;\u76f4\u63a5\u8c03\u7528 file_get_contents(&#039;http:\/\/&#8230;&#039;)\n<ul>\n<li>\u9700 SWOOLE_HOOK_TCP \u624d\u80fd\u534f\u7a0b\u5316<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>\u25b6 2. \u6b63\u786e\u914d\u7f6e\u4f4d\u7f6e<\/h5>\n<p><span class=\"token comment\">\/\/ \u5fc5\u987b\u5728 Co\\\\run() \u5185\u90e8\u6700\u5f00\u59cb\u8c03\u7528&#xff01;<\/span><br \/>\n<span class=\"token function\">Co<span class=\"token punctuation\">\\\\<\/span>run<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">function<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u2705 \u6b63\u786e&#xff1a;\u5728\u534f\u7a0b\u4e0a\u4e0b\u6587\u4e2d\u8bbe\u7f6e<\/span><br \/>\n    <span class=\"token class-name static-context\">Co<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">set<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string single-quoted-string\">&#039;hook_flags&#039;<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token constant\">SWOOLE_HOOK_TCP<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token function\">go<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">function<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u6b64\u5904\u7684 file_get_contents \u4f1a\u81ea\u52a8\u534f\u7a0b\u5316<\/span><br \/>\n        <span class=\"token variable\">$data<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">file_get_contents<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string single-quoted-string\">&#039;http:\/\/api.example.com&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">echo<\/span> <span class=\"token variable\">$data<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h5>\u25b6 3. \u9519\u8bef\u914d\u7f6e\u793a\u4f8b<\/h5>\n<p><span class=\"token comment\">\/\/ \u274c \u9519\u8bef&#xff1a;\u5728\u4e3b\u8fdb\u7a0b&#xff08;\u975e\u534f\u7a0b&#xff09;\u4e2d\u8c03\u7528<\/span><br \/>\n<span class=\"token class-name static-context\">Co<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">set<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string single-quoted-string\">&#039;hook_flags&#039;<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token constant\">SWOOLE_HOOK_TCP<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u629b\u51fa\u5f02\u5e38&#xff01;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u274c \u9519\u8bef&#xff1a;\u5728\u5b50\u534f\u7a0b\u4e2d\u8bbe\u7f6e&#xff08;\u5e94\u5168\u5c40\u8bbe\u7f6e&#xff09;<\/span><br \/>\n<span class=\"token function\">go<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">function<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name static-context\">Co<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">set<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&#8230;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u53ef\u80fd\u5bfc\u81f4\u90e8\u5206\u534f\u7a0b\u672a\u751f\u6548<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<hr \/>\n<h4>\u56db\u3001\u907f\u5751\u6307\u5357<\/h4>\n<table>\n<tr>\u9677\u9631\u7834\u5c40\u65b9\u6848<\/tr>\n<tbody>\n<tr>\n<td>Hook \u672a\u751f\u6548<\/td>\n<td>\u786e\u4fdd\u5728 Co\\\\run() \u5185\u90e8\u6700\u5f00\u59cb\u8c03\u7528 Co::set()<\/td>\n<\/tr>\n<tr>\n<td>\u6df7\u5408\u963b\u585e\/\u975e\u963b\u585e<\/td>\n<td>\u907f\u514d\u5728 Hook \u540e\u4f7f\u7528\u539f\u751f\u963b\u585e\u51fd\u6570&#xff08;\u5982 sleep()&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>cURL \u672a\u534f\u7a0b\u5316<\/td>\n<td>\u9700\u989d\u5916\u542f\u7528 SWOOLE_HOOK_CURL&#xff08;Swoole 4.5&#043;&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>\u25b6 \u9a8c\u8bc1 Hook \u662f\u5426\u751f\u6548<\/h5>\n<p><span class=\"token function\">Co<span class=\"token punctuation\">\\\\<\/span>run<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">function<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name static-context\">Co<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">set<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">[<\/span><span class=\"token string single-quoted-string\">&#039;hook_flags&#039;<\/span> <span class=\"token operator\">&#061;&gt;<\/span> <span class=\"token constant\">SWOOLE_HOOK_TCP<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token variable\">$start<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">microtime<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant boolean\">true<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5e76\u53d1\u8bf7\u6c42<\/span><br \/>\n    <span class=\"token function\">go<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">function<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token function\">file_get_contents<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string single-quoted-string\">&#039;http:\/\/httpbin.org\/delay\/1&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">go<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">function<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span> <span class=\"token function\">file_get_contents<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string single-quoted-string\">&#039;http:\/\/httpbin.org\/delay\/1&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u603b\u8017\u65f6 \u2248 1 \u79d2&#xff08;\u975e 2 \u79d2&#xff09;\u2192 Hook \u751f\u6548<\/span><br \/>\n    <span class=\"token keyword\">echo<\/span> <span class=\"token string double-quoted-string\">&#034;\u8017\u65f6: &#034;<\/span> <span class=\"token operator\">.<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token function\">microtime<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant boolean\">true<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#8211;<\/span> <span class=\"token variable\">$start<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">.<\/span> <span class=\"token string double-quoted-string\">&#034; \u79d2\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<hr \/>\n<h4>\u4e94\u3001\u7ec8\u6781\u5fc3\u6cd5<\/h4>\n<p>**\u201cHook \u4e0d\u662f\u9b54\u6cd5&#xff0c;<br \/>\n\u800c\u662f\u540c\u6b65\u7684\u9690\u8eab\u8863\u2014\u2014<\/p>\n<ul>\n<li>\u5f53\u4f60 \u542f\u7528 TCP Hook&#xff0c;<br \/>\n\u4f60\u5728\u900f\u660e\u534f\u7a0b\u5316&#xff1b;<\/li>\n<li>\u5f53\u4f60 \u5168\u5c40\u8bbe\u7f6e&#xff0c;<br \/>\n\u4f60\u5728\u7edf\u4e00\u4e0a\u4e0b\u6587&#xff1b;<\/li>\n<li>\u5f53\u4f60 \u9a8c\u8bc1\u5e76\u53d1&#xff0c;<br \/>\n\u4f60\u5728\u786e\u8ba4\u751f\u6548\u3002<\/li>\n<\/ul>\n<p>\u771f\u6b63\u7684\u9ad8\u5e76\u53d1&#xff0c;<br \/>\n\u59cb\u4e8e\u5bf9\u963b\u585e\u7684\u656c\u754f&#xff0c;<br \/>\n\u6210\u4e8e\u5bf9\u7ec6\u8282\u7684\u7cbe\u63a7\u3002\u201d<\/p>\n<hr \/>\n<h4>\u7ed3\u8bed<\/h4>\n<p>\u4ece\u4eca\u5929\u8d77&#xff1a;<\/p>\n<li>\u6240\u6709\u534f\u7a0b\u7a0b\u5e8f\u5728 Co\\\\run() \u5f00\u5934\u8bbe\u7f6e hook_flags<\/li>\n<li>\u7528\u5e76\u53d1\u6d4b\u8bd5\u9a8c\u8bc1 Hook \u662f\u5426\u751f\u6548<\/li>\n<li>\u4f18\u5148\u4f7f\u7528 SWOOLE_HOOK_ALL&#xff08;\u9664\u975e\u6709\u51b2\u7a81&#xff09;<\/li>\n<p>\u56e0\u4e3a\u6700\u597d\u7684\u534f\u7a0b\u7f16\u7a0b&#xff0c;<br \/>\n\u4e0d\u662f\u624b\u52a8\u5f02\u6b65&#xff0c;<br \/>\n\u800c\u662f\u8ba9\u540c\u6b65\u4ee3\u7801\u81ea\u52a8\u98de\u7fd4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Co::set([hook_flags &gt; SWOOLE_HOOK_TCP]); \u662f Swoole \u534f\u7a0b\u8fd0\u884c\u65f6\u7684\u5173\u952e\u914d\u7f6e\u6307\u4ee4&#xff0c;\u7528\u4e8e\u542f\u7528\u5bf9 TCP \u76f8\u5173\u51fd\u6570\u7684\u534f\u7a0b Hook&#xff08;\u5373\u81ea\u52a8\u534f\u7a0b\u5316&#xff09;\u3002\u5b83\u51b3\u5b9a\u4e86\u54ea\u4e9b\u963b\u585e I\/O \u64cd\u4f5c\u4f1a\u88ab\u900f\u660e\u5730\u66ff\u6362\u4e3a\u975e\u963b\u585e\u534f\u7a0b\u8c03\u5ea6&#xff0c;\u662f\u5b9e\u73b0\u9ad8\u6027\u80fd\u5e76\u53d1\u7684\u6838\u5fc3\u5f00\u5173\u3002\u4e00\u3001\u6838\u5fc3\u539f\u7406&#xff1a;\u4ec0\u4e48\u662f Hook&#xff1f;<br \/>\n\u25b6 1. Hook \u7684\u672c\u8d28<br \/>\n\u76ee\u7684&#xff1a;\u5c06 \u963b\u585e\u5f0f I\/O \u51fd\u6570&amp;#xff0<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2140,205,292],"topic":[],"class_list":["post-67061","post","type-post","status-publish","format-standard","hentry","category-server","tag-swoole","tag-tcp-ip","tag-292"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Co::set([\u2018hook_flags\u2018 =\uff1e SWOOLE_HOOK_TCP])\uff1b\u7684\u5e96\u4e01\u89e3\u725b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/67061.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Co::set([\u2018hook_flags\u2018 =\uff1e SWOOLE_HOOK_TCP])\uff1b\u7684\u5e96\u4e01\u89e3\u725b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Co::set([hook_flags &gt; SWOOLE_HOOK_TCP]); \u662f Swoole \u534f\u7a0b\u8fd0\u884c\u65f6\u7684\u5173\u952e\u914d\u7f6e\u6307\u4ee4&#xff0c;\u7528\u4e8e\u542f\u7528\u5bf9 TCP \u76f8\u5173\u51fd\u6570\u7684\u534f\u7a0b Hook&#xff08;\u5373\u81ea\u52a8\u534f\u7a0b\u5316&#xff09;\u3002\u5b83\u51b3\u5b9a\u4e86\u54ea\u4e9b\u963b\u585e I\/O \u64cd\u4f5c\u4f1a\u88ab\u900f\u660e\u5730\u66ff\u6362\u4e3a\u975e\u963b\u585e\u534f\u7a0b\u8c03\u5ea6&#xff0c;\u662f\u5b9e\u73b0\u9ad8\u6027\u80fd\u5e76\u53d1\u7684\u6838\u5fc3\u5f00\u5173\u3002\u4e00\u3001\u6838\u5fc3\u539f\u7406&#xff1a;\u4ec0\u4e48\u662f Hook&#xff1f; \u25b6 1. Hook \u7684\u672c\u8d28 \u76ee\u7684&#xff1a;\u5c06 \u963b\u585e\u5f0f I\/O \u51fd\u6570&amp;#xff0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/67061.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-27T23:25:16+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/67061.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/67061.html\",\"name\":\"Co::set([\u2018hook_flags\u2018 =\uff1e SWOOLE_HOOK_TCP])\uff1b\u7684\u5e96\u4e01\u89e3\u725b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-27T23:25:16+00:00\",\"dateModified\":\"2026-01-27T23:25:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/67061.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/67061.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/67061.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Co::set([\u2018hook_flags\u2018 =\uff1e SWOOLE_HOOK_TCP])\uff1b\u7684\u5e96\u4e01\u89e3\u725b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Co::set([\u2018hook_flags\u2018 =\uff1e SWOOLE_HOOK_TCP])\uff1b\u7684\u5e96\u4e01\u89e3\u725b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/67061.html","og_locale":"zh_CN","og_type":"article","og_title":"Co::set([\u2018hook_flags\u2018 =\uff1e SWOOLE_HOOK_TCP])\uff1b\u7684\u5e96\u4e01\u89e3\u725b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Co::set([hook_flags &gt; SWOOLE_HOOK_TCP]); \u662f Swoole \u534f\u7a0b\u8fd0\u884c\u65f6\u7684\u5173\u952e\u914d\u7f6e\u6307\u4ee4&#xff0c;\u7528\u4e8e\u542f\u7528\u5bf9 TCP \u76f8\u5173\u51fd\u6570\u7684\u534f\u7a0b Hook&#xff08;\u5373\u81ea\u52a8\u534f\u7a0b\u5316&#xff09;\u3002\u5b83\u51b3\u5b9a\u4e86\u54ea\u4e9b\u963b\u585e I\/O \u64cd\u4f5c\u4f1a\u88ab\u900f\u660e\u5730\u66ff\u6362\u4e3a\u975e\u963b\u585e\u534f\u7a0b\u8c03\u5ea6&#xff0c;\u662f\u5b9e\u73b0\u9ad8\u6027\u80fd\u5e76\u53d1\u7684\u6838\u5fc3\u5f00\u5173\u3002\u4e00\u3001\u6838\u5fc3\u539f\u7406&#xff1a;\u4ec0\u4e48\u662f Hook&#xff1f; \u25b6 1. Hook \u7684\u672c\u8d28 \u76ee\u7684&#xff1a;\u5c06 \u963b\u585e\u5f0f I\/O \u51fd\u6570&amp;#xff0","og_url":"https:\/\/www.wsisp.com\/helps\/67061.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-27T23:25:16+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/67061.html","url":"https:\/\/www.wsisp.com\/helps\/67061.html","name":"Co::set([\u2018hook_flags\u2018 =\uff1e SWOOLE_HOOK_TCP])\uff1b\u7684\u5e96\u4e01\u89e3\u725b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-27T23:25:16+00:00","dateModified":"2026-01-27T23:25:16+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/67061.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/67061.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/67061.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Co::set([\u2018hook_flags\u2018 =\uff1e SWOOLE_HOOK_TCP])\uff1b\u7684\u5e96\u4e01\u89e3\u725b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/67061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=67061"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/67061\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=67061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=67061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=67061"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=67061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}