文档
https://wiki.swoole.com/#/environment
安装
安装太麻烦了,直接在宝塔里面选择php版本,安装swoole扩展
查看当前php命令版本
php -v
如果不是刚刚安装swoole的版本,就设置一下
ln -sf /www/server/php/74/bin/php /usr/bin/php
最后查看swoole有没有安装成功
php -m
TCP协议
创建 服务端test1.php、客户端test1.php
<?php
//创建Server对象,监听 127.0.0.1:9501 端口。
$server = new Swoole\Server('127.0.0.1', 9501);
//监听连接进入事件。
$server->on('connect', function ($server, $fd) {
echo "客户端连接上了.\n";
});
//监听数据接收事件。
$server->on('receive', function ($server, $fd, $reactor_id, $data) {
echo "客户端发送过来的消息:{$data} \n";
$server->send($fd, "收到{$fd}的消息:{$data} \n");
});
//监听连接关闭事件。
$server->on('close', function ($server, $fd) {
echo "客户端离开.\n";
});
//启动服务器
$server->start();
{/tabs-pane}
{tabs-pane label="test2.php"}
<?php
//创建客户端
$client = new Swoole\Client(SWOOLE_SOCK_TCP);
if (!$client->connect('127.0.0.1', 9501, -1)) {
exit("connect failed. Error: {$client->errCode}\n");
}
$client->send("hello world\n");
echo $client->recv();
$client->close();
{/tabs-pane}
创建终端执行命令,运行服务端
php test1.php
运行结果
客户端连接上了.
客户端发送过来的消息:hello world
客户端离开.
再创建终端执行命令,运行客户端
php test2.php
运行结果
收到1的消息:hello world
UDP协议
<?php
/**
* UDP协议
*/
//创建Server对象
$server = new Swoole\Server('127.0.0.1', 9501,SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
//监听数据接收事件
$server->on('Packet', function ($server, $data, $clientInfo) {
echo $data;
echo "\n";
var_dump($clientInfo);
echo "\n";
$server->sendto($clientInfo['address'], $clientInfo['port'], "Server:{$data}");
});
//启动服务器
$server->start();
{/tabs-pane}
{tabs-pane label="test2.php 客户端"}
<?php
$client = new Swoole\Client(SWOOLE_SOCK_UDP,SWOOLE_SOCK_SYNC);
$client->sendto('127.0.0.1', 9501,"hello world\n");
echo $client->recv();
$client->close();
/*
$client = new Swoole\Client(SWOOLE_SOCK_UDP);
$client->connect('127.0.0.1', 9501);
$client->send("hello world\n");
echo $client->recv();
$client->close();
*/
{/tabs-pane}
同样的先启动服务端test1.php,再启动客户端test2.php访问服务端
php test1.php
php test2.php
TCP/UDP协议区别
TCP协议:
面向连接,发送数据前需要连接
提供可靠的服务,通过tcp协议传输数据,不丢失,无差错,不重复且按序到达
对系统资源要求较多
UDP协议:
无连接,发送数据前无需连接
udp协议尽力交付,但不保证交付
具有较好的实时性,工作效率比tcp高。适用于高速传输和实时性较高的通信和广播通信
对系统资源要求较少
Http协议
<?php
$http = new Swoole\Http\Server('0.0.0.0', 9502);
$http->on('Request', function ($request, $response) {
echo "来了!!";
$response->header('Content-Type', 'text/html; charset=utf-8');
$response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
});
$http->start();
浏览器访问 IP:端口
websocket
<?php
//websocket.php
//创建WebSocket Server对象,
$ws = new Swoole\WebSocket\Server('0.0.0.0', 9501);
//监听WebSocket连接打开事件
$ws->on('Open', function ($ws, $request) {
$ws->push($request->fd, "hello, {$request->fd},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();
{/tabs-pane}
{tabs-pane label="index.html"}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>恭喜,创建成功!</title>
</head>
<body>
<script>
var wsServer = 'ws://IP: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);
};
</script>
</body>
</html>
{/tabs-pane}
运行
php websocket.php
打开浏览器,F12并访问 index.html
websocket在线测试工具:http://www.websocket-test.com/
评论 (0)