網頁遊戲能用PHP作後端開發嗎?
固然能夠。最好走HTTP,也能夠作網絡編程,並且寫代碼超簡單,1個函數就能夠建一個服務器端。stream_socket_server()
多線程不是什麼好主意,你能夠用php libevent擴展,異步高併發。PHP還有不少網絡的擴展包。
PHP的開發效率高,.net Java ,你足夠有錢,能夠用。php
libevent是一個基於事件驅動的高性能網絡庫。支持多種 I/O 多路複用技術, epoll、 poll、 dev/poll、 select 和 kqueue 等;支持 I/O,定時器和信號等事件;註冊事件優先級。
PHP libevent擴展安裝:
libevent擴展依賴於原始的libevent庫,必須先把libevent庫安裝。
(1)安裝libevent庫html
- wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz
- tar zxvf libevent-2.0.20-stable.tar.gz
- cd libevent-2.0.20-stable/
- ./configure --prefix=/usr/local/libevent-2.0.20/
- make
- make install
(2)安裝libevent擴展(http://pecl.php.net/package/libevent)
git
- wget http://pecl.php.net/get/libevent-0.1.0.tgz
- tar -zxvf libevent-0.1.0.tgz
- cd libevent-0.1.0
- ./configure --with-php-config=/usr/local/php54/bin/php-config --with-libevent=/usr/local/libevent-2.0.20/
- make && make install
- #php.ini添加extension=libevent.so
PHP Libevent擴展介紹:
(1)常量
(2)函數
github
- event_base_free() 釋放資源,這不能銷燬綁定事件
- event_base_loop() 處理事件,根據指定的base來處理事件循環
- event_base_loopbreak() 當即取消事件循環,行爲各break語句相同
- event_base_loopexit() 在指定的時間後退出循環
- event_base_new() 建立而且初始事件
- event_base_priority_init() 設定事件的優先級
- event_base_set() 關聯事件到事件base
- event_buffer_base_set() 關聯緩存的事件到event_base
- event_buffer_disable() 禁用一個緩存的事件
- event_buffer_enable() 啓用一個指定的緩存的事件
- event_buffer_fd_set() 改變一個緩存的文件系統描述
- event_buffer_free() 釋放緩存事件
- event_buffer_new() 創建一個新的緩存事件
- event_buffer_priority_set() 緩存事件的優先級設定
- event_buffer_read() 讀取緩存事件中的數據
- event_buffer_set_callback() 給緩存的事件設置或重置回調hansh函數
- event_buffer_timeout_set() 給一個緩存的事件設定超時的讀寫時間
- event_buffer_watermark_set 設置讀寫事件的水印標記
- event_buffer_write() 向緩存事件中寫入數據
- event_add() 向指定的設置中添加一個執行事件
- event_del() 從設置的事件中移除事件
- event_free() 清空事件句柄
- event_new() 建立一個新的事件
- event_set() 準備想要在event_add中添加事件
PHP Libevent擴展使用:
例1:5s後觸發callback
web
- $base = event_base_new();
- $event = event_new();
-
- event_set($event, 0, EV_TIMEOUT, function() {
- echo "function called";
- });
- event_base_set($event, $base);
-
- event_add($event, 5000000);
- event_base_loop($base);
例2:打印輸入流
編程
- function print_line($fd, $events, $arg)
- {
- static $max_requests = 0;
- $max_requests++;
- if ($max_requests == 10) {
- // exit loop after 10 writes
- event_base_loopexit($arg[1]);
- }
- echo fgets($fd);
- }
-
- // create base and event
- $base = event_base_new();
- $event = event_new();
-
- $fd = STDIN;
- // set event flags
- event_set($event, $fd, EV_READ | EV_PERSIST, 'print_line', array($event, $base));
- // set event base
- event_base_set($event, $base);
- // enable event
- event_add($event);
- // start event loop
- event_base_loop($base);
例3:實現簡單web server
在cli執行後,打開瀏覽器2000端口試試看。
後端
- $socket = stream_socket_server ('tcp://0.0.0.0:2000', $errno, $errstr);
- stream_set_blocking($socket, 0);
-
- $base = event_base_new();
- $event = event_new();
- event_set($event, $socket, EV_READ | EV_PERSIST, 'ev_accept', $base);
- event_base_set($event, $base);
- event_add($event);
- event_base_loop($base);
-
- function ev_accept($socket, $flag, $base)
- {
- $connection = stream_socket_accept($socket);
- stream_set_blocking($connection, 0);
-
- $buffer = event_buffer_new($connection, 'ev_read', NULL, 'ev_error', $connection);
- event_buffer_base_set($buffer, $base);
- event_buffer_timeout_set($buffer, 30, 30);
- event_buffer_watermark_set($buffer, EV_READ, 0, 0xffffff);
- event_buffer_priority_set($buffer, 10);
- event_buffer_enable($buffer, EV_READ | EV_PERSIST);
-
- $GLOBALS['_'] = $buffer; //這個buffer必定要賦給個全局的變量 貌似是傳值過程當中的bug 或者5.3.8的閉包仍是有問題?
- }
-
- function ev_error($buffer, $error, $connection)
- {
- event_buffer_disable($buffer, EV_READ | EV_WRITE);
- event_buffer_free($buffer);
- fclose($connection);
- }
-
- function ev_read($buffer, $connection)
- {
- while ($read = event_buffer_read($buffer, 256)) {
-
- }
- fwrite($connection , date('Y-m-d H:i:s'));
- ev_error($buffer , '' , $connection);
- }
參考資料:
瀏覽器