php安装swoole

  1. 文档地址
  2. 韩天峰blog
  3. 阮一峰非PHP方案

  4. 安装前的准备

     yum install \
     git \
     gcc \
     gcc-c++ \
     wget \
     make \
     automake \
     autoconf \
     pcre-devel \
     -y \
     # 如果启用async-redis (--enable-async-redis)
     git clone https://github.com/redis/hiredis.git
     cd ./hiredis
     make
     make install
     # 如果启用http2 (--enable-http2)
     wget https://github.com/nghttp2/nghttp2/releases/download/v1.25.0/nghttp2-1.25.0.tar.gz
     tar -zxvf nghttp2-1.25.0.tar.gz
     cd nghttp2-1.25.0
     ./configure
     make
     make install
    
  5. 下载swoole安装 参考链接 编译参数

     wget http://pecl.php.net/get/swoole-1.9.1.tgz
     tar -zxvf swoole-1.9.1.tgz
     cd swoole-1.9.1
     phpize
    
     ./configure \
    
     make
     make install
    
  6. 在php.ini添加swoole.so

     extension=swoole.so
    
  7. 聊天功能

  8. 聊天功能,php文件

     # 原生写法
     <?php
     function on_open(swoole_websocket_server $server, $request)
     {
         foreach ($server->connections as $fd) {
             //新接入用户客户端id:$request->fd
             //消息接收者客户端id:$fd
             $data = '系统提示:有新用户加入';
             $server->push($fd, $data);
         }
     }
    
     function on_message(swoole_websocket_server $server, $request)
     {
         $data = $request->data;
         foreach ($server->connections as $fd) {
             //消息发送者客户端id:$request->fd
             //消息接收者客户端id:$fd
             $server->push($fd, $data);
         }
     }
    
     function on_close($server, $fd)
     {
         //退出用户客户端id:$fd
     }
    
     $swoole_talk = new SwooleTalk();
     $server = new swoole_websocket_server("0.0.0.0", 1234);
     $server->on('open', 'on_open');
     $server->on('message', 'on_message');
     $server->on('close', 'on_close');
     $server->start();
    
     # 匿名函数(闭包)写法
     <?php
     $server = new swoole_websocket_server("0.0.0.0", 1234);
     $server->on('open', function (swoole_websocket_server $server, $request) {
         foreach ($server->connections as $fd) {
             //新接入用户客户端id:$request->fd
             //消息接收者客户端id:$fd
             $data = '系统提示:有新用户加入';
             $server->push($fd, $data);
         }
     });
     $server->on('message', function (swoole_websocket_server $server, $frame) {
         $data = $request->data;
         foreach ($server->connections as $fd) {
             //消息发送者客户端id:$request->fd
             //消息接收者客户端id:$fd
             $server->push($fd, $data);
         }
     });
     $server->on('close', function ($server, $fd) {
         //退出用户客户端id:$fd
     });
     $server->start();
    
     # 面向对象写法
     <?php
     class SwooleTalk
     {
         public function onOpen(swoole_websocket_server $server, $request)
         {
             foreach ($server->connections as $fd) {
                 //新接入用户客户端id:$request->fd
                 //消息接收者客户端id:$fd
                 $data = '系统提示:有新用户加入';
                 $server->push($fd, $data);
             }
         }
    
         public function onMessage(swoole_websocket_server $server, $request)
         {
             $data = $request->data;
             foreach ($server->connections as $fd) {
                 //消息发送者客户端id:$request->fd
                 //消息接收者客户端id:$fd
                 $server->push($fd, $data);
             }
         }
    
         public function onClose($server, $fd)
         {
             //退出用户客户端id:$fd
         }
     }
     $swoole_talk = new SwooleTalk();
     $server = new swoole_websocket_server("0.0.0.0", 1234);
     $server->on('open', array($swoole_talk, 'onOpen'));
     $server->on('message', array($swoole_talk, 'onMessage'));
     $server->on('close', array($swoole_talk, 'onClose'));
     $server->start();
    
  9. 聊天功能,html文件

     <!DOCTYPE html>
     <html>
     <head>
         <title></title>
         <meta charset="UTF-8">
         <script type="text/javascript">
             var str = '';
             var socketTalk = new WebSocket("ws://[ip]:1234");
             socketTalk.onopen = function (event) {};
             socketTalk.onmessage = function (event) {
                 str = str + '<p>' + event.data + '</p>';
                 console.log(event.data);
                 document.getElementById("talk").innerHTML = str;
             }
         </script>
     </head>
     <body>
     <div id="talk"></div>
     <input type="text" id="content">
     <button onclick="socketTalk.send( document.getElementById('content').value )">发送</button>
     </body>
     </html>
    
  10. 打开防火墙1234端口

     firewall-cmd --zone=public --add-port=1234/tcp --permanent
     systemctl restart firewalld
    
  11. 启动swoole服务

     php /usr/local/nginx-1.11.5/html/index.php
    
  12. 官方文档

@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2018-05-23 16:25:59

results matching ""

    No results matching ""