{"id":24302,"date":"2024-11-21T14:53:15","date_gmt":"2024-11-21T06:53:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24302\/"},"modified":"2024-11-21T14:53:15","modified_gmt":"2024-11-21T06:53:15","slug":"thinkphp6%e8%81%8a%e5%a4%a9%e5%ae%a4%e5%bc%80%e5%8f%91%e6%8c%87%e5%8d%97%ef%bc%9a%e5%ae%9e%e7%8e%b0%e5%ae%9e%e6%97%b6%e9%80%9a%e8%ae%af%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24302\/","title":{"rendered":"ThinkPHP6\u804a\u5929\u5ba4\u5f00\u53d1\u6307\u5357\uff1a\u5b9e\u73b0\u5b9e\u65f6\u901a\u8baf\u529f\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169182190882223.jpg\" class=\"aligncenter\" title=\"ThinkPHP6\u804a\u5929\u5ba4\u5f00\u53d1\u6307\u5357\uff1a\u5b9e\u73b0\u5b9e\u65f6\u901a\u8baf\u529f\u80fd\u63d2\u56fe\" alt=\"ThinkPHP6\u804a\u5929\u5ba4\u5f00\u53d1\u6307\u5357\uff1a\u5b9e\u73b0\u5b9e\u65f6\u901a\u8baf\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>ThinkPHP6\u804a\u5929\u5ba4\u5f00\u53d1\u6307\u5357\uff1a\u5b9e\u73b0\u5b9e\u65f6\u901a\u8baf\u529f\u80fd<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u5b9e\u65f6\u901a\u8baf\u7684\u9700\u6c42\u4e5f\u8d8a\u6765\u8d8a\u5927\u3002\u804a\u5929\u5ba4\u4f5c\u4e3a\u4e00\u79cd\u5e38\u89c1\u7684\u5b9e\u65f6\u901a\u8baf\u65b9\u5f0f\uff0c\u53d7\u5230\u4e86\u5e7f\u6cdb\u7684\u5173\u6ce8\u548c\u4f7f\u7528\u3002\u672c\u6587\u5c06\u901a\u8fc7\u4f7f\u7528ThinkPHP6\u6846\u67b6\uff0c\u4e3a\u5927\u5bb6\u63d0\u4f9b\u4e00\u79cd\u7b80\u5355\u3001\u5feb\u901f\u5b9e\u73b0\u5b9e\u65f6\u901a\u8baf\u529f\u80fd\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u4e00\u3001\u73af\u5883\u914d\u7f6e\uff1a<br \/>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6e\u597d\u5f00\u53d1\u73af\u5883\u3002\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PHP\u548cThinkPHP6\u6846\u67b6\u3002\u540c\u65f6\uff0c\u672c\u6587\u5c06\u4f7f\u7528MySQL\u6570\u636e\u5e93\uff0c\u56e0\u6b64\u4e5f\u9700\u8981\u786e\u4fdd\u4f60\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u5e76\u914d\u7f6e\u4e86MySQL\u3002<\/p>\n<p>\u4e8c\u3001\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868\uff1a<br \/>\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u540d\u4e3achatroom\u7684\u6570\u636e\u5e93\u3002\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amessages\u7684\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u804a\u5929\u6d88\u606f\u3002\u8868\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>CREATE TABLE `messages` (\n  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\n  `content` text COLLATE utf8mb4_unicode_ci NOT NULL,\n  `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e09\u3001\u7f16\u5199\u63a7\u5236\u5668\u548c\u89c6\u56fe\uff1a<br \/>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aChatroom\u63a7\u5236\u5668\uff0c\u7528\u4e8e\u5904\u7406\u804a\u5929\u5ba4\u76f8\u5173\u7684\u903b\u8f91\u3002\u5728app\/controller\u76ee\u5f55\u4e0b\u521b\u5efaChatroom.php\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>acadeView;\nuse GatewayWorkerLibGateway;\n\nclass Chatroom\n{\n    public function index()\n    {\n        return View::fetch('index');\n    }\n\n    public function sendMessage()\n    {\n        $content = input('post.content');\n        $data = [\n            'content' =&amp;gt; $content,\n            'created_at' =&amp;gt; date('Y-m-d H:i:s')\n        ];\n            hinkacadeDb::name('messages')-&amp;gt;insert($data);\n        Gateway::sendToAll(json_encode($data));\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728app\/view\u76ee\u5f55\u4e0b\u521b\u5efaindex.html\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>\n\n\n    &lt;title&gt;\u804a\u5929\u5ba4&lt;\/title&gt;&lt;script src=\"https:\/\/cdn.bootcdn.net\/ajax\/libs\/jquery\/3.6.0\/jquery.min.js\"&gt;&lt;\/script&gt;&lt;div&gt;\n        &lt;textarea id=\"message\" style=\"width: 300px; height: 100px;\"&gt;&lt;\/textarea&gt;&lt;button onclick=\"sendMessage()\"&gt;\u53d1\u9001&lt;\/button&gt;\n    &lt;\/div&gt;\n    &lt;div id=\"chatContent\"&gt;&lt;\/div&gt;\n\n&lt;script&gt;\n    var websocket = new WebSocket('ws:\/\/localhost:8282');\n    websocket.onopen = function () {\n        console.log('\u8fde\u63a5\u6210\u529f');\n    };\n    websocket.onmessage = function (evt) {\n        var message = JSON.parse(evt.data);\n        $('#chatContent').append('&lt;p&gt;' + message.content + ' - ' + message.created_at + '');\n    };\n    websocket.onerror = function () {\n        console.log('\u8fde\u63a5\u5931\u8d25');\n    };\n    websocket.onclose = function () {\n        console.log('\u65ad\u5f00\u8fde\u63a5');\n    };\n\n    function sendMessage() {\n        var content = $('#message').val();\n        $.ajax({\n            type: 'POST',\n            url: '&lt;?php echo url(\"Chatroom\/sendMessage\"); ?&gt;',\n            data: {content: content},\n            success: function () {\n                $('#message').val('');\n            },\n            error: function () {\n                alert('\u53d1\u9001\u5931\u8d25');\n            }\n        });\n    }\n&lt;\/script&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u56db\u3001\u542f\u52a8WebSocket\u670d\u52a1\uff1a<br \/>ThinkPHP6\u9ed8\u8ba4\u5e76\u6ca1\u6709\u96c6\u6210WebSocket\u670d\u52a1\uff0c\u6211\u4eec\u9700\u8981\u501f\u52a9GatewayWorker\u6269\u5c55\u6765\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u6211\u4eec\u8981\u5b89\u88c5GatewayWorker\u6269\u5c55\uff1a<\/p>\n<pre>composer require workerman\/gatewayworker<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efastart.php\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>acadeDb;\nuse WorkermanWorker;\nuse GatewayWorkerGateway;\n\nrequire __DIR__ . '\/vendor\/autoload.php';\n\n$worker = new Worker('websocket:\/\/0.0.0.0:8282');\n$worker-&amp;gt;name = 'ChatroomGateway';\n$worker-&amp;gt;count = 1;\n\n$worker-&amp;gt;onWorkerStart = function () {\n    Gateway::$registerAddress = '127.0.0.1:1238';\n    Gateway::onConnect(function ($connection) {\n        $messages = Db::name('messages')-&amp;gt;select();\n        Gateway::sendToCurrentClient(json_encode($messages));\n    });\n    Gateway::onMessage(function ($connection, $data) {\n        Gateway::sendToAll($data);\n    });\n};\n\nWorker::runAll();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u5728\u547d\u4ee4\u884c\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8WebSocket\u670d\u52a1\uff1a<\/p>\n<pre>php start.php start<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e94\u3001\u5b8c\u6210\uff1a<br \/>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95eehttp:\/\/localhost\/chatroom\/index\u6765\u4f7f\u7528\u804a\u5929\u5ba4\u4e86\u3002\u8f93\u5165\u6d88\u606f\u540e\u70b9\u51fb\u53d1\u9001\uff0c\u5373\u53ef\u5b9e\u73b0\u6d88\u606f\u7684\u5b9e\u65f6\u53d1\u9001\u548c\u63a5\u6536\u3002<\/p>\n<p>\u7ed3\u8bed\uff1a<br \/>\u901a\u8fc7\u672c\u6587\u7684\u6307\u5357\uff0c\u6211\u4eec\u6210\u529f\u5730\u4f7f\u7528ThinkPHP6\u6846\u67b6\u548cGatewayWorker\u6269\u5c55\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u804a\u5929\u5ba4\u3002\u5e0c\u671b\u672c\u6587\u80fd\u4e3a\u8bfb\u8005\u63d0\u4f9b\u4e00\u4e9b\u6709\u7528\u7684\u53c2\u8003\uff0c\u5e2e\u52a9\u5feb\u901f\u5b9e\u73b0\u5b9e\u65f6\u901a\u8baf\u529f\u80fd\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u672c\u6587\u53ea\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff0c\u5b9e\u9645\u9879\u76ee\u4e2d\u8fd8\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u8fdb\u884c\u76f8\u5e94\u7684\u6269\u5c55\u548c\u4f18\u5316\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP6\u804a\u5929\u5ba4\u5f00\u53d1\u6307\u5357\uff1a\u5b9e\u73b0\u5b9e\u65f6\u901a\u8baf\u529f\u80fd\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>ThinkPHP6\u804a\u5929\u5ba4\u5f00\u53d1\u6307\u5357\uff1a\u5b9e\u73b0\u5b9e\u65f6\u901a\u8baf\u529f\u80fd \u5f15\u8a00\uff1a\u968f\u7740\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u5b9e\u65f6\u901a\u8baf\u7684\u9700\u6c42\u4e5f\u8d8a\u6765\u8d8a\u5927\u3002\u804a\u5929\u5ba4\u4f5c\u4e3a\u4e00\u79cd\u5e38\u89c1\u7684\u5b9e\u65f6\u901a\u8baf\u65b9\u5f0f\uff0c\u53d7\u5230\u4e86\u5e7f\u6cdb\u7684\u5173\u6ce8\u548c\u4f7f\u7528\u3002\u672c\u6587\u5c06\u901a\u8fc7\u4f7f\u7528ThinkPHP6\u6846\u67b6\uff0c\u4e3a\u5927\u5bb6\u63d0\u4f9b\u4e00\u79cd\u7b80\u5355\u3001\u5feb\u901f\u5b9e\u73b0\u5b9e\u65f6\u901a\u8baf\u529f\u80fd\u7684\u65b9\u6cd5\u3002 \u4e00\u3001\u73af\u5883\u914d\u7f6e\uff1a\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6e\u597d\u5f00\u53d1\u73af\u5883\u3002\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PHP\u548cThinkPHP6\u6846\u67b6\u3002\u540c\u65f6\uff0c\u672c\u6587\u5c06\u4f7f\u7528MySQL\u6570\u636e\u5e93\uff0c\u56e0\u6b64\u4e5f\u9700\u8981\u786e\u4fdd\u4f60\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u5e76\u914d\u7f6e\u4e86MySQL\u3002 \u4e8c\u3001\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868\uff1a\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u540d\u4e3achatroom\u7684\u6570\u636e\u5e93\u3002\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amessages\u7684\u8868\uff0c\u7528\u4e8e\u5b58\u50a8\u804a\u5929\u6d88\u606f\u3002\u8868\u7ed3\u6784\u5982\u4e0b\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b CREATE TABLE `messages` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `content` text COLLATE utf8mb4_unicode_ci NOT NULL, `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; \u767b\u5f55\u540e\u590d\u5236 \u4e09\u3001\u7f16\u5199\u63a7\u5236\u5668\u548c\u89c6\u56fe\uff1a\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aChatroom\u63a7\u5236\u5668\uff0c\u7528\u4e8e\u5904\u7406\u804a\u5929\u5ba4\u76f8\u5173\u7684\u903b\u8f91\u3002\u5728app\/controller\u76ee\u5f55\u4e0b\u521b\u5efaChatroom.php\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a acadeView; use GatewayWorkerLibGateway; class Chatroom { public function index() { return View::fetch(&#8216;index&#8217;); } public function sendMessage() { $content = [&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-24302","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24302","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=24302"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24302\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}