# 入門
Swoole雖然是標準的PHP擴展,實際上與普通的擴展不同。普通的擴展只是提供一個庫函數。而swoole擴展在運行后會接管PHP的控制權,進入事件循環。當IO事件發生后,swoole會自動回調指定的PHP函數。
Swoole要求使用者必須具備一定的Linux/Unix環境編程基礎,《[學習Swoole需要掌握哪些基礎知識](學習Swoole需要掌握哪些基礎知識.md)》 本文列出了基礎知識清單。
默認讀者已具備如下能力:
* 熟練使用PHP語言
* 熟練使用MySQL、Redis數據庫
* 熟練使用Linux操作系統
* 基本了解Unix網絡編程相關知識(參閱《Unix網絡編程(卷1)》)
* 基本的gdb使用
## swoole_server
強大的TCP/UDP Server框架,多線程,EventLoop,事件驅動,異步,Worker進程組,Task異步任務,毫秒定時器,SSL/TLS隧道加密。
* swoole_http_server是swoole_server的子類,內置了Http的支持
* swoole_websocket_server是swoole_http_server的子類,內置了WebSocket的支持
* swoole_redis_server是swoole_server的子類,內置了Redis服務器端協議的支持子類可以調用父類的所有方法和屬性
## swoole_client
TCP/UDP/UnixSocket客戶端,支持IPv4/IPv6,支持SSL/TLS隧道加密,支持SSL客戶端整數,支持同步并發調用,也支持異步事件驅動編程。
## swoole_event
EventLoop API,讓用戶可以直接操作底層的事件循環,將socket,stream,管道等Linux文件加入到事件循環中。
eventloop接口僅可用于socket類型的文件描述符,不能用于磁盤文件讀寫
## swoole_async
異步IO接口,提供了 異步文件系統IO,定時器,異步DNS查詢,異步MySQL等API,異步Http客戶端,異步Redis客戶端。
## swoole_timer
異步毫秒定時器,可以實現間隔時間或一次性的定時任務
swoole_async_read/swoole_async_write 文件系統操作的異步接口
## swoole_process
進程管理模塊,可以方便的創建子進程,進程間通信,進程管理。
## swoole_buffer
強大的內存區管理工具,像C一樣進行指針計算,又無需關心內存的申請和釋放,而且不用擔心內存越界,底層全部做好了。
## swoole_table
基于共享內存和自旋鎖實現的超高性能內存表。徹底解決線程,進程間數據共享,加鎖同步等問題。
## tcp server
```php
<?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, $data);
});
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
// Start our server, listen on the port and be ready to accept connections.
$serv->start();
- swoole簡介
- swoole功能概述
- 序章
- 開發必讀
- 1 環境搭建
- 1.1 環境搭建
- 1.2 搭建Echo服務器
- 2 初識Swoole
- 2.1 Worker進程
- 2.2 TaskWorker進程
- 2.3 Timer定時器
- 2.4 Process進程
- 2.5 Table內存表
- 2.6 多端口監聽
- 2.7 sendfile文件支持
- 2.8 SSL支持
- 2.9 熱重啟
- 2.10 http_server
- 附錄*server配置
- 附錄*server函數
- 附錄*server屬性
- 附錄*server回調函數
- 附錄*server高級特性
- 心跳檢測
- 3 Swoole協議
- 3.1 EOF協議
- 3.2 固定包頭協議
- 3.3 Http協議
- 3.4 WebSocket協議
- 3.5 MTQQ協議
- 內置http_server
- 內置websocket_server
- Swoole\Redis\Server
- 4 Swoole異步IO
- 4.1 AsyncIO
- 異步文件系統IO
- swoole_async_readfile
- swoole_async_writefile
- swoole_async_read
- swoole_async_write
- 5 swoole異步客戶端
- ws_client
- http_client
- mysql_client
- redis_client
- tcp_client
- http2_client
- 6 swoole協程
- Swoole\Coroutine\Http\Client
- Swoole\Coroutine\MySQL
- Swoole\Coroutine\Redis
- Coroutine\PostgreSQL
- Swoole\Coroutine\Client
- Swoole\Coroutine\Socket
- Swoole\Coroutine\Channel
- Coroutine
- Swoole\Coroutine::create
- Swoole\Coroutine::resume
- Swoole\Coroutine::suspend
- Swoole\Coroutine::sleep
- Coroutine::getaddrinfo
- Coroutine::gethostbyname
- swoole_async_dns_lookup_coro
- Swoole\Coroutine::getuid
- getDefer
- setDefer
- recv
- Coroutine::stats
- Coroutine::fread
- Coroutine::fget
- Coroutine::fwrite
- Coroutine::readFIle
- Coroutine::writeFIle
- Coroutine::exec
- 7 swoole_process
- process::construct
- process::start
- process::name
- process::signal
- process::setaffinity
- process::exit
- process::kill
- process::daemon
- process->exec
- process::wait
- process::alarm
- 8 swoole定時器
- swoole_timer_tick
- swoole_timer_after
- swoole_timer_clear
- 9 swoole_event
- swoole_event_add
- swoole_event_set
- swoole_event_del
- swoole_event_wait
- swoole_event_defer
- swoole_event_write
- swoole_event_exit
- swoole提供的function
- 常見問題
- 客戶端鏈接失敗原因
- 如何設置進程數
- 如何實現異步任務
- 如何選擇swoole三種模式
- php中哪些函數是阻塞的
- 是否可以共用1個redis或mysql連接
- 如何在回調函數中訪問外部的變量
- 為什么不要send完后立即close
- 不同的Server程序實例間如何通信
- MySQL的連接池、異步、斷線重連
- 在php-fpm或apache中使用swoole
- 學習Swoole需要掌握哪些基礎知識
- 在phpinfo中有在php-m中沒有
- 同步阻塞與異步非阻塞選擇
- CURL發送POST請求服務器端超時
- 附錄
- 預定義常量
- 內核參數調優
- php四種回調寫法
- 守護進程程序常用數據結構
- swoole生命周期
- swoole_server中內存管理機制
- 使用jemalloc優化swoole內存分配性能
- Reactor、Worker、Task的關系
- Manager進程
- Swoole的實現
- Reactor線程
- 安裝擴展
- swoole-worker手冊
- swoole相關開源項目
- 寫在后面的話
- 版本更新記錄
- 4.0