{"id":24941,"date":"2024-11-21T09:44:24","date_gmt":"2024-11-21T01:44:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24941\/"},"modified":"2024-11-21T09:44:24","modified_gmt":"2024-11-21T01:44:24","slug":"workerman%e5%bc%80%e5%8f%91%ef%bc%9a%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%9f%ba%e4%ba%8etcp%e5%8d%8f%e8%ae%ae%e7%9a%84%e8%bf%9c%e7%a8%8b%e6%96%87%e4%bb%b6%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24941\/","title":{"rendered":"Workerman\u5f00\u53d1\uff1a\u5982\u4f55\u5b9e\u73b0\u57fa\u4e8eTCP\u534f\u8bae\u7684\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169931801835379.jpg\" class=\"aligncenter\" title=\"Workerman\u5f00\u53d1\uff1a\u5982\u4f55\u5b9e\u73b0\u57fa\u4e8eTCP\u534f\u8bae\u7684\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\u63d2\u56fe\" alt=\"Workerman\u5f00\u53d1\uff1a\u5982\u4f55\u5b9e\u73b0\u57fa\u4e8eTCP\u534f\u8bae\u7684\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\u63d2\u56fe\" \/><\/p>\n<p>Workerman\u5f00\u53d1\uff1a\u5982\u4f55\u5b9e\u73b0\u57fa\u4e8eTCP\u534f\u8bae\u7684\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740\u4e91\u8ba1\u7b97\u548c\u8fdc\u7a0b\u5de5\u4f5c\u7684\u5174\u8d77\uff0c\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\u6210\u4e3a\u4e86\u8d8a\u6765\u8d8a\u591a\u4f01\u4e1a\u548c\u4e2a\u4eba\u7684\u9700\u6c42\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Workerman\u6846\u67b6\u5b9e\u73b0\u4e00\u4e2a\u57fa\u4e8eTCP\u534f\u8bae\u7684\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<br \/>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u4e00\u4e9b\u5fc5\u8981\u7684\u5de5\u5177\u548c\u73af\u5883\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PHP\u73af\u5883\uff0c\u5e76\u4e14\u62e5\u6709\u4f7f\u7528Composer\u7684\u6743\u5229\u3002\u7136\u540e\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Workerman\u3002\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\uff1a<\/p>\n<pre>composer require workerman\/workerman<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e8c\u3001\u5efa\u7acbTCP\u670d\u52a1\u5668<br \/>\u4f7f\u7528Workerman\u5efa\u7acbTCP\u670d\u52a1\u5668\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php require_once __DIR__.'\/vendor\/autoload.php';\n\nuse WorkermanWorker;\n\n$tcp_worker = new Worker(\"tcp:\/\/0.0.0.0:8080\");\n\n$tcp_worker-&gt;onConnect = function ($connection) {\n    echo \"New client connected\n\";\n};\n\n$tcp_worker-&amp;gt;onClose = function ($connection) {\n    echo \"Client connection closed\n\";\n};\n\n$tcp_worker-&amp;gt;onMessage = function ($connection, $data) {\n    echo \"Received message from client: $data\n\";\n    \/\/ \u5728\u8fd9\u91cc\u89e3\u6790\u5ba2\u6237\u7aef\u4f20\u6765\u7684\u547d\u4ee4\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u6587\u4ef6\u64cd\u4f5c\n    \/\/ ...\n};\n\nWorker::runAll();<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e09\u3001\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42<br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5904\u7406\u5ba2\u6237\u7aef\u4f20\u6765\u7684\u8bf7\u6c42\uff0c\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u6587\u4ef6\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u5904\u7406\u5ba2\u6237\u7aef\u4f20\u6765\u7684\u547d\u4ee4\uff0c\u6bd4\u5982\u4e0a\u4f20\u6587\u4ef6\u3001\u4e0b\u8f7d\u6587\u4ef6\u3001\u5220\u9664\u6587\u4ef6\u7b49\u64cd\u4f5c\uff1a<\/p>\n<pre>\/\/ ...\n\n$tcp_worker-&amp;gt;onMessage = function ($connection, $data) {\n    echo \"Received message from client: $data\n\";\n    $command = json_decode($data, true);\n    \n    switch ($command['action']) {\n        case 'upload':\n            if (isset($command['file'])) {\n                $file_content = base64_decode($command['file']);\n                file_put_contents($command['path'], $file_content);\n                $connection-&amp;gt;send(\"File uploaded successfully\n\");\n            } else {\n                $connection-&amp;gt;send(\"Invalid file format\n\");\n            }\n            break;\n        \n        case 'download':\n            if (file_exists($command['path'])) {\n                $file_content = file_get_contents($command['path']);\n                $file_content_base64 = base64_encode($file_content);\n                $connection-&amp;gt;send(json_encode(['data' =&amp;gt; $file_content_base64]).\"\n\");\n            } else {\n                $connection-&amp;gt;send(\"File not found\n\");\n            }\n            break;\n        \n        case 'delete':\n            if (file_exists($command['path'])) {\n                unlink($command['path']);\n                $connection-&amp;gt;send(\"File deleted successfully\n\");\n            } else {\n                $connection-&amp;gt;send(\"File not found\n\");\n            }\n            break;\n            \n        \/\/ \u5176\u4ed6\u547d\u4ee4\u7684\u5904\u7406\u4ee3\u7801...\n    }\n};\n\n\/\/ ...<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0a\u8ff0\u4ee3\u7801\u4e2d\u6211\u4eec\u5047\u8bbe\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u91c7\u7528JSON\u683c\u5f0f\uff0c\u5e76\u4e14\u4f7f\u7528\u4e86base64\u5bf9\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u4e86\u7f16\u7801\u3002<\/p>\n<p>\u56db\u3001\u4e0e\u5ba2\u6237\u7aef\u4ea4\u4e92<br \/>\u5ba2\u6237\u7aef\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u652f\u6301TCP\u534f\u8bae\u7684\u5de5\u5177\u6216\u7f16\u7a0b\u8bed\u8a00\u4e0e\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u5ba2\u6237\u7aef\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u4e0a\u4f20\u6587\u4ef6\uff1a<\/p>\n<pre>import socket\nimport json\n\naddress = ('127.0.0.1', 8080)\nclient_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nclient_socket.connect(address)\n\ncommand = {\n    'action': 'upload',\n    'path': '\/path\/to\/file.txt',\n    'file': ''\n}\n\nwith open('file.txt', 'rb') as file:\n    command['file'] = file.read().decode('base64')\n\nclient_socket.send(json.dumps(command).encode())\nprint(client_socket.recv(1024).decode())\nclient_socket.close()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e94\u3001\u603b\u7ed3<br \/>\u901a\u8fc7\u4f7f\u7528Workerman\u6846\u67b6\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u8f7b\u677e\u5730\u5b9e\u73b0\u57fa\u4e8eTCP\u534f\u8bae\u7684\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\u3002\u672c\u6587\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5e76\u8ba8\u8bba\u4e86\u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42\u4ee5\u53ca\u4e0e\u5ba2\u6237\u7aef\u4ea4\u4e92\u7684\u65b9\u6cd5\u3002\u5e0c\u671b\u8bfb\u8005\u80fd\u591f\u901a\u8fc7\u672c\u6587\u4e86\u89e3\u5230\u5982\u4f55\u4f7f\u7528Workerman\u5f00\u53d1\u6b64\u7c7b\u7cfb\u7edf\uff0c\u5e76\u4ece\u4e2d\u83b7\u5f97\u542f\u53d1\u548c\u5e2e\u52a9\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u6269\u5c55\u548c\u6539\u8fdb\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fWorkerman\u5f00\u53d1\uff1a\u5982\u4f55\u5b9e\u73b0\u57fa\u4e8eTCP\u534f\u8bae\u7684\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Workerman\u5f00\u53d1\uff1a\u5982\u4f55\u5b9e\u73b0\u57fa\u4e8eTCP\u534f\u8bae\u7684\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf \u5f15\u8a00\uff1a\u968f\u7740\u4e91\u8ba1\u7b97\u548c\u8fdc\u7a0b\u5de5\u4f5c\u7684\u5174\u8d77\uff0c\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\u6210\u4e3a\u4e86\u8d8a\u6765\u8d8a\u591a\u4f01\u4e1a\u548c\u4e2a\u4eba\u7684\u9700\u6c42\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Workerman\u6846\u67b6\u5b9e\u73b0\u4e00\u4e2a\u57fa\u4e8eTCP\u534f\u8bae\u7684\u8fdc\u7a0b\u6587\u4ef6\u7ba1\u7406\u7cfb\u7edf\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u51c6\u5907\u5de5\u4f5c\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u4e00\u4e9b\u5fc5\u8981\u7684\u5de5\u5177\u548c\u73af\u5883\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PHP\u73af\u5883\uff0c\u5e76\u4e14\u62e5\u6709\u4f7f\u7528Composer\u7684\u6743\u5229\u3002\u7136\u540e\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5Workerman\u3002\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\uff1a composer require workerman\/workerman \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u5efa\u7acbTCP\u670d\u52a1\u5668\u4f7f\u7528Workerman\u5efa\u7acbTCP\u670d\u52a1\u5668\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a &lt;?php require_once __DIR__.&#8217;\/vendor\/autoload.php&#8217;; use WorkermanWorker; $tcp_worker = new Worker(&#8220;tcp:\/\/0.0.0.0:8080&#8221;); $tcp_worker-&gt;onConnect = function ($connection) { echo &#8220;New client connected &#8220;; }; $tcp_worker-&amp;gt;onClose = function ($connection) { echo &#8220;Client connection closed &#8220;; }; $tcp_worker-&amp;gt;onMessage = function ($connection, $data) { echo &#8220;Received message from client: $data &#8220;; \/\/ \u5728\u8fd9\u91cc\u89e3\u6790\u5ba2\u6237\u7aef\u4f20\u6765\u7684\u547d\u4ee4\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u6587\u4ef6\u64cd\u4f5c \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24941","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24941"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24941\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}