swoole扩展怎么用?是php扩展还是框架
swoole扩展是PHP扩展。php swoole扩展,PHP语言的高性能网络通信框架,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异步DNS查询。
1、下载swoole源码包
[root@nginx ~]# wget https://codeload.github.com/swoole/swoole-src/tar.gz/swoole-1.7.17-stable
2、解压进入swoole文件夹
[root@nginx ~]# tar -zxvf swoole-1.7.17-stable
[root@nginx ~]# cd swoole-src-swoole-1.7.17-stable/
3、编译安装swoole
[root@nginx swoole-src-swoole-1.7.17-stable]# phpize
[root@nginx swoole-src-swoole-1.7.17-stable]# ./configure
[root@nginx swoole-src-swoole-1.7.17-stable]# make && make install
4、php.ini配置文件加载swoole.so模块
[root@nginx swoole-src-swoole-1.7.17-stable]# vi /usr/local/php/lib/php.ini
注意 php命令行运行和浏览器运行的配置文件不一样。
php 命令行的配置:
[root@nginx swoole-src-swoole-1.7.17-stable]# php --ini
Configuration File (php.ini) Path: /usr/local/lib
Loaded Configuration File: /usr/local/lib/php.ini//配置文件
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
5、查看swoole模块是否已经安装成功
[root@nginx swoole-src-swoole-1.7.17-stable]# php -m
6、编写服务端httpServer.php文件并运行
$serv = new swoole_server("127.0.0.1", 9501);
$serv->on('connect', function ($serv, $fd){
echo "Client:Connect.\n";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
$serv->send($fd, 'Swoole: '.$data);
});
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
$serv->start();
运行httpServer.php
[root@nginx swoole-src-swoole-1.7.17-stable]# php httpServer.php
7、用telnet测试
[root@nginx ~]# telnet 127.0.0.1 9501
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello 客户端
Swoole: hello 服务端
来源:PHP swoole扩展安装和使用-http://www.leixuesong.cn/498