zoukankan      html  css  js  c++  java
  • php基于swoole扩展的WebSocket

    php的swoole的扩展可以实现WebSocket通信,方法如下

    1、php添加swoole扩展;

    一:两种安装方式:
    1》编译安装:
    1>wget http://pecl.php.net/get/swoole-1.9.6.tgz 或者 
        wget http://www.taikongliu.com/swoole/swoole-1.7.6-stable.tar.gz        //下载swoole    
    2>tar -zxvf swoole-1.7.6-stable.tar.gz        //解压swoole
    3>cd swoole-src-swoole-1.7.6-stable/        //进入swoole
    4>/phpstudy/server/php/bin/phpize        //生成configure
    5>./configure --with-php-config=/phpstudy/server/php/bin/php-config        //编译
    6>make && make install            //安装
    7>cd /phpstudy/server/php/lib/php/extensions/no-debug-non-zts-20121212    //查看是否安转上了swoole.so    (注意:此文件下边都是你安装的拓展)
    8>vim /phpstudy/server/php/etc/php.ini     //在php.ini添加extension=swoole.so加入到文件最后一行
    9>/phpstudy/server/nginx/sbin/nginx  -s reload    //重启nginx    
    10>查看phpinfo,这时候swoole拓展已经装上了

    2》PECL安装:
    1>pecl install swoole    //如果以上步骤一切正常的话,即表示swoole已经成功的安装了。
    2>extension=swoole.so            //成功之后,我们打开php.ini(不知道配置文件在哪的回去再把CLI看一遍),把swoole.so加入到文件最后
    3>$ php -m | grep swoole    //查看swoole是否被正确的安装

    注意:官方推荐使用PECL方式安装,如果服务端环境有两个php环境,一个是rpm包安装的,一个是编译安装,那么PECL安装应用到rpm的PHP下,遇到个坑,由于使用的是php编译安装的环境,而我用PECL安装swoole,总是在phpinfo()函数中加载不出swoole扩展

    2、服务端:创建服务端文件server.php(该文件可以放到任何目录下)

    $server = new swoole_websocket_server("0.0.0.0", 9501);
    $server->set(array(
            'worker_num' => 8,   //工作进程数量
            'daemonize' => true, //是否作为守护进程
    ));
    
    $server->on('open', function (swoole_websocket_server $server, $request) {
        echo "server: handshake success with fd{$request->fd}
    ";
    });
    
    $server->on('message', function (swoole_websocket_server $server, $frame) {
        file_put_contents('2.txt',$frame->fd.':'.$frame->data.'opcode:'.$frame->opcode.'fin:'.$frame->finish);
        echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}
    ";
        $server->push($frame->fd, "this is server");
    });
    
    $server->on('close', function ($ser, $fd) {
        echo "client {$fd} closed
    ";
    });
    
    $server->start();

    开启进程 php ./server.php   

    注意:如果daemonize 配置为true则 php ./server.php 开启进程后该进程常住系统内存,如果未开启则不能中断 php ./server.php

    3、客户端:使用html5的WebSocket请求服务端

    ws = new WebSocket("ws://192.168.152.129:9501");
    console.log(ws);
    ws.onopen = function() {  
        console.log("open");  
        ws.send("send message");  
    }  
    ws.onmessage = function(e) {  
        console.log(e.data);  
    }  
    ws.onclose = function(e) {  
        console.log("closed");  
    }  
    ws.onerror = function(e) {  
        console.log("error");  
    }  

    当ws.send的时候回调用服务端 $server->on('message', function (swoole_websocket_server $server, $frame)方法 并且返回 "this is server" 

    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    闭区间上的连续函数必定是一致连续的
    利用开区间覆盖的约简给出$\bf{Lindelöf}$覆盖定理的一个新证明
    $\mathbf{R}^n$中的紧集是闭有界集
    $\mathbf{R}^n$中的紧集是闭有界集
    陶哲轩实分析 习题10.2.7 导函数有界的函数一致连续
    $\mathbf{R}$上的离散点集是至多可数集
    利用开区间覆盖的约简给出$\bf{Lindelöf}$覆盖定理的一个新证明
    闭区间上的连续函数必定是一致连续的
    $\mathbf{R}$上的离散点集是至多可数集
    利用开区间覆盖的约简给出有限覆盖定理的一个新证明
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/7568478.html
Copyright © 2011-2022 走看看