首页 > 后端开发 > php教程 > swoole一

swoole一

WBOY
发布: 2016-07-29 09:04:19
原创
1154 人浏览过

公司业务中,聊天系统用的是 swoole 框架,这个框架是c语言写的php扩展,使用起来也很方便!

1 安装 过程很简单(不做介绍)
2 来看看 从官网文档copy 过来的例子,我把注释写详细了

<code><span>// Server</span><span><span>class</span><span>Server</span>
{</span><span>private</span><span>$serv</span>;
<span>//构造函数</span><span>public</span><span><span>function</span><span>__construct</span><span>()</span> {</span><span>//新建一个对象,接收所有的ip链接,端口设置为9501</span><span>$this</span>->serv = <span>new</span> swoole_server(<span>"0.0.0.0"</span>, <span>9501</span>);
        <span>$this</span>->serv->set(<span>array</span>(
            <span>'worker_num'</span> => <span>8</span>, <span>//设置work进程的数量为8</span><span>'daemonize'</span> => <span>false</span>,<span>//设置为后台进程</span><span>'max_request'</span> => <span>10000</span>, <span>//每个worker进程允许处理的最大任务数</span><span>'dispatch_mode'</span> => <span>2</span>,
            <span>'debug_mode'</span>=> <span>1</span>
        ));

        <span>$this</span>->serv->on(<span>'Start'</span>, <span>array</span>(<span>$this</span>, <span>'onStart'</span>));<span>//设置回调函数 onstart</span><span>$this</span>->serv->on(<span>'Connect'</span>, <span>array</span>(<span>$this</span>, <span>'onConnect'</span>));
        <span>$this</span>->serv->on(<span>'Receive'</span>, <span>array</span>(<span>$this</span>, <span>'onReceive'</span>));
        <span>$this</span>->serv->on(<span>'Close'</span>, <span>array</span>(<span>$this</span>, <span>'onClose'</span>));
        <span>$this</span>->serv->start();
    }

    <span>public</span><span><span>function</span><span>onStart</span><span>( <span>$serv</span> )</span> {</span><span>echo</span><span>"Start\n"</span>;
    }

    <span>public</span><span><span>function</span><span>onConnect</span><span>( <span>$serv</span>, <span>$fd</span>, <span>$from_id</span> )</span> {</span><span>$serv</span>->send( <span>$fd</span>, <span>"Hello {$fd}!"</span> );
    }

    <span>public</span><span><span>function</span><span>onReceive</span><span>( swoole_server <span>$serv</span>, <span>$fd</span>, <span>$from_id</span>, <span>$data</span> )</span> {</span><span>echo</span><span>"Get Message From Client {$fd}:{$data}\n"</span>;
    }

    <span>public</span><span><span>function</span><span>onClose</span><span>( <span>$serv</span>, <span>$fd</span>, <span>$from_id</span> )</span> {</span><span>echo</span><span>"Client {$fd} close connection\n"</span>;
    }
}
<span>// 启动服务器</span><span>$server</span> = <span>new</span> Server();</code>
登录后复制

A. 通过构造函数创建swoole_server对象
B. 调用set函数设置swoole_server的相关配置选项
C. 调用on函数设置相关回调函数 关于set配置选项以及on回调函数的具体说明

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了swoole一,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板