本篇文章為大家展示了如何在Linux中安裝Swoole,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
下載好放到/usr/local/src下,解壓縮:
tar -zxvf swoole-2.2.0.tgz
準備擴展安裝編譯環境:
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擴展已經安裝成功:
查看phpinfo信息:
(測試前說明:以下使用的端口,要確認服務器放行,寶塔環境還需要添加安全組規則)
【創建TCP服務器】
創建server.php:
<?php //創建Server對象,監聽 127.0.0.1:9501端口 $serv = new swoole_server("127.0.0.1", 9501); //監聽連接進入事件 $serv->on('connect', function ($serv, $fd) { echo "Client: Connect.\n"; }); //監聽數據接收事件 $serv->on('receive', function ($serv, $fd, $from_id, $data) { $serv->send($fd, "Server: ".$data); }); //監聽連接關閉事件 $serv->on('close', function ($serv, $fd) { echo "Client: Close.\n"; }); //啟動服務器 $serv->start();
啟動TCP服務:
php server.php
查看9501端口已被監聽:
netstat -an | grep 9501
使用telnet連接TCP服務,輸入hello,服務器返回hello即測試成功:
telnet 127.0.0.1 9501
(如果telnet工具沒有安裝,執行yum install telnet
、yum install telnet-server
)
也可以寫一個TCP客戶端連接TCP服務器端:
創建tcp_client.php:
<?php //創建Client對象,監聽 127.0.0.1:9501端口 $client = new swoole_client(SWOOLE_SOCK_TCP); if(!$client->connect("127.0.0.1" ,9501)){ echo "連接失敗"; exit; } //向tcp服務器發送消息 fwrite(STDOUT, "請輸入:"); $msg = trim(fgets(STDIN)); $client->send($msg); //接受tcp服務器消息 $result = $client->recv(); echo $result;
啟動tcp客戶端:
php tcp_client.php
測試結果:
【創建UDP服務器】
創建udp_server.php:
<?php //創建Server對象,監聽 127.0.0.1:9502端口,類型為SWOOLE_SOCK_UDP $serv = new swoole_server("127.0.0.1", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP); //監聽數據接收事件 $serv->on('Packet', function ($serv, $data, $clientInfo) { $serv->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data); var_dump($clientInfo); }); //啟動服務器 $serv->start();
啟動UDP服務:
php udp_server.php
查看9502端口已被監聽:
netstat -an | grep 9502
使用netcat連接UDP服務,
輸入hello,服務器返回hello即測試成功(CentOS):
nc -u 127.0.0.1 9502
(如果沒有安裝netcat監聽器,執行yum install -y nc
)
【創建Web服務器】
創建http_server.php:
<?php $http = new swoole_http_server("0.0.0.0", 9501); //配置靜態文件根目錄(可選) $http->set([ 'document_root' => '/www/wwwroot/lwsblog', 'enable_static_handler' => true, ]); $http->on('request', function ($request, $response) { var_dump($request->get, $request->post); //設置header $response->header("Content-Type", "text/html; charset=utf-8"); //設置cookie $response->cookie("name", "lws", time()+3600); //發送Http響應體,并結束請求處理。 $response->end("<h2>Hello Swoole. #".rand(1000, 9999)."</h2>"); }); $http->start();
啟動服務:
php http_server.php
(如果9501端口已經被占用查看進程PID,殺死進程:)
lsof -i:9501
kill 9013
瀏覽器訪問主機地址:端口號,得到程序預期結果即測試成功:
【創建WebSocket服務器】
創建ws_server.php:
<?php //創建websocket服務器對象,監聽0.0.0.0:9501端口 $ws = new swoole_websocket_server("0.0.0.0", 9501); //配置靜態文件根目錄(可選) $ws ->set([ 'document_root' => '/www/wwwroot/lwsblog', 'enable_static_handler' => true, ]); //監聽WebSocket連接打開事件 $ws->on('open', function ($ws, $request) { var_dump($request->fd, $request->get, $request->server); $ws->push($request->fd, "hello, welcome\n"); }); //監聽WebSocket消息事件 $ws->on('message', function ($ws, $frame) { echo "Message: {$frame->data}\n"; $ws->push($frame->fd, "server: {$frame->data}"); }); //監聽WebSocket連接關閉事件 $ws->on('close', function ($ws, $fd) { echo "client-{$fd} is closed\n"; }); $ws->start();
運行程序:(這里還是要確認監聽的端口沒有被占用,如果被占用查看進程PID,殺死進程)
php ws_server.php
前端頁面js監聽:(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>
使用谷歌瀏覽器訪問前端頁面:
服務器端收到請求信息:
上述內容就是如何在Linux中安裝Swoole,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。