swoole的安装与简单应用
安装swoole
下载swoole包
wget https://github.com/swoole/swoole-src/archive/swoole-1.7.6-stable.tar.gz
解压
tar zxvf swoole-1.7.6-stable.tar.gz
进入目录文件
cd swoole-1.7.6-stable
准备扩展安装编译环境
phpize
配置:(–with-php-config==后面是你自己的php-config位置)
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
php加载swoole扩展
在php配置文件加入 extension=swoole.so
重启服务
service php-fpm restart
service nginx restart
检查phpinfo()出现swoole,则安装成功
基本使用
1.创建TCP服务 server.php
2.启动TCP服务
php server.php
查看9501端口已被监听:netstat -an | grep 9501
使用telnet连接TCP服务,输入hello,服务器返回hello即测试成功:
(如果telnet工具没有安装,执行yum install telnet 、yum install telnet-server)
3.写一个TCP客户端连接TCP服务器端:tcp_client.php
执行它
表示连接成功
4.创建udp服务器 udp_server.php 并运行 php udp_server.php
使用netcat连接UDP服务,输入hello,服务器返回hello即测试成功(CentOS):
(如果没有安装netcat监听器,执行yum install -y nc)
5.创建Web服务器 http_server.php
6.启动服务,php http_server.php,浏览器访问127.0.0.1:9501
7.创建WebSocket服务器 ws_server.php:并运行:php ws_server.php
8.前端页面js监听:web.html(127.0.0.1改成你的主机地址)
9.访问它,
服务器端返回连接参数:
以上就是swoole的简单应用