{"id":47922,"date":"2024-12-02T09:18:51","date_gmt":"2024-12-02T01:18:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47922\/"},"modified":"2024-12-02T09:18:51","modified_gmt":"2024-12-02T01:18:51","slug":"php%e7%bd%91%e7%bb%9c%e7%bc%96%e7%a8%8b%e6%8c%87%e5%8d%97%ef%bc%9awebsocket%e7%bc%96%e7%a8%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47922\/","title":{"rendered":"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aWebSocket\u7f16\u7a0b\u8be6\u89e3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aWebSocket\u7f16\u7a0b\u8be6\u89e3<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aWebSocket\u7f16\u7a0b\u8be6\u89e3\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>WebSocket \u662f\u4e00\u79cd\u5141\u8bb8\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u901a\u8fc7\u5355\u4e2a TCP \u8fde\u63a5\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u4fe1\u7684\u534f\u8bae\u3002\u4f7f\u7528 PHP \u5f00\u53d1 WebSocket \u5e94\u7528\u7a0b\u5e8f\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u5b89\u88c5 Ratchet Pawl \u5e93\u3002\u521b\u5efa WebSocket \u670d\u52a1\u5668\uff0c\u4f7f\u7528 Ratchet Pawl \u5e93\u5e76\u5b9e\u73b0 WebSocket \u5e94\u7528\u7a0b\u5e8f\u7c7b\u3002\u5b9e\u73b0 onOpen\u3001onMessage\u3001onClose \u548c onError \u65b9\u6cd5\u6765\u5904\u7406\u8fde\u63a5\u548c\u6d88\u606f\u3002\u4f7f\u7528 send() \u65b9\u6cd5\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u3002\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u804a\u5929\u5ba4\u6f14\u793a WebSocket \u5e94\u7528\u7a0b\u5e8f\u7684\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729943446671cd7962cc9e.jpg\" class=\"aligncenter\" title=\"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aWebSocket\u7f16\u7a0b\u8be6\u89e3\u63d2\u56fe\" alt=\"php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aWebSocket\u7f16\u7a0b\u8be6\u89e3\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aWebSocket \u7f16\u7a0b\u8be6\u89e3<\/strong><\/p>\n<p>WebSocket \u662f\u4e00\u79cd\u5168\u53cc\u5de5\u901a\u4fe1\u534f\u8bae\uff0c\u5141\u8bb8\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u5728\u5355\u4e2a TCP \u8fde\u63a5\u4e0a\u8fdb\u884c\u5b9e\u65f6\u901a\u4fe1\u3002\u4e0b\u9762\u662f\u4f7f\u7528 PHP \u7f16\u5199 WebSocket \u5e94\u7528\u7a0b\u5e8f\u7684\u5206\u6b65\u6307\u5357\u3002<\/p>\n<p><strong>1. \u5b89\u88c5\u4f9d\u8d56<\/strong><\/p>\n<pre>composer require ratchet\/pawl<\/pre>\n<p><strong>2. \u521b\u5efa WebSocket \u670d\u52a1\u5668<\/strong><\/p>\n<p>\u4f7f\u7528 Ratchet Pawl \u5e93\u521b\u5efa WebSocket \u670d\u52a1\u5668\uff1a<\/p>\n<pre>use Ratchet\\Server\\IoServer;\nuse Ratchet\\Http\\HttpServer;\nuse Ratchet\\WebSocket\\WsServer;\nuse Example\\WebsocketApplication;\n\n$transport = new HttpServer(new WsServer(new WebsocketApplication()));\n$server = IoServer::factory($transport, 8080);\n$server-&gt;run();<\/pre>\n<p><strong>3. \u5b9e\u73b0 WebSocket \u5e94\u7528\u7a0b\u5e8f<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u5b9e\u73b0 <code>\\Ratchet\\WebSocket\\MessageComponentInterface<\/code> \u63a5\u53e3\u7684 WebSocket \u5e94\u7528\u7a0b\u5e8f\u7c7b\uff1a<\/p>\n<pre>namespace Example;\n\nclass WebsocketApplication implements \\Ratchet\\WebSocket\\MessageComponentInterface\n{\n    \/\/ ...\n}<\/pre>\n<p><strong>4. \u5904\u7406\u8fde\u63a5\u548c\u6d88\u606f<\/strong><\/p>\n<p>\u5728\u5e94\u7528\u7a0b\u5e8f\u7c7b\u4e2d\uff0c\u5b9e\u73b0\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong>onOpen<\/strong>: \u5728\u5ba2\u6237\u7aef\u8fde\u63a5\u65f6\u8c03\u7528\u3002<\/li>\n<li><strong>onMessage<\/strong>: \u5728\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u65f6\u8c03\u7528\u3002<\/li>\n<li><strong>onClose<\/strong>: \u5728\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5\u65f6\u8c03\u7528\u3002<\/li>\n<li><strong>onError<\/strong>: \u5728\u51fa\u73b0\u9519\u8bef\u65f6\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p><strong>5. \u53d1\u9001\u6d88\u606f<\/strong><\/p>\n<p>\u4f7f\u7528 <code>$this-&gt;send()<\/code> \u65b9\u6cd5\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4e3a\u4e86\u5c55\u793a\u4e00\u4e2a\u7b80\u5355\u7684 WebSocket \u5e94\u7528\u7a0b\u5e8f\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u804a\u5929\u5ba4\uff1a<\/p>\n<p><strong>\u524d\u7aef\uff08HTML\uff09\uff1a<\/strong><\/p>\n<pre>&lt;div id=\"chat\"&gt;\n  &lt;input id=\"message\" type=\"text\" \/&gt;\n  &lt;button id=\"send-button\"&gt;Send&lt;\/button&gt;\n  &lt;ul id=\"messages\"&gt;&lt;\/ul&gt;\n&lt;\/div&gt;<\/pre>\n<p><strong>JavaScript\uff1a<\/strong><\/p>\n<pre>const socket = new WebSocket('ws:\/\/localhost:8080');\n\nsocket.onmessage = (event) =&gt; {\n  const message = JSON.parse(event.data);\n  $('#messages').append(`&lt;li&gt;${message.username}: ${message.body}&lt;\/li&gt;`);\n};\n\n$('#send-button').click(() =&gt; {\n  const message = {\n    username: 'user',\n    body: $('#message').val()\n  };\n\n  socket.send(JSON.stringify(message));\n  $('#message').val('');\n});<\/pre>\n<p><strong>WebSocket \u5e94\u7528\u7a0b\u5e8f\uff08PHP\uff09\uff1a<\/strong><\/p>\n<pre>use Ratchet\\MessageComponentInterface;\nuse Ratchet\\ConnectionInterface;\n\nclass ChatApplication implements MessageComponentInterface\n{\n    protected $clients;\n\n    public function __construct()\n    {\n        $this-&gt;clients = new \\SplObjectStorage;\n    }\n\n    public function onOpen(ConnectionInterface $conn)\n    {\n        \/\/ Add the new connection to the list of clients\n        $this-&gt;clients-&gt;attach($conn);\n    }\n\n    public function onMessage(ConnectionInterface $from, $msg)\n    {\n        $message = json_decode($msg);\n\n        \/\/ Broadcast the message to all other clients\n        foreach ($this-&gt;clients as $client) {\n            if ($client !== $from) {\n                $client-&gt;send(json_encode($message));\n            }\n        }\n    }\n\n    public function onClose(ConnectionInterface $conn)\n    {\n        \/\/ Remove the connection from the list of clients\n        $this-&gt;clients-&gt;detach($conn);\n    }\n\n    public function onError(ConnectionInterface $conn, \\Exception $e)\n    {\n        \/\/ Log the error and close the connection\n        echo $e-&gt;getMessage() . PHP_EOL;\n        $conn-&gt;close();\n    }\n}<\/pre>\n<p><strong>\u8fd0\u884c Websocket \u670d\u52a1\u5668<\/strong><\/p>\n<p>\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u8fd0\u884c WebSocket \u670d\u52a1\u5668\uff1a<\/p>\n<pre>php websocket-server.php<\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong>\u786e\u4fdd\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u5728\u540c\u4e00\u673a\u5668\u4e0a\u8fd0\u884c\u6216\u5728\u9632\u706b\u5899\u4e2d\u5141\u8bb8\u8be5\u7aef\u53e3\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8ephp\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aWebSocket\u7f16\u7a0b\u8be6\u89e3\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aWebSocket\u7f16\u7a0b\u8be6\u89e3 \u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aphp\u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aWebSocket\u7f16\u7a0b\u8be6\u89e3\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 WebSocket \u662f\u4e00\u79cd\u5141\u8bb8\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u901a\u8fc7\u5355\u4e2a TCP \u8fde\u63a5\u8fdb\u884c\u5168\u53cc\u5de5\u901a\u4fe1\u7684\u534f\u8bae\u3002\u4f7f\u7528 PHP \u5f00\u53d1 WebSocket \u5e94\u7528\u7a0b\u5e8f\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u5b89\u88c5 Ratchet Pawl \u5e93\u3002\u521b\u5efa WebSocket \u670d\u52a1\u5668\uff0c\u4f7f\u7528 Ratchet Pawl \u5e93\u5e76\u5b9e\u73b0 WebSocket \u5e94\u7528\u7a0b\u5e8f\u7c7b\u3002\u5b9e\u73b0 onOpen\u3001onMessage\u3001onClose \u548c onError \u65b9\u6cd5\u6765\u5904\u7406\u8fde\u63a5\u548c\u6d88\u606f\u3002\u4f7f\u7528 send() \u65b9\u6cd5\u5411\u5ba2\u6237\u7aef\u53d1\u9001\u6d88\u606f\u3002\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u804a\u5929\u5ba4\u6f14\u793a WebSocket \u5e94\u7528\u7a0b\u5e8f\u7684\u5b9e\u6218\u6848\u4f8b\u3002 PHP \u7f51\u7edc\u7f16\u7a0b\u6307\u5357\uff1aWebSocket \u7f16\u7a0b\u8be6\u89e3 WebSocket \u662f\u4e00\u79cd\u5168\u53cc\u5de5\u901a\u4fe1\u534f\u8bae\uff0c\u5141\u8bb8\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u5728\u5355\u4e2a TCP \u8fde\u63a5\u4e0a\u8fdb\u884c\u5b9e\u65f6\u901a\u4fe1\u3002\u4e0b\u9762\u662f\u4f7f\u7528 PHP \u7f16\u5199 WebSocket \u5e94\u7528\u7a0b\u5e8f\u7684\u5206\u6b65\u6307\u5357\u3002 1. \u5b89\u88c5\u4f9d\u8d56 composer require ratchet\/pawl 2. \u521b\u5efa WebSocket \u670d\u52a1\u5668 \u4f7f\u7528 Ratchet Pawl \u5e93\u521b\u5efa WebSocket \u670d\u52a1\u5668\uff1a use [&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-47922","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47922","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=47922"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47922\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}