PHP swoole扩展安装和使用

  • 时间:
  • 浏览:80

PHP swoole扩展安装和使用

PHPPHP编程

php swoole扩展,PHP语言的高性能网络通信框架,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,数据库连接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异步DNS查询。

1、下载swoole源码包

1
[root@nginx ~]# wget https://codeload.github.com/swoole/swoole-src/tar.gz/swoole-1.7.17-stable

2、解压进入swoole文件夹

1
2
[root@nginx ~]# tar -zxvf swoole-1.7.17-stable
[root@nginx ~]# cd swoole-src-swoole-1.7.17-stable/

3、编译安装swoole

1
2
3
[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模块

1
[root@nginx swoole-src-swoole-1.7.17-stable]# vi /usr/local/php/lib/php.ini

注意 php命令行运行和浏览器运行的配置文件不一样。

php 命令行的配置:

1
2
3
4
5
[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)

php浏览器运行配置文件路径:

5、查看swoole模块是否已经安装成功

1
[root@nginx swoole-src-swoole-1.7.17-stable]# php -m

6、编写服务端httpServer.php文件并运行

1
2
3
4
5
6
7
8
9
10
11
$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

1
[root@nginx swoole-src-swoole-1.7.17-stable]# php httpServer.php

7、用telnet测试

1
2
3
4
5
6
[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 服务端

swoole官网:http://www.swoole.com/