{"id":96,"date":"2019-11-28T15:14:16","date_gmt":"2019-11-28T07:14:16","guid":{"rendered":"http:\/\/blog.yuekegu.com\/?p=96"},"modified":"2019-11-28T15:14:16","modified_gmt":"2019-11-28T07:14:16","slug":"%e5%88%a9%e7%94%a8swoole%e5%ae%9e%e7%8e%b0phpwebsocket%e7%9b%b4%e6%92%ad%ef%bc%8c%e5%8d%b3%e4%bd%bf%e9%80%9a%e8%ae%af%e4%bb%a3%e7%a0%81%ef%bc%8c%e5%8f%8alinux%e4%b8%8bswoole%e5%ae%89%e8%a3%85","status":"publish","type":"post","link":"https:\/\/book.yuekegu.com\/index.php\/2019\/11\/28\/%e5%88%a9%e7%94%a8swoole%e5%ae%9e%e7%8e%b0phpwebsocket%e7%9b%b4%e6%92%ad%ef%bc%8c%e5%8d%b3%e4%bd%bf%e9%80%9a%e8%ae%af%e4%bb%a3%e7%a0%81%ef%bc%8c%e5%8f%8alinux%e4%b8%8bswoole%e5%ae%89%e8%a3%85\/","title":{"rendered":"\u5229\u7528Swoole\u5b9e\u73b0PHP+websocket\u76f4\u64ad\uff0c\u5373\u4f7f\u901a\u8baf\u4ee3\u7801\uff0c\u53calinux\u4e0bswoole\u5b89\u88c5\u57fa\u672c\u914d\u7f6e"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"php\u5b89\u88c5swoole\">php\u5b89\u88c5swoole<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-\u4e0b\u8f7dswoole\u5b89\u88c5\">1. \u4e0b\u8f7dswoole\u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>wget http:\/\/pecl.php.net\/get\/swoole-1.9.1.tgz\ntar -zxvf swoole-1.9.1.tgz\ncd swoole-1.9.1\nphpize\n.\/configure\nmake\nmake install<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-\u5728phpini\u6dfb\u52a0swooleso\">2. \u5728php.ini\u6dfb\u52a0swoole.so<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">extension=swoole.so<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">php -m\u67e5\u770b\u662f\u5426\u5b89\u88c5\u6210\u529f<\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"773\" src=\"http:\/\/book.yuekegu.com\/wp-content\/uploads\/2019\/11\/1183495-20170716193213910-13872163621.png\" alt=\"\" class=\"wp-image-97\" srcset=\"https:\/\/book.yuekegu.com\/wp-content\/uploads\/2019\/11\/1183495-20170716193213910-13872163621.png 500w, https:\/\/book.yuekegu.com\/wp-content\/uploads\/2019\/11\/1183495-20170716193213910-13872163621-194x300.png 194w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u73af\u5883\u4f9d\u8d56<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4ec5\u652f\u6301Linux\uff0cFreeBSD\uff0cMacOS\uff0c3\u7c7b\u64cd\u4f5c\u7cfb\u7edf<\/li><li>Linux\u5185\u6838\u7248\u672c2.3.32\u4ee5\u4e0a<\/li><li>PHP5.3.10\u4ee5\u4e0a\u7248\u672c<\/li><li>gcc4.4\u4ee5\u4e0a\u7248\u672c\u6216\u8005clang<\/li><li>cmake2.4+\uff0c\u7f16\u8bd1\u4e3alibswoole.so\u4f5c\u4e3aC\/C++\u5e93\u65f6\u9700\u8981\u4f7f\u7528cmake&nbsp;<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">PHP\u7248\u672c\u4f9d\u8d56<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>swoole\u4ec5\u652f\u6301PHP5.3.10\u6216\u66f4\u9ad8\u7248\u672c\uff0c\u5efa\u8bae\u4f7f\u7528PHP5.4+<\/li><li>swoole\u4e0d\u4f9d\u8d56php\u7684stream\u3001sockets\u3001pcntl\u3001posix\u3001sysvmsg\u7b49\u6269\u5c55\u3002PHP\u53ea\u9700\u5b89\u88c5\u6700\u57fa\u672c\u7684\u6269\u5c55\u5373\u53ef&nbsp;<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">PHP\u76f4\u64ad\u4ee3\u7801<\/h1>\n\n\n\n<p>1.start.php \u4f7f\u7528\u65f6\u9700\u8981\u5f00\u542f\uff0c\u670d\u52a1\u5668\u8f93\u5165\uff08php start.php\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/php\u5728\u7ebf\u76f4\u64ad\u793a\u4f8b\u4ee3\u7801\n\/\/\u4f7f\u7528PHPCLI\u6a21\u5f0f\u8fd0\u884c\n\/\/\u547d\u4ee4\uff1aphp start.php\n\n\/\/\u8bbe\u7f6e\u8def\u5f84\ndefine('_ROOT_', dirname(__FILE__));\nrequire_once _ROOT_.'\/function.php';\n\/\/\u76d1\u542c\u5730\u5740\u548c\u7aef\u53e3\n$server = new swoole_websocket_server(\"0.0.0.0\uff08\u8fd9\u91cc\u5c31\u662f\u56db\u4e2a0\uff0c\u4e0d\u8981\u6539\uff09\", 8888);\n\/\/\u670d\u52a1\u7aef\u63a5\u6536\u8fde\u63a5\u4e8b\u4ef6\n$server->on('open', function (swoole_websocket_server $server, $request) {\n    if(!file_exists(_ROOT_.'\/client\/'.$request->fd.'.client')){\n        @file_put_contents(_ROOT_.'\/client\/'.$request->fd.'.client',$request->fd);\n    }\n});\n\/\/\u670d\u52a1\u7aef\u63a5\u6536\u4fe1\u606f\u4e8b\u4ef6\n$server->on('message', function (swoole_websocket_server $server, $frame) {\n    foreach(notice(_ROOT_.'\/client\/') as $v){\n            $server->push($v,$frame->data);\n    }\n});\n\/\/\u670d\u52a1\u7aef\u63a5\u6536\u5173\u95ed\u4e8b\u4ef6\n$server->on('close', function ($ser, $fd) {\n    @unlink(_ROOT_.'\/client\/'.$fd.'.client');\n});\n\/\/\u670d\u52a1\u5f00\u542f\n$server->start();<\/code><\/pre>\n\n\n\n<p> 2.index.html \u76f4\u64ad\u9875\u9762\uff0c\u8bbf\u95ee\u8be5\u9875\u9762\u89c2\u770b\u76f4\u64ad <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\n&lt;html>\n&lt;head lang=\"en\">\n    &lt;meta charset=\"UTF-8\">\n    &lt;title>\u5728\u7ebf\u76f4\u64ad\u754c\u9762&lt;\/title>\n&lt;\/head>\n&lt;body>\n&lt;img id=\"receiver\" style='width:640px;height:480px'\/>\n&lt;script type=\"text\/javascript\" charset=\"utf-8\">\n    var ws = new WebSocket(\"ws:\/\/\u6539\u6210\u81ea\u5df1\u670d\u52a1\u5668ip:8888\");\n    var image = document.getElementById('receiver');\n    ws.onopen = function(){\n\n    }\n    ws.onmessage = function(data)\n    {\n        image.src=data.data;\n    }\n&lt;\/script>\n&lt;\/body>\n&lt;\/html><\/code><\/pre>\n\n\n\n<p> 3.rec.html\u4e3b\u64ad\u5f55\u5236\u9875\u9762\uff0c\u8bbf\u95ee\u8be5\u9875\u9762\u8fdb\u884c\u76f4\u64ad\u5f55\u5236 <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\n&lt;html>\n&lt;head lang=\"en\">\n    &lt;meta charset=\"UTF-8\">\n    &lt;title>\u4e3b\u64ad\u5f55\u5236\u754c\u9762&lt;\/title>\n&lt;\/head>\n&lt;body>\n&lt;video id=\"video\" autoplay=\"\" style='width:640px;height:480px'>&lt;\/video>\n&lt;canvas id=\"output\" style=\"display:none\">&lt;\/canvas>\n&lt;script type=\"text\/javascript\" charset=\"utf-8\">\n    var ws = new WebSocket(\"ws:\/\/\u81ea\u5df1\u670d\u52a1\u5668ip:8888\");\n    var back = document.getElementById('output');\n    var backcontext = back.getContext('2d');\n    var video = document.getElementById(\"video\");\n    var success = function(stream){\n        video.src = window.URL.createObjectURL(stream);\n    }\n    ws.onopen = function(){\n        draw();\n    }\n    var draw = function(){\n        try{\n            backcontext.drawImage(video,0,0, back.width, back.height);\n        }catch(e){\n            if (e.name == \"NS_ERROR_NOT_AVAILABLE\") {\n                return setTimeout(draw, 100);\n            } else {\n                throw e;\n            }\n        }\n        if(video.src){\n            ws.send(back.toDataURL(\"image\/jpeg\", 0.5));\n        }\n        setTimeout(draw, 100);\n    }\n    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||\n    navigator.mozGetUserMedia || navigator.msGetUserMedia;\n    navigator.getUserMedia({video:true, audio:false}, success, console.log);\n&lt;\/script>\n&lt;\/body>\n&lt;\/html><\/code><\/pre>\n\n\n\n<p>\n\n4.function.php \u7edf\u8ba1\u6570\u636e\u9875\u9762\n\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/\u7edf\u8ba1\u5728\u7ebf\u4eba\u6570\nfunction clearDir($dir)\n{\n    $n = 0;\n    if ($dh = opendir($dir))\n    {\n        while (($file = readdir($dh)) !== false)\n        {\n            if ($file == '.' or $file == '..')\n            {\n                continue;\n            }\n            if (is_file($dir . $file)) {\n                $n++;\n            }\n        }\n    }\n    closedir($dh);\n    return $n;\n}\n\n\/\/\u901a\u77e5\u5728\u7ebf\u7684\u4eba\nfunction notice($dir){\n    if ($dh = opendir($dir))\n    {\n        while (($file = readdir($dh)) !== false)\n        {\n            if ($file == '.' or $file == '..')\n            {\n                continue;\n            }\n            if (is_file($dir . $file)) {\n                $array[]=file_get_contents($dir.$file);\n            }\n        }\n    }\n    closedir($dh);\n    return $array;\n}<\/code><\/pre>\n\n\n\n<p> 5.\u5728\u540c\u7ea7\u76ee\u5f55\u4e0b\u5efa\u7acbclient\u6587\u4ef6\uff0c\u5b58\u653e\u4fe1\u606f <\/p>\n\n\n\n<h1 class=\"wp-block-heading\">PHP \u5373\u4f7f\u901a\u8baf<\/h1>\n\n\n\n<p>1.socket.php \u4e00\u6837\uff0c\u4f7f\u7528\u65f6\u9700\u8981\u5f00\u542f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n    \/\/\u521b\u5efawebsocket\u670d\u52a1\u5668\u5bf9\u8c61\uff0c\u76d1\u542c0.0.0.0:9502\u7aef\u53e3\n    $ws = new swoole_websocket_server(\"0.0.0.0\", 9502);\n\n    \/\/\u76d1\u542cWebSocket\u8fde\u63a5\u6253\u5f00\u4e8b\u4ef6\n    $ws->on('open', function ($ws, $request) {\n        $fd[] = $request->fd;\n        $GLOBALS['fd'][] = $fd;\n        \/\/$ws->push($request->fd, \"hello, welcome\\n\");\n    });\n\n    \/\/\u76d1\u542cWebSocket\u6d88\u606f\u4e8b\u4ef6\n    $ws->on('message', function ($ws, $frame) {\n        $msg =  'from'.$frame->fd.\":{$frame->data}\\n\";\n    \/\/var_dump($GLOBALS['fd']);\n    \/\/exit;\n        foreach($GLOBALS['fd'] as $aa){\n            foreach($aa as $i){\n                $ws->push($i,$msg);\n            }\n        }\n       \/\/ $ws->push($frame->fd, \"server: {$frame->data}\");\n        \/\/ $ws->push($frame->fd, \"server: {$frame->data}\");\n    });\n\n    \/\/\u76d1\u542cWebSocket\u8fde\u63a5\u5173\u95ed\u4e8b\u4ef6\n    $ws->on('close', function ($ws, $fd) {\n        echo \"client-{$fd} is closed\\n\";\n    });\n\n    $ws->start();<\/code><\/pre>\n\n\n\n<p>\n\n2.socket.html\u804a\u5929\u9875\u9762\n\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n&lt;head>\n    &lt;meta charset=\"UTF-8\">\n    &lt;title>Title&lt;\/title>\n&lt;\/head>\n&lt;body>\n&lt;div id=\"msg\">&lt;\/div>\n&lt;input type=\"text\" id=\"text\">\n&lt;input type=\"submit\" value=\"\u53d1\u9001\u6570\u636e\" onclick=\"song()\">\n&lt;\/body>\n&lt;script>\n    var msg = document.getElementById(\"msg\");\n    var wsServer = 'ws:\/\/60.205.208.176:9502';\n    \/\/\u8c03\u7528websocket\u5bf9\u8c61\u5efa\u7acb\u8fde\u63a5\uff1a\n    \/\/\u53c2\u6570\uff1aws\/wss(\u52a0\u5bc6)\uff1a\/\/ip:port \uff08\u5b57\u7b26\u4e32\uff09\n    var websocket = new WebSocket(wsServer);\n    \/\/onopen\u76d1\u542c\u8fde\u63a5\u6253\u5f00\n    websocket.onopen = function (evt) {\n        \/\/websocket.readyState \u5c5e\u6027\uff1a\n        \/*\n        CONNECTING    0    The connection is not yet open.\n        OPEN    1    The connection is open and ready to communicate.\n        CLOSING    2    The connection is in the process of closing.\n        CLOSED    3    The connection is closed or couldn't be opened.\n        *\/\n        msg.innerHTML = websocket.readyState;\n    };\n\n    function song(){\n        var text = document.getElementById('text').value;\n        document.getElementById('text').value = '';\n        \/\/\u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\n        websocket.send(text);\n    }\n      \/\/\u76d1\u542c\u8fde\u63a5\u5173\u95ed\n\/\/    websocket.onclose = function (evt) {\n\/\/        console.log(\"Disconnected\");\n\/\/    };\n\n    \/\/onmessage \u76d1\u542c\u670d\u52a1\u5668\u6570\u636e\u63a8\u9001\n    websocket.onmessage = function (evt) {\n        msg.innerHTML += evt.data +'&lt;br>';\n\/\/        console.log('Retrieved data from server: ' + evt.data);\n    };\n\/\/\u76d1\u542c\u8fde\u63a5\u9519\u8bef\u4fe1\u606f\n\/\/    websocket.onerror = function (evt, e) {\n\/\/        console.log('Error occured: ' + evt.data);\n\/\/    };\n\n&lt;\/script>\n&lt;\/html><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>php\u5b89\u88c5swoole 1. \u4e0b\u8f7dswoole\u5b89\u88c5 2. \u5728php.ini\u6dfb\u52a0swoole.so extension=swoole.so php -m\u67e5\u770b\u662f\u5426\u5b89\u88c5\u6210\u529f \u73af\u5883\u4f9d\u8d56 \u4ec5\u652f\u6301Linux\uff0cFreeBSD\uff0cMacOS\uff0c3\u7c7b\u64cd\u4f5c\u7cfb\u7edf Linux\u5185\u6838\u7248\u672c2.3.32\u4ee5\u4e0a PHP5.3.10\u4ee5\u4e0a\u7248\u672c gcc4.4\u4ee5\u4e0a\u7248\u672c\u6216\u8005clang cmake2.4+\uff0c\u7f16\u8bd1\u4e3alibswoole.so\u4f5c\u4e3aC\/C++\u5e93\u65f6\u9700\u8981\u4f7f\u7528cmake&nbsp; PHP\u7248\u672c\u4f9d\u8d56 swoole\u4ec5\u652f\u6301PHP5.3.10\u6216\u66f4\u9ad8\u7248\u672c\uff0c\u5efa\u8bae\u4f7f\u7528PHP5.4+ swoole\u4e0d\u4f9d\u8d56php\u7684stream\u3001sockets\u3001pcntl\u3001posix\u3001sysvmsg\u7b49\u6269\u5c55\u3002PHP\u53ea\u9700\u5b89\u88c5\u6700\u57fa\u672c\u7684\u6269\u5c55\u5373\u53ef&nbsp; PHP\u76f4\u64ad\u4ee3\u7801 1.start.php \u4f7f\u7528\u65f6\u9700\u8981\u5f00\u542f\uff0c\u670d\u52a1\u5668\u8f93\u5165\uff08php start.php\uff09 2.index.html \u76f4\u64ad\u9875\u9762\uff0c\u8bbf\u95ee\u8be5\u9875\u9762\u89c2\u770b\u76f4\u64ad 3.rec.html\u4e3b\u64ad\u5f55\u5236\u9875\u9762\uff0c\u8bbf\u95ee\u8be5\u9875\u9762\u8fdb\u884c\u76f4\u64ad\u5f55\u5236 4.function.php \u7edf\u8ba1\u6570\u636e\u9875\u9762 5.\u5728\u540c\u7ea7\u76ee\u5f55\u4e0b\u5efa\u7acbclient\u6587\u4ef6\uff0c\u5b58\u653e\u4fe1\u606f PHP \u5373\u4f7f\u901a\u8baf 1.socket.php \u4e00\u6837\uff0c\u4f7f\u7528\u65f6\u9700\u8981\u5f00\u542f 2.socket.html\u804a\u5929\u9875\u9762<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-22"],"_links":{"self":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":0,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"wp:attachment":[{"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/book.yuekegu.com\/index.php\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}