当前位置:K88软件开发文章中心编程语言PHPSwoole → 文章内容

Swoole server函数列表

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:54:52

由 路飞 创建, 最后一次修改 2016-09-26 swoole_server函数列表Table of Contents swoole_server::__construct swoole_server::set swoole_server::on swoole_server::addlistener swoole_server::handler swoole_server::start swoole_server::reload swoole_server::shutdown swoole_server::addtimer swoole_server::deltimer swoole_server::after swoole_server::close swoole_server::send swoole_server::connection_info swoole_server::connection_list swoole_server::stats swoole_server::task swoole_server::taskwait swoole_server::finish swoole_server::heartbeat swoole_get_mysqli_sock swoole_set_process_name swoole_version swoole_strerror swoole_errno swoole_get_local_ip swoole_server::__construct功能描述:创建一个swoole_server资源对象函数原型:// 类成员函数public function swoole_server::__construct(string $host, int $port, int $mode = SWOOLE_PROCESS, int $sock_type = SWOOLE_SOCK_TCP);// 公共函数function swoole_server_create(string $host, int $port, int $mode = SWOOLE_PROCESS, int $sock_type = SWOOLE_SOCK_TCP);返回:一个swoole_server对象参数说明:参数说明string host监听的IP地址int port监听的端口号int mode运行模式int sock_type指定的socket类型说明: host、port、socket_type的详细说明见swoole_server::addlistener。mode指定了swoole_server的运行模式,共有如下三种:mode类型说明SWOOLE_BASEBase模式传统的异步非阻塞Server。在Reactor内直接回调PHP的函数。如果回调函数中有阻塞操作会导致Server退化为同步模式。worker_num参数对与BASE模式仍然有效,swoole会启动多个Reactor进程SWOOLE_THREAD线程模式(已废弃)多线程Worker模式,Reactor线程来处理网络事件轮询,读取数据。得到的请求交给Worker线程去处理。多线程模式比进程模式轻量一些,而且线程之间可以共享堆栈和资源。 访问共享内存时会有同步问题,需要使用Swoole提供的锁机制来保护数据。SWOOLE_PROCESS进程模式(默认)Swoole提供了完善的进程管理、内存保护机制。 在业务逻辑非常复杂的情况下,也可以长期稳定运行,适合业务逻辑非常复杂的场景。样例:$serv = new swoole_server("127.0.0.1" , 8888 , SWOOLE_PROCESS , SWOOLE_SOCK_TCP);swoole_server::set功能描述:设置swoole_server运行时的各项参数函数原型:// 类成员函数public function swoole_server::set(array $setting);// 公共函数function swoole_server_set(swoole_server $server, array $setting);返回:无参数说明:参数说明array setting配置选项数组,采用key-value形式说明:该函数必须在swoole_server::start函数调用前调用。全部swoole_server的配置参数点此查看样例:$serv->set( array( 'worker_num' => 8, 'max_request' => 10000, 'max_conn' => 100000, 'dispatch_mode' => 2, 'debug_mode'=> 1, 'daemonize' => false, ));swoole_server::on功能描述:绑定swoole_server的相关回调函数函数原型:// 类成员函数public function bool swoole_server->on(string $event, mixed $callback);返回:设置成功返回true,否则返回false参数说明:参数说明string event回调的名称(大小写不敏感)mixed callback回调的PHP函数,可以是函数名的字符串,类静态方法,对象方法数组,匿名函数说明:该函数必须在swoole_server::start函数调用前调用。此方法与swoole_server::handler功能相同,作用是与swoole_client风格保持一致。swoole_server::on中事件名称字符串不要加on。全部的回调函数列表点此查看样例:$serv->on('connect', function ($serv, $fd){ echo "Client:Connect.\n";});$serv->on('receive', array( $myclass, 'onReceive' ) ); // onReceive是myclass的成员函数swoole_server::addlistener功能描述:给swoole_server增加一个监听的地址和端口函数原型:// 类成员函数public function swoole_server::addlistener(string $host, int $port, $type = SWOOLE_SOCK_TCP);// 公共函数function swoole_server_addlisten(swoole_server $serv, string $host, int $port, $type = SWOOLE_SOCK_TCP);返回:无参数说明:参数说明string host监听的IP地址int port监听的端口号int sock_type指定的socket类型说明: swoole支持如下socket类型:sock_type说明SWOOLE_TCP/SWOOLE_SOCK_TCPTCP IPv4 SocketSWOOLE_TCP6/SWOOLE_SOCK_TCP6TCP IPv6 SocketSWOOLE_UDP/SWOOLE_SOCK_UDPUDP IPv4 SocketSWOOLE_UDP6/SWOOLE_SOCK_UDP6UDP IPv4 SocketSWOOLE_UNIX_DGRAMUnix UDP SocketSWOOLE_UNIX_STREAMUnix TCP SocketUnix Socket仅在1.7.1+后可用,此模式下host参数必须填写可访问的文件路径,port参数忽略Unix Socket模式下,客户端fd将不再是数字,而是一个文件路径的字符串SWOOLE_TCP等是1.7.0+后提供的简写方式,与1.7.0前的SWOOLE_SOCK_TCP是等同的样例:$serv->addlistener("127.0.0.1", 9502, SWOOLE_SOCK_TCP);$serv->addlistener("192.168.1.100", 9503, SWOOLE_SOCK_TCP);$serv->addlistener("0.0.0.0", 9504, SWOOLE_SOCK_UDP);$serv->addlistener("/var/run/myserv.sock", 0, SWOOLE_UNIX_STREAM);swoole_server_addlisten($serv, "127.0.0.1", 9502, SWOOLE_SOCK_TCP);swoole_server::handler功能描述:设置Server的事件回调函数函数原型:// 类成员函数public function swoole_server::handler(string $event_name, mixed $event_callback_function);// 公共函数function swoole_server_handler(swoole

[1] [2] [3] [4]  下一页


Swoole server函数列表