Posted 楂樻€ц兘API绀惧尯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
銇裤仾銇曘倱銇撱倱銇仭銇€岃€佹潕銆嶏紝鏃㈢煡銇€岃阿椤堕亾浜恒€嶃€?/span>
浜嬫儏鏄繖鏍风殑锛屽仛涓轰竴涓窡鎴戞棗榧撶浉褰撶殑銆岃阿椤舵硶甯堛€?-- 鑰佽档璺熸垜璇达細
涓€閮ㄥ垎鏍煎眬姣旇緝澶х殑娉ヨ吙瀛愪及璁″唴蹇冩棭灏卞凡缁忛瑙佸埌浜嗚繖绉嶃€岄鍙c€嶏紝铏界劧鍙兘娌℃湁鍋氬ソ杩庢帴杩欑銆岄鍙c€嶇殑鐗╃悊鍑嗗锛屼絾涔熻嚦灏戝仛濂戒簡蹇冪悊鍑嗗锛?span class="mq-8">娌″叧绯昏繜鏃╄繕浼氿煋堝洖鏉ョ殑銆?/span>
鏈€杩戠殑鐡滈兘姣旇緝甯︽湁榛戣壊骞介粯鐨勫懗閬擄紝姣斿銆屽奖瑙嗚〃婕旇壓鏈銆嶇殑绮変笣浠紑鍥㈢兢鎸戜竴浜涙枃瀛﹀垱浣滅綉绔欏苟鎴愬姛浣夸箣琚姞鍏ュ埌404鍚嶅崟锛屽叾瑙勬ā銆佸叾姘斿娍銆佸叾鎵撴硶涔嬪噷鍘夈€佸叾鎴樼暐涔嬪ぇ鎵嬬瑪锛屽牚绉板吀鑼冧互鑷充簬鍙戝睍鍒板悗闈紝宸茬粡鍐茬牬浜嗗弻鏂规墍鑳芥帉鎺х殑鑼冨洿銆?/span>
棣栧厛鏄垜鍑犱箮锛堟敞鎰忔槸鍑犱箮锛夊彧鍙戞妧鏈被鏂囩珷锛屼汉鐢熷甯堛€佽亴鍦烘暀鑲层€佸壇涓氳禋閽便€佸勾钖櫨涓囩殑鎴戣祫鍘嗗皻娴呴兘璁蹭笉浜?/span>
鍏舵鏄啓鎶€鏈枃绔犱篃杩樻槸鎸洪夯鐑︾殑锛屼竴鏄妧鏈湰韬槸鍚﹁瑙e埌浣嶏紝浜屾槸鏂囨鍔╂敾鏄惁缁欏姏锛屾渶鍚庤繕寰楄€冭檻閰嶅浘
浠婂ぉ鎴戜滑璇磂vent-loop锛岀‘鍒囪鏄€荤粨骞舵娊璞vent-loop锛岃阿椤堕亾浜鸿€佹潕甯︾潃澶у宸茬粡浠巗ocket鍩虹鍒皊elect IO澶嶇敤鍐嶅埌epoll锛堝疄闄呬笂鏄痚vent鎵╁睍锛夛紝鍩烘湰涓婃妸鍩虹浠庡ご鍒板熬鎾镐簡涓€杈癸紝杩橀『甯︽悶瀹氫簡鍚屾銆佸紓姝ャ€侀樆濉炪€侀潪闃诲绛夋蹇碉紝浣犺璇翠綘娌′换浣曟敹鑾凤細
鑷充簬浣犱俊涓嶄俊锛屾垜鍙嶆涓嶄俊
浼楁墍鍛ㄧ煡锛學orkerman瀹為檯涓婃槸涓€涓ぇ涓€缁熺殑缁撳悎浣擄紝鏁村悎浜員CP銆乁DP绛変竴鍧ㄥ崗璁紝绾犻泦浜嗚繘绋嬫帶鍒朵笌绠$悊锛屽泭鎷簡鍚勫紡鍚勬牱鐨別vent绫诲簱锛屾€讳箣灏辨槸鍚勭涓ゅ紑鑺便€?/span>鐒惰€屾湁鏃跺€欙紝鎴戜滑闇€瑕佺嫭绔嬪彲鎷嗗垎鐨勭粍浠讹紝姣斿鍟ユ椂鍊欏憿锛?/span>灏辨瘮濡傝浠婂勾骞村簳锛屽湪璐靛徃骞冲彴娌荤悊閮ㄩ棬鎬诲寘宸ュご杞╄緯绉冮《鍚屽織鐨勭壍澶翠笅锛屽钩鍙版不鐞嗛儴鑱斿悎鍩虹骞冲彴閮ㄤ互鍙婂畨鍏ㄥ璁¢儴绛夋暟涓儴闂ㄥ睍寮€浜嗐€岃繋鏂版槬銆佸簡榧犲勾銆嶇殑鏈嶅姟缁勪欢鎷嗗垎涓ゅ紑鑺辩壒鍒鍔紝鑰屼綘 --- 娆ч槼灏嗚礋璐e皢PHP璇█鐨凟vent-Loop鎶借薄骞剁粍浠跺寲锛屾渶缁堟垚缁╃畻鍏ュ勾搴昈KR鑰冩牳骞朵笌骞寸粓濂栫洿鎺ユ寕閽┿€?/span>
杩欏氨鏄槑鎽嗙潃鑰佹澘鍘熶笂鑽夊喅鎰忛€佸ぇ瀹跺厤璐圭鎶ワ紝鑰屼綘涔熷喅瀹氥€屽蹇ソ鐪併€嶅湴瀹屾垚浠诲姟锛屼簬鏄綘鐬勫噯浜唃ithub涓婅但璧湁鍚嶇殑Reactphp锛?/span>
Reactphp鏄涓嬪浘杩欐牱shai鍎垮緱锛孴A鎶奺vent-loop鐩存帴鎶借薄鍑烘潵浣滀负浜嗕竴涓簳灞傚熀纭€缁勪欢锛岃繖鏍峰熀浜庤繖涓猠vent-loop缁勪欢鍙互蹇€熷疄鐜皊tream鏈嶅姟銆乭ttp鏈嶅姟銆乻ocket鏈嶅姟绛?..
鎵€浠ヤ粖澶╂闃崇殑浠诲姟灏辨槸缁煎悎涓€涓嬩箣鍓嶇殑绔犺妭锛屾娊璞″嚭涓€涓猠vent-loop缁勪欢銆?/span>鏂囦欢鐩綍鏍戞槸杩欐牱鐨勶細
-Event/ // Event鐩綍
|------Libevent.php // 鏈鍩轰簬Event鎵╁睍瀹炵幇鐨?/span>
|------Select.php // 璇惧悗浣滀笟锛?/span>
|------Libev.php // 璇惧悗浣滀笟锛?/span>
|------Factory.php // 璇惧悗浣滀笟锛?/span>
-index.php // 鍏ュ彛鏂囦欢
Event/Libevent.php锛?/span>
<?php
namespace Event;
class Libevent {
const EV_ALL = 1;
const EV_READ = 2;
const EV_WRITE = 4;
const EV_EXCEPTION = 8;
public $o_event_config = null;
public $o_event_base = null;
/*
* @desc : 淇濆瓨浜嬩欢event
* */
public $a_event = array();
public $a_client = array();
public function __construct() {
$o_event_config = new EventConfig();
$o_event_base = new EventBase( $o_event_config );
$this->o_event_config = $o_event_config;
$this->o_event_base = $o_event_base;
}
/*
* @desc : 娣诲姞涓€涓簨浠?/span>
* @param : socket fd
* @param : event type, EV_READ EV_WRITE
* @param : callback
* */
public function add( $r_fd, $i_event_type, $f_callback ) {
$i_event_flag = $i_event_type == self::EV_READ ? Event::READ | Event::PERSIST : Event::WRITE | Event::PERSIST ;
$o_event = new Event( $this->o_event_base, $r_fd, $i_event_flag, $f_callback );
$o_event->add();
$i_fd = intval( $r_fd );
$this->a_event[ $i_fd ][ $i_event_type ] = $o_event;
$this->a_client[ $i_fd ] = $r_fd;
//echo json_encode( $this->a_client )." | ".json_encode( $this->a_event ).PHP_EOL;
}
/*
* @desc : 鍒犻櫎涓€涓簨浠?/span>
* */
public function del( $r_fd, $i_event_type ) {
$i_fd = intval( $r_fd );
// 鍒犻櫎鎵€鏈変簨浠剁被鍨?/span>
if ( self::EV_ALL === $i_event_type ) {
if ( isset( $this->a_event[ $i_fd ] ) ) {
if ( isset( $this->a_event[ $i_fd ][ self::EV_WRITE ] ) ) {
echo "del write".PHP_EOL;
$o_event = $this->a_event[ $i_fd ][ self::EV_WRITE ];
$o_event->free();
unset( $this->a_event[ $i_fd ][ self::EV_WRITE ] );
}
if ( isset( $this->a_event[ $i_fd ][ self::EV_READ ] ) ) {
echo "del read".PHP_EOL;
$o_event = $this->a_event[ $i_fd ][ self::EV_READ ];
$o_event->free();
unset( $this->a_event[ $i_fd ][ self::EV_READ ] );
}
if ( isset( $this->a_event[ $i_fd ][ self::EV_EXCEPTION ] ) ) {
echo "del exception".PHP_EOL;
$o_event = $this->a_event[ $i_fd ][ self::EV_EXCEPTION ];
$o_event->free();
unset( $this->a_event[ $i_fd ][ self::EV_EXCEPTION ] );
}
}
}
// 鍒犻櫎鎸囧畾浜嬩欢绫诲瀷
else {
if ( isset( $this->a_event[ $i_fd ] ) ) {
if ( isset( $this->a_event[ $i_fd ][ $i_event_type ] ) ) {
$o_event = $this->a_event[ $i_fd ][ $i_event_type ];
$o_event->free();
unset( $this->a_event[ $i_fd ][ $i_event_type ] );
}
}
}
unset( $this->a_client[ $i_fd ] );
}
/*
* @desc : 闄峰叆浜嬩欢寰幆
* */
public function loop() {
$this->o_event_base->loop();
}
}
index.php锛?/span>
define( "DS", DIRECTORY_SEPARATOR );
define( "ROOT", __DIR__ );
spl_autoload_register( function( $s_class_name ) {
$s_path = str_replace( "\", "/", $s_class_name );
$s_file = ROOT.DS.$s_path.'.php';
require_once $s_file;
} );
// 鍒涘缓涓€涓潪闃诲鐨刲isten-socket璧勬簮
$r_listen_socket = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
socket_set_option( $r_listen_socket, SOL_SOCKET, SO_REUSEADDR, 1 );
socket_set_option( $r_listen_socket, SOL_SOCKET, SO_REUSEPORT, 1 );
socket_bind( $r_listen_socket, '0.0.0.0', 6666 );
socket_listen( $r_listen_socket );
socket_set_nonblock( $r_listen_socket );
// 鍒涘缓涓€涓狤vent-Loop
// 鐒跺悗灏嗕笂闈㈢殑listen-socket鍔犲叆鍒癊vent-Loop涓?/span>
// 杩欓噷灏卞凡缁忔瘮杈冪洿鐧戒簡锛屽彧瑕佷綘瑕佸垱寤轰竴涓祫婧?/span>
// 杩欎釜璧勬簮鍙互鏄痵ocket銆佸彲浠ユ槸stream绛夌瓑
// 鍥犱负Event-Loop宸茬粡琚垜浠娊璞$О浜嗘ā鍧?/span>
// 鎵€浠ヤ綘鍒涘缓鐨勮祫婧愬彧瑕佹敮鎸丒vent锛岄偅涔堥兘寰堟柟渚垮湴閫氳繃Event-Loop妯″潡鍐欏嚭鏉?/span>
$o_event_loop = new EventLibevent();
$o_event_loop->add( $r_listen_socket, EventLibevent::EV_READ, function() use( $r_listen_socket, $o_event_loop ) {
$r_connection_socket = socket_accept( $r_listen_socket );
$o_event_loop->add( $r_connection_socket, EventLibevent::EV_WRITE, function() use ( $r_connection_socket, $o_event_loop ) {
$s_data = "Hello HTTP World!";
$s_html = "HTTP/1.1 200 OK Content-Length: ".strlen( $s_data )." {$s_data} ";
$i_written = socket_write( $r_connection_socket, $s_html, strlen( $s_html ) );
$o_event_loop->del( $r_connection_socket, EventLibevent::EV_ALL );
});
} );
$o_event_loop->loop();
娴佺▼鍜岄€昏緫涓婃槸涓嶆槸娓呯埥鏃犳瘮锛?/span>璇翠笅澶ф閫昏緫锛?/span>
绗竴姝ワ細鍦≒HP閲屽悇绉嶈兘鏀寔Event-Loop鐨勮祫婧愶紝姣斿socket銆佹瘮濡俿tream
绗簩姝ワ細鍒濆鍖朎vent-Loop锛岀劧鍚庡皢绗竴姝ラ噷鍒涘缓濂界殑璧勬簮鎵斿埌Event-Loop閲?/span>
绗笁姝ワ細瀹屾垚
鎴戠暀浜嗕竴浜涜澶栦綔涓氾紝涓昏鏄垜澶噿浜嗘噿寰楀紕锛屼竴涓畬鏁寸殑Event-Loop妯″潡杩樺樊涓€涓狿HP Interface锛岀劧鍚嶭ibevent銆丼elect銆丩ibev閮藉疄鐜拌繖涓狪nterface锛岃€孎actory.php鍒欑郴缁熷垯鏍规嵁绯荤粺褰撳墠宸茬粡鍏峰鐨処O澶嶇敤鑷姩閫夋嫨鏈€濂界殑IO澶嶇敤鏂规硶骞跺垵濮嬪寲Event-Loop瀵硅薄锛?/span>
鏈変簡杩欐牱涓€涓狤vent-Loop缁勪欢锛屼綘鍙互寰堝揩鍩轰簬TA瀹炵幇涓€涓€?/span>鍩轰簬浜嬩欢鐩戝惉鐨勫紓姝ラ潪闃诲楂樻€ц兘銆岺TTP鏈嶅姟鍣ㄣ€乀CP鏈嶅姟鍣ㄣ€丳HP鐗堢殑Redis鏈嶅姟鍣ㄣ€丳HP鐗堟湰鐨凪emcache鏈嶅姟鍣紝杩欏彨鍟ワ紵杩欏氨鍙€岄珮鍐呰仛銆佷綆鑰﹀悎銆嶏紝鐜╃殑灏辨槸涓撲笟锛屽懆鏉版鏄€庝箞鍞辩殑鏉ョ潃锛?/span>
涓€娈垫垜鍐欏ソ鐨勫簱鏂囦欢锛実ithub涓€鏀惧ソ澶氬勾锛孴A涓€鐩村湪韬竟
鎴戝啓鐨勬槸瓒婃潵瓒婄渷鍔茶交鏉撅紝褰撶劧鎴戜篃甯屾湜浣犱滑涔熻秺鐪嬭秺杞绘澗銆?/span>
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章