> 環境說明:
> 系統:Ubuntu14.04 (安裝教程包括CentOS6.5)
> PHP版本:PHP-5.5.10
> swoole版本:1.7.6-stable
## PHP安裝
要用swoole,首先需要有PHP環境。由于swoole的某些特性,最好是能夠從源碼編譯安裝PHP,這樣在使用過程中可以避免很多不必要的錯誤。 PHP下載地址:[http://php.net/](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 pcre-devel
~~~
(注:以上命令是我在實際使用中驗證過的可以使用的,可能會和其他教程提供的命令不同) 當上述命令執行后,即可開始安裝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](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的echo服務器
~~~
// 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文檔(?[配置選項](https://github.com/LinkedDestiny/swoole-doc/blob/master/doc/01.swoole_server%E9%85%8D%E7%BD%AE%E9%80%89%E9%A1%B9.md))
這里只給出簡單介紹。onStart回調在server運行前被調用,onConnect在有新客戶端連接過來時被調用,onReceive函數在有數據發送到server時被調用,onClose在有客戶端斷開連接時被調用。 這里就可以大概看出如何使用swoole:在onConnect處監聽新的連接;在onReceive處接收數據并處理,然后可以調用send函數將處理結果發送出去;在onClose處處理客戶端下線的事件。
下面貼出客戶端的代碼:
~~~
<?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的客戶端實例,并調用connect函數向指定的IP及端口發起連接請求。隨后即可通過recv()和send()兩個函數來接收和發送請求。需要注意的是,這里我使用了默認的同步阻塞客戶端,因此recv和send操作都會產生網絡阻塞。
(以上兩段代碼均以上傳git,地址:[https://github.com/LinkedDestiny/swoole-doc/tree/master/src/01)](https://github.com/LinkedDestiny/swoole-doc/tree/master/src/01%EF%BC%89)