11

前言:都是为了生存

有收获的话请加颗小星星,没有收获的话可以 反对 没有帮助 举报三连

一、什么是swoole

Swoole:面向生产环境的 PHP 异步网络通信引擎

使 PHP 开发人员可以编写高性能的异步并发 TCP、UDP、Unix Socket、HTTP,WebSocket 服务。Swoole 可以广泛应用于互联网、移动通信、企业软件、云计算、网络游戏、物联网(IOT)、车联网、智能家居等领域。 使用 PHP + Swoole 作为网络通信框架,可以使企业 IT 研发团队的效率大大提升,更加专注于开发创新产品。

二、安装

1、pecl

pecl install swoole

2、源码安装

curl -O https://pecl.php.net/get/swoole-4.2.5.tgz
tar -zxvf swoole-4.2.5.tgz
cd swoole-4.2.5.tgz
phpize
./configure
make && make install
# 加入到php.ini中
php --ini
extension=swoole.so

三、基本入门

注意:示例代码都引入了"symfony/var-dumper"包进行美化打印

Swoole的绝大部分功能只能用于cli命令行环境

1、TCP服务器

<?php
// new
$server = new swoole_server("0.0.0.0", "9501",SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

// connect 连接
$server->on("connect", function (swoole_server $server, $fd) {
    dump("{$fd}连接");
    $server->send($fd, "欢迎{$fd}大山驴\n");
});
// receive 回调
$server->on("receive", function (swoole_server $server, $fd, $from_id, $data) {
    $server->send($fd, "服务端回复:{$data}\n");
    foreach ($server->connections as $connection) {
        if ($connection != $fd){
            $server->send($connection, "{$fd}说{$data}");
        }
    }

});
// close
$server->on("close", function (swoole_server $server, $fd) {
    dump("{$fd}关闭");
    foreach ($server->connections as $connection) {
        if ($connection != $fd){
            $server->send($connection, "{$fd}断开连接");
        }
    }
});
// start
$server->start();
php tcp_server.php

telnet 127.0.0.1 9501

2、UDP服务器

// new
<?php
$server = new swoole_server("0.0.0.0", "9502", SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
// packet
$server->on("Packet", function (swoole_server $server, $data, $clientInfo) {
    $server->sendto($clientInfo['address'], $clientInfo['port'], "服务器回复: {$data}");
    dump($clientInfo);
});
// start
$server->start();
php udp_server.php

netcat -u 127.0.0.1 9502

3、Web服务器

<?php
// new
$http = new swoole_http_server(HOST, "9503");
// request
$http->on("request", function (\Swoole\Http\Request $request, \Swoole\Http\Response $response) {
    dump($request->get, $request->post);
    $response->header("Content-Type", "text/html; charset=utf-8");
    $response->end("<h1>Hello Swoole. #" . mt_rand(1000, 9999) . "</h1>\n");
});
// start
$http->start();
php web_server.php

curl -XGET "127.0.0.1:9503?id=1&name=aa&age=26"
curl -XPOST "127.0.0.1:9503?id=1&name=aa&age=26" -d "love=like"

4、WebSocket服务器

服务端

<?php
// new
$ws = new swoole_websocket_server(HOST, "9504");
// open
$ws->on("open", function (Swoole\WebSocket\Server $ws, \Swoole\Http\Request $request) {
    dump($request->fd, $request->get, $request->server);
    $ws->push($request->fd, "你是大山驴\n");
});
// message
$ws->on("message", function (\Swoole\WebSocket\Server $ws, $frame) {
    dump("消息: {$frame->data}\n");
    $ws->push($frame->fd, "服务端回复: {$frame->data}\n");
});
// close
$ws->on("close", function (Swoole\WebSocket\Server $ws, $fd) {
    dump("{$fd}关闭");
});
// start
$ws->start();

客户端

let ws = new WebSocket("ws://127.0.0.1:9504");
ws.onopen = function (ws) {
    console.log("连接服务器");
};

ws.onclose = function (ws) {
    console.log("断开连接");
};

ws.onmessage = function (ws) {
    console.log('接收来自服务器的消息:' + ws.data);
};

ws.onerror = function (ws, event) {
    console.log('错误了:' + ws.data);
};

5、定时器

<?php
// 每2秒执行一次
swoole_timer_tick(2000, function ($time_id){
    dump($time_id);
});

// 3秒后执行
swoole_timer_after(3000, function (){
    dump("这是3s后");
});

OMGZui
651 声望25 粉丝

Is Engineer, not Programmer