swoole一

WBOY
WBOYOriginal
2016-07-29 09:04:191156Durchsuche

公司业务中,聊天系统用的是 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教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn