本文實(shí)例講述了Linux下源碼包安裝Swoole及基本使用操作。分享給大家供大家參考,具體如下:
下載Swoole PECL擴(kuò)展源碼包:http://pecl.php.net/package/swoole
關(guān)于PHP版本依賴選擇:
下載好放到/usr/local/src下,解壓縮:
tar -zxvf swoole-2.2.0.tgz
準(zhǔn)備擴(kuò)展安裝編譯環(huán)境:
phpize
查看php-config位置:
find / -name php-config
配置:(--with-php-config==后面是你自己的php-config位置)
./configure --with-php-config=/www/server/php/72/bin/php-config
編譯安裝:
make make install
在php.ini里面加一行 :
extension = swoole.so
使用 php -m 命令查看swoole擴(kuò)展已經(jīng)安裝成功:
查看phpinfo信息:
(測試前說明:以下使用的端口,要確認(rèn)服務(wù)器放行,寶塔環(huán)境還需要添加安全組規(guī)則)
【創(chuàng)建TCP服務(wù)器】
創(chuàng)建server.php:
?php //創(chuàng)建Server對象,監(jiān)聽 127.0.0.1:9501端口 $serv = new swoole_server("127.0.0.1", 9501); //監(jiān)聽連接進(jìn)入事件 $serv->on('connect', function ($serv, $fd) { echo "Client: Connect.\n"; }); //監(jiān)聽數(shù)據(jù)接收事件 $serv->on('receive', function ($serv, $fd, $from_id, $data) { $serv->send($fd, "Server: ".$data); }); //監(jiān)聽連接關(guān)閉事件 $serv->on('close', function ($serv, $fd) { echo "Client: Close.\n"; }); //啟動服務(wù)器 $serv->start();
啟動TCP服務(wù):
php server.php
查看9501端口已被監(jiān)聽:
netstat -an | grep 9501
使用telnet連接TCP服務(wù),輸入hello,服務(wù)器返回hello即測試成功:
telnet 127.0.0.1 9501
(如果telnet工具沒有安裝,執(zhí)行yum install telnet
、yum install telnet-server
)
也可以寫一個(gè)TCP客戶端連接TCP服務(wù)器端:
創(chuàng)建tcp_client.php:
?php //創(chuàng)建Client對象,監(jiān)聽 127.0.0.1:9501端口 $client = new swoole_client(SWOOLE_SOCK_TCP); if(!$client->connect("127.0.0.1" ,9501)){ echo "連接失敗"; exit; } //向tcp服務(wù)器發(fā)送消息 fwrite(STDOUT, "請輸入:"); $msg = trim(fgets(STDIN)); $client->send($msg); //接受tcp服務(wù)器消息 $result = $client->recv(); echo $result;
啟動tcp客戶端:
php tcp_client.php
測試結(jié)果:
【創(chuàng)建UDP服務(wù)器】
創(chuàng)建udp_server.php:
?php //創(chuàng)建Server對象,監(jiān)聽 127.0.0.1:9502端口,類型為SWOOLE_SOCK_UDP $serv = new swoole_server("127.0.0.1", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP); //監(jiān)聽數(shù)據(jù)接收事件 $serv->on('Packet', function ($serv, $data, $clientInfo) { $serv->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data); var_dump($clientInfo); }); //啟動服務(wù)器 $serv->start();
啟動UDP服務(wù):
php udp_server.php
查看9502端口已被監(jiān)聽:
netstat -an | grep 9502
使用netcat連接UDP服務(wù),
輸入hello,服務(wù)器返回hello即測試成功(CentOS):
nc -u 127.0.0.1 9502
(如果沒有安裝netcat監(jiān)聽器,執(zhí)行yum install -y nc
)
【創(chuàng)建Web服務(wù)器】
創(chuàng)建http_server.php:
?php $http = new swoole_http_server("0.0.0.0", 9501); //配置靜態(tài)文件根目錄(可選) $http->set([ 'document_root' => '/www/wwwroot/lwsblog', 'enable_static_handler' => true, ]); $http->on('request', function ($request, $response) { var_dump($request->get, $request->post); //設(shè)置header $response->header("Content-Type", "text/html; charset=utf-8"); //設(shè)置cookie $response->cookie("name", "lws", time()+3600); //發(fā)送Http響應(yīng)體,并結(jié)束請求處理。 $response->end("h1>Hello Swoole. #".rand(1000, 9999)."/h1>"); }); $http->start();
啟動服務(wù):
php http_server.php
(如果9501端口已經(jīng)被占用查看進(jìn)程PID,殺死進(jìn)程:)
lsof -i:9501
kill 9013
瀏覽器訪問主機(jī)地址:端口號,得到程序預(yù)期結(jié)果即測試成功:
【創(chuàng)建WebSocket服務(wù)器】
創(chuàng)建ws_server.php:
?php //創(chuàng)建websocket服務(wù)器對象,監(jiān)聽0.0.0.0:9501端口 $ws = new swoole_websocket_server("0.0.0.0", 9501); //配置靜態(tài)文件根目錄(可選) $ws ->set([ 'document_root' => '/www/wwwroot/lwsblog', 'enable_static_handler' => true, ]); //監(jiān)聽WebSocket連接打開事件 $ws->on('open', function ($ws, $request) { var_dump($request->fd, $request->get, $request->server); $ws->push($request->fd, "hello, welcome\n"); }); //監(jiān)聽WebSocket消息事件 $ws->on('message', function ($ws, $frame) { echo "Message: {$frame->data}\n"; $ws->push($frame->fd, "server: {$frame->data}"); }); //監(jiān)聽WebSocket連接關(guān)閉事件 $ws->on('close', function ($ws, $fd) { echo "client-{$fd} is closed\n"; }); $ws->start();
運(yùn)行程序:(這里還是要確認(rèn)監(jiān)聽的端口沒有被占用,如果被占用查看進(jìn)程PID,殺死進(jìn)程)
php ws_server.php
前端頁面js監(jiān)聽:(127.0.0.1改成你的主機(jī)地址)
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> html xmlns="http://www.w3.org/1999/xhtml"> head> title>WebSocket/title> /head> body> /body> script type="text/javascript"> var wsServer = 'ws://127.0.0.1:9501'; var websocket = new WebSocket(wsServer); websocket.onopen = function (evt) { console.log("Connected to WebSocket server."); }; websocket.onclose = function (evt) { console.log("Disconnected"); }; websocket.onmessage = function (evt) { console.log('Retrieved data from server: ' + evt.data); }; websocket.onerror = function (evt, e) { console.log('Error occured: ' + evt.data); }; /script> /html>
使用谷歌瀏覽器訪問前端頁面:
服務(wù)器端收到請求信息:
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP擴(kuò)展開發(fā)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《php正則表達(dá)式用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
標(biāo)簽:涼山 梅河口 遼陽 甘肅 昭通 九江 十堰 韶關(guān)
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Linux下源碼包安裝Swoole及基本使用操作圖文詳解》,本文關(guān)鍵詞 Linux,下,源碼,包安裝,包,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。