php使用event擴(kuò)展的io復(fù)用測(cè)試的示例
先要安裝event擴(kuò)展,這樣才可以使用libevent的事件機(jī)制
pecl install event
測(cè)試代碼
//連接重用//創(chuàng)建資源流的上下文$context=stream_context_create([ ’socket’=>[ ’backlog’=>2000 ]]);stream_context_set_option($context,’socket’,’so_reuseaddr’,1); //設(shè)置連接重用//sock_set_option($this->server, SOL_SOCKET, SO_REUSEADDR, 1); //復(fù)用還處于 TIME_WAIT$socket = stream_socket_server('tcp://0.0.0.0:8070', $errno, $errstr,STREAM_SERVER_BIND | STREAM_SERVER_LISTEN,$context);stream_set_blocking($socket,false);//非阻塞//綁定事件$base=new EventBase();//監(jiān)聽服務(wù)端的socket$event=new Event($base,$socket,Event::PERSIST |Event::READ | Event::WRITE,function ($socket)use(&$base){ $client = stream_socket_accept($socket); //stream_set_blocking($client,false);//非阻塞// var_dump($client);// $content=’21335435’;// $string='HTTP/1.1 200 OKrn';// $string.='Content-Type: text/html;charset=utf-8rn';// $string.='Content-Length: '.strlen($content).'rnrn';// fwrite($client,$string.$content);// fclose($client); //global $base; //var_dump($socket,$client); $base=new EventBase(); //監(jiān)聽客戶端socket $event=new Event($base,$client,Event::PERSIST |Event::READ | Event::WRITE,function ($client){ $msg=fread($client,65535);//// if($msg){ //匹配請(qǐng)求頭包含了keep-alive//// } $content=’hello world’; $string='HTTP/1.1 200 OKrn'; $string.='Content-Type: text/html;charset=utf-8rn'; $string.='Connection: keep-alivern'; $string.='Content-Length: '.strlen($content).'rnrn'; fwrite($client,$string.$content); fclose($client); //當(dāng)socket斷開連接,刪除事件 $event->del();//刪除事件 }); $event->add(); //加入事件監(jiān)聽 $base->loop(); //監(jiān)視客戶端 //$event->del();//刪除事件});$event->add(); //加入事件監(jiān)聽var_dump($base->loop()); //調(diào)度掛起事件監(jiān)聽
以上就是php使用event擴(kuò)展的io復(fù)用測(cè)試的示例的詳細(xì)內(nèi)容,更多關(guān)于php io復(fù)用測(cè)試的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. JSP實(shí)時(shí)顯示當(dāng)前系統(tǒng)時(shí)間的四種方式示例解析2. 程序猿說love的100種語(yǔ)言3. SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例4. PHP使用POP3讀取郵箱接收郵件的示例代碼5. JavaScript本地儲(chǔ)存:localStorage、sessionStorage、cookie的使用6. vue中解決拖拽改變存在iframe的div大小時(shí)卡頓問題7. AJAX原理以及axios、fetch區(qū)別實(shí)例詳解8. vue+elementUI下拉框回顯問題及解決方式9. python 模擬在天空中放風(fēng)箏的示例代碼10. 深入淺出JavaScript前端中的設(shè)計(jì)模式
