安裝swoole
以mac操作系統(tǒng)為例,如果你是mac新手啥刻,推薦閱讀 程序員如何優(yōu)雅使用mac
環(huán)境要求:php版本大于7.0
? swoole php -v
PHP 7.1.19 (cli) (built: Jun 25 2018 10:42:21) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.1.19, Copyright (c) 1999-2018, by Zend Technologies
使用pecl
安裝swoole应狱,安裝過程中绑嘹,會提示你是否需要安裝某些擴展郭脂,可自主選擇yes或no年碘,如果是選擇安裝redis擴展,本機需要安裝redis環(huán)境
pecl install swoole
選擇redis擴展需要先安裝相應(yīng)的庫
brew install redis
brew install hiredis
ide自動提示
默認情況下展鸡,phpstorm
并不會自動提示swoole擴展包的相關(guān)函數(shù)屿衅,需要借助 swoole-ide-helper 實現(xiàn)自動提示
安裝方法:
在項目的根目錄執(zhí)行:
composer require --dev "eaglewu/swoole-ide-helper:dev-master"
對于compoesr不熟悉的同學(xué),請參閱這一次莹弊,真正掌握composer
接下來使用swoole搭建tcp涤久,udp, http, websocket 服務(wù),體驗swoole的基本使用
tcp 服務(wù)
新建server.php
<?php
// 創(chuàng)建Server對象忍弛,監(jiān)聽 127.0.0.1:9501 端口
$serv = new swoole_server("127.0.0.01", 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();
服務(wù)端啟動:
? server [master] ? php server.php
[2018-04-26 09:08:59 @96245.0] TRACE Create swoole_server host=127.0.0.01, port=9501, mode=3, type=1
telnet 測試連接
? ~ telnet 127.0.0.1 9501
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello
Server: hello
退出telnet的方式:ctrl+], 然后再按 ctrl+d
udp 服務(wù)
新建udp_server.php
<?php
// 創(chuàng)建UDP Server對象
$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();
? server [master] ? php udp_server.php
[2018-04-26 09:18:10 @97828.0] TRACE Create swoole_server host=127.0.0.1, port=9502, mode=3, type=2
? ~ netcat -u 127.0.0.1 9502
hello
Server hello
http 服務(wù)
<?php
$http = new swoole_http_server('0.0.0.0', 9502);
$http->on('request', function ($request, $response) {
$response->header("Content-Type", "text/html; charset=utf-8");
$time = date('Y-m-d H:i:s', time());
$response->end("<h1>{$time}--這是swoole提供的http服務(wù)响迂,修改代碼后要重啟服務(wù)才能生效</h1>");
});
$http->start();
? http php http_server.php
[2018-07-27 09:25:05 @53999.0] TRACE Create swoole_server host=0.0.0.0, port=9502, mode=3, type=1
瀏覽器訪問:http://127.0.0.1:9502/
swoole-http
websocket 服務(wù)
ws 服務(wù)端
新建server.php
文件
<?php
class Ws {
CONST HOST = "0.0.0.0";
CONST PORT = 8812;
public $ws = null;
public function __construct()
{
$this->ws = new swoole_websocket_server(self::HOST, self::PORT);
$this->ws->set(
[
'worker_num' => 2,
]
);
$this->ws->on('open', [$this, 'onOpen']);
$this->ws->on('message', [$this, 'onMessage']);
$this->ws->on('close', [$this, 'onClose']);
$this->ws->start();
}
/**
* 監(jiān)聽連接事件
* @param $ws
* @param $request
*/
public function onOpen($ws, $request)
{
echo "建立連接,客戶端id:{$request->fd}\n";
}
/**
* 監(jiān)聽消息事件
* @param $ws
* @param $frame
*/
public function onMessage($ws, $frame)
{
echo "客戶端發(fā)送的數(shù)據(jù): {$frame->data}\n";
$pushData = date("Y-m-d H:i:s");
$ws->push($frame->fd, "服務(wù)端推送的數(shù)據(jù): {$pushData}");
}
/**
* 監(jiān)聽關(guān)閉事件
* @param $ws
* @param $fd
*/
public function onClose($ws, $fd)
{
echo "客戶端:{$fd} 關(guān)閉了連接\n";
}
}
$ws = new Ws();
ws 客戶端
新建client.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>websocket client</title>
</head>
<body>
<h1>swoole-websocket測試</h1>
<script>
var wsUrl = "ws://127.0.0.1:8812";
var websocket = new WebSocket(wsUrl);
websocket.onopen = function(evt) {
websocket.send("我是客戶端细疚,我要連接服務(wù)端");
console.log('連接成功');
}
websocket.onmessage = function(evt) {
console.log("接收到的服務(wù)端信息: " + evt.data);
}
websocket.onclose = function(evt) {
console.log("服務(wù)端關(guān)閉了");
}
websocket.onerror = function(evt, e) {
console.log("出錯了: " + evt.data);
}
</script>
</body>
</html>
結(jié)果顯示
開啟ws服務(wù):
ws server
瀏覽器打開ws_client.html
文件:
ws client
如果覺得本文對你有所幫助蔗彤,點個贊,或者賞杯咖啡錢疯兼,你的認可對我很重要