tsung 正在
11 years ago
PHP 也是可以很 event - Ricky
latest #46
tsung
11 years ago
Build a Realtime Web
tsung
11 years ago
Realtime web 必須具備主動發送訊息的機制
tsung
11 years ago
Long Polling - IE6 也可以的方案
立即下載
tsung
11 years ago
Chunked Transfer Encoding - HTTP 1.1 新增
tsung
11 years ago
將資料拆成一個一個區塊送回去, Header, Data * n , End
tsung
11 years ago
Header: Transfer-Encoding: chunked
tsung
11 years ago
Long Polling 只能單向溝通(Server -> Client), 還有 Cross Domain 的問題.
tsung
11 years ago
WebSocket: 持久連線, 雙向溝通, 允許 Binary Data, 支援 Cross Domain.
tsung
11 years ago
C10K Problem - Server 同時間大量連線
tsung
11 years ago
連線到 10K 時, 設計不良的架構就會出問題.
tsung
11 years ago
硬體不再是效能的瓶頸點 (CPU / RAM 加倍也沒用)
tsung
11 years ago
Blocking I/O
tsung
11 years ago
每個 Thread / Process 只服務一個連線, 透過 Blocking I/O 處理連線. ex: Apache with Prefork / Worker MPM.
tsung
11 years ago
Blocking I/O: 充分運用 CPU 資源
tsung
11 years ago
Asynchronous I/O
tsung
11 years ago
一個 Thread / Process 處理多個連線, 使用事件驅動方式處理連線. ex: Nginx, Memcached, Node.js
tsung
11 years ago
Asynchronous I/O: 節省記憶體, 反應速度快, 架構簡單.
tsung
11 years ago
程式邏輯需注意 Blocked API, 會導致其他連線也被 Block.
tsung
11 years ago
反應速度要快, 否則無法應付瞬間大量連線
tsung
11 years ago
使用資源要省, 否則大量連線會吃光系統資源.
tsung
11 years ago
select(), poll() 效能會隨著數量遞減, 時間複雜度 O(n)
tsung
11 years ago
socket_select(): 1024 limit on Linux (FDSET_SIZE)
tsung
11 years ago
新一代事件通知事件: kqueue: *BSD / epoll: Linux / /dev/pool: Solaris. 時間複雜度為 O(1) - 缺點: API 不相容, 使用 libevent 來解決.
tsung
11 years ago
pecl Libevent (難用), 可用 pecl event
tsung
11 years ago
pecl event: OOP, 支援 libevent2, HTTP DNS OpenSSL 支援
tsung
11 years ago
Simple Web Server
tsung
11 years ago
event loop: 所有事件的起點
tsung
11 years ago
callback
tsung
11 years ago
setDefaultCallback(function($request) {});
tsung
11 years ago
setCallback('hello', ....)
tsung
11 years ago
buffer: 所有輸出輸入都在 Buffer 裡
tsung
11 years ago
buffer event
tsung
11 years ago
setCallbacks(onRead, onWrite, onEvent, $arg);
tsung
11 years ago
onRead: 當收到資料時
tsung
11 years ago
onWrite: 當資料已經送出, Buffer Empty
tsung
11 years ago
onEvent: 當連線狀態改變, 例如使用者斷線.
tsung
11 years ago
timeout event
tsung
11 years ago
只會跑一次.
tsung
11 years ago
Socket.IO - the cross-browser WebSocket for realtime apps
tsung
11 years ago
phpsocket.io - Socket.IO Server Side
tsung
11 years ago
tsung
11 years ago
支援 wesocket, JSONP Polling, XHRPolling, 和 Socket.IO 相似用法
tsung
11 years ago
PHP 5.4 以上, ext-event (1.8 以上)
tsung
11 years ago
JS 直接使用 socket.io.
back to top