您的位置:首页 > 其它

swoole新手教程01-环境搭建及扩展安装

2017-04-29 12:13 495 查看


写在前面的废话

《swoole源代码分析》已经写了13章,整个swoole的核心架构基本都分析的差点儿相同了。于是心里一直以来想整理swoole的文档并写一份教程的想法就再度浮了出来。

实话说,我接触swoole乃至接触PHP都仅有9个月的时间。而自7月份以来一直在公司做Android开发。也有没有了使用swoole的机会。所以,如今我仅仅能写出一份入门级教程,帮助刚刚接触swoole的人理解和使用swoole写一些简单的样例。从而初步掌握-swoole的使用方法。

Git地址:https://github.com/LinkedDestiny/swoole-doc


第一章
环境搭建及扩展安装

环境说明: 系统:Ubuntu14.04 (安装教程包含CentOS6.5)

PHP版本号:PHP-5.5.10

swoole版本号:1.7.6-stable


PHP安装

要用swoole。首先须要有PHP环境。因为swoole的某些特性,最好是可以从源代码编译安装PHP。这样在使用过程中可以避免非常多不必要的错误。

PHP下载地址:http://php.net/

在这里挑选你想用的版本号就可以。下载源代码包后,解压至本地随意文件夹(保证读写权限),留待使用。 安装PHP前。须要安装编译环境和PHP的相关依赖。以下是相关命令:

Ubuntu环境下:

sudo apt-get install build-essential gcc g++ autoconf libiconv-hook-dev libmcrypt-dev libxml2-dev libmysqlclient-dev libcurl4-openssl-dev libjpeg8-dev libpng12-dev libfreetype6-dev


CentOS环境下:

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers gd gd2 gd-devel gd2-devel perl-CPAN


(注:以上命令是我在实际使用中验证过的能够使用的,可能会和其它教程提供的命令不同) 当上述命令运行后,就可以開始安装PHP。

命令例如以下:

cd php-5.5.10/
./configure --prefix=/usr/local/php --with-config-file-path=/etc/php --enable-fpm --enable-pcntl --enable-mysqlnd --enable-opcache --enable-sockets --enable-sysvmsg --enable-sysvsem  --enable-sysvshm --enable-shmop --enable-zip --enable-ftp --enable-soap --enable-xml --enable-mbstring --disable-rpath --disable-debug --disable-fileinfo --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pcre-regex --with-iconv --with-zlib --with-mcrypt --with-gd --with-openssl --with-mhash --with-xmlrpc --with-curl --with-imap-ssl
sudo make
sudo make install
sudo cp php.ini-development /etc/php/


至此。PHP已经安装成功。可是此时在终端里是无法直接通过php --version查看php版本号的。

还须要将PHP的可运行文件夹加入到环境变量中。 使用Vim/Sublime打开~/.bashrc。在末尾加入例如以下内容:

export PATH=/usr/local/php/bin:$PATH
export PATH=/usr/local/php/sbin:$PATH


保存后。终端输入命令:

source ~/.bashrc


此时就可以通过php --version查看php版本号。看到例如以下内容:

PHP 5.5.10 (cli) (built: Apr 26 2014 09:46:14)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies


即说明成功安装。


Swoole安装

安装完PHP后。就可以安装swoole扩展。

swoole扩展下载地址:https://github.com/swoole/swoole-src/releases 尽量选择stable版本号,alpha版本号最好仅用于实验新特性。

解压源代码至随意文件夹。运行例如以下命令:

cd swoole-src-swoole-1.7.6-stable/
phpize
./configure --enable-async-mysql
sudo make
sudo make install


(注:swoole的./configure有非常多额外參数,能够通过./configure --help命令查看,这里仅开启当中async-mysql项。其它均选择默认项) 安装完毕后,进入/etc/php文件夹下,打开php.ini文件,在当中加上例如以下一句:

extension=swoole.so


随后在终端中输入命令

php -m


查看扩展安装情况。假设在列出的扩展中看到了swoole,则说明成功安装。


基本实例

以下贴一个主要的基于swoole的echoserver

// Server
class Server
{
private $serv;

public function __construct() {
$this->serv = new swoole_server("0.0.0.0", 9501);
$this->serv->set(array(
'worker_num' => 8,
'daemonize' => false,
'max_request' => 10000,
'dispatch_mode' => 2,
'debug_mode'=> 1
));

$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));

$this->serv->start();
}

public function onStart( $serv ) {
echo "Start\n";
}

public function onConnect( $serv, $fd, $from_id ) {
$serv->send( $fd, "Hello {$fd}!" );
}

public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
echo "Get Message From Client {$fd}:{$data}\n";
}

public function onClose( $serv, $fd, $from_id ) {
echo "Client {$fd} close connection\n";
}
}
// 启动服务器
$server = new Server();


从代码中能够看出,创建一个swoole_server基本分三步:

1. 通过构造函数创建swoole_server对象

2. 调用set函数设置swoole_server的相关配置选项

3. 调用on函数设置相关回调函数 关于set配置选项以及on回调函数的详细说明,请參考我整理的swoole文档(临时还没有……小伙伴们耐心等两天……届时我会给出链接哒)

这里仅仅给出简介。onStart回调在server执行前被调用,onConnect在有新client连接过来时被调用,onReceive函数在有数据发送到server时被调用。onClose在有client断开连接时被调用。

这里就能够大概看出怎样使用swoole:在onConnect处监听新的连接;在onReceive处接收数据并处理。然后能够调用send函数将处理结果发送出去;在onClose处处理client下线的事件。

以下贴出client的代码:

<?php
class Client
{
private $client;

public function __construct() {
$this->client = new swoole_client(SWOOLE_SOCK_TCP);
}

public function connect() {
if( !$this->client->connect("127.0.0.1", 9501 , 1) ) {
echo "Error: {$fp->errMsg}[{$fp->errCode}]\n";
}
$message = $this->client->recv();
echo "Get Message From Server:{$message}\n";

fwrite(STDOUT, "请输入消息:");
$msg = trim(fgets(STDIN));
$this->client->send( $msg );
}
}

$client = new Client();
$client->connect();


这里。通过swoole_client创建一个基于TCP的client实例。并调用connect函数向指定的IP及port发起连接请求。

随后就可以通过recv()和send()两个函数来接收和发送请求。

须要注意的是。这里我使用了默认的同步堵塞client,因此recv和send操作都会产生网络堵塞。

(以上两段代码均以上传git,地址:https://github.com/LinkedDestiny/swoole-doc/tree/master/src/01)

下章预告:swoole的task使用以及实例:简单聊天室
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: