本文實例講述了Linux下源碼包安裝Swoole及基本使用操作。分享給大家供大家參考,具體如下:
下載Swoole PECL擴展源碼包:http://pecl.php.net/package/swoole
關(guān)于PHP版本依賴選擇:
![](/d/20211017/8552ea5467248ad8678a92915e61bbb7.gif)
下載好放到/usr/local/src下,解壓縮:
tar -zxvf swoole-2.2.0.tgz
準(zhǔn)備擴展安裝編譯環(huán)境:
phpize
![](/d/20211017/3fb324bc49e9e16edc439e1fb935ec09.gif)
查看php-config位置:
find / -name php-config
![](/d/20211017/8e9e70695b46a131df976db7d6e0bb15.gif)
配置:(--with-php-config==后面是你自己的php-config位置)
./configure --with-php-config=/www/server/php/72/bin/php-config
編譯安裝:
make make install
![](/d/20211017/a5adb47fc7342408bcafa751e20e81a6.gif)
在php.ini里面加一行 :
extension = swoole.so
使用 php -m 命令查看swoole擴展已經(jīng)安裝成功:
![](/d/20211017/6e6d2fec6e196c77f03dd37953160726.gif)
查看phpinfo信息:
![](/d/20211017/d09fa1704b5df0e487b009521909ed68.gif)
(測試前說明:以下使用的端口,要確認服務(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)聽連接進入事件
$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
![](/d/20211017/6529499723ff79da54970f1bb8f65911.gif)
使用telnet連接TCP服務(wù),輸入hello,服務(wù)器返回hello即測試成功:
telnet 127.0.0.1 9501
![](/d/20211017/d59d8dd94f4d060c6de3bc7f192637b7.gif)
(如果telnet工具沒有安裝,執(zhí)行yum install telnet
、yum install telnet-server
)
也可以寫一個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é)果:
![](/d/20211017/10fe382222a4c43d5a3bb7bf838c0819.gif)
【創(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
![](/d/20211017/9f22f10abf1424058f93f86fa40f536e.gif)
使用netcat連接UDP服務(wù),
輸入hello,服務(wù)器返回hello即測試成功(CentOS):
nc -u 127.0.0.1 9502
![](/d/20211017/6ab662f0cde3213e03e8551c9833f1e0.gif)
(如果沒有安裝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)被占用查看進程PID,殺死進程:)
![](/d/20211017/5c7819d3fba4173cdd210b2f8ed7dfc1.gif)
lsof -i:9501
![](/d/20211017/bf9a5400e1c8b2d2e1f6594a19f8229c.gif)
kill 9013
瀏覽器訪問主機地址:端口號,得到程序預(yù)期結(jié)果即測試成功:
![](/d/20211017/44b5f40d67db9ee02ed8ea3902eec6c8.gif)
【創(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();
運行程序:(這里還是要確認監(jiān)聽的端口沒有被占用,如果被占用查看進程PID,殺死進程)
php ws_server.php
前端頁面js監(jiān)聽:(127.0.0.1改成你的主機地址)
!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>
使用谷歌瀏覽器訪問前端頁面:
![](/d/20211017/089dd9efe4773d11e3eaa9297a87c5e0.gif)
服務(wù)器端收到請求信息:
![](/d/20211017/6ed9f95d44d7655fb19222df50e780cc.gif)
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP擴展開發(fā)教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php正則表達式用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:- php安裝swoole擴展的方法
- php異步多線程swoole用法實例
- PHP的swoole擴展安裝方法詳細教程
- 使用swoole擴展php websocket示例
- ThinkPHP5.0框架結(jié)合Swoole開發(fā)實現(xiàn)WebSocket在線聊天案例詳解
- PHP+swoole實現(xiàn)簡單多人在線聊天群發(fā)
- linux下安裝openssl、swoole等擴展的詳細步驟
- linux平臺編譯安裝PHP7并安裝Redis擴展與Swoole擴展實例教程
- 利用swoole+redis實現(xiàn)股票和區(qū)塊鏈服務(wù)
- docker搭建php+nginx+swoole+mysql+redis環(huán)境的方法
- 在PHP 7下安裝Swoole與Yar,Yaf的方法教程
- centos7環(huán)境下swoole1.9的安裝與HttpServer的使用方法分析