PHP 的异步并行和协程 C 扩展 Swoole

码农软件 · 软件分类 · 高性能网络开发库 · 2019-09-01 13:59:23

软件介绍

PHP的异步、并行、高性能网络通信引擎,使用纯C语言编写,提供了PHP语言的异步多线程服务器异步TCP/UDP网络客户端异步MySQL异步Redis数据库连接池AsyncTask消息队列毫秒定时器异步文件读写异步DNS查询。 Swoole内置了Http/WebSocket服务器端/客户端Http2.0服务器端

Swoole底层内置了异步非阻塞、多线程的网络IO服务器。PHP程序员仅需处理事件回调即可,无需关心底层。与Nginx/Tornado/Node.js等全异步的框架不同,Swoole既支持全异步,也支持同步。

除了异步IO的支持之外,Swoole为PHP多进程的模式设计了多个并发数据结构和IPC通信机制,可以大大简化多进程并发编程的工作。其中包括了并发原子计数器并发HashTableChannelLock进程间通信IPC等丰富的功能特性。

Swoole从2.0版本开始支持了内置协程,可以使用完全同步的代码实现异步程序。PHP代码无需额外增加任何关键词,底层自动进行协程调度,实现异步。

Swoole可以广泛应用于互联网、移动通信、企业软件、网络游戏、物联网、车联网、智能家庭等领域。 使用PHP+Swoole作为网络通信框架,可以使企业IT研发团队的效率大大提升,更加专注于开发创新产品。

Swoole是开源免费的自由软件,授权协议是Apache2.0。企业和个人开发者均可免费使用Swoole的代码,并且在Swoole之上所作的修改可用于商业产品,无需开源(注:必须保留原作者的版权声明)。

    Server

    $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, 'Swoole: '.$data);
        $serv->close($fd);
    });
    $serv->on('close', function ($serv, $fd) {
        echo "Client: Close.\n";
    });
    $serv->start();

    Client

    $client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
    $client->on("connect", function($cli) {
        $cli->send("hello world\n");
    });
    $client->on("receive", function($cli, $data = ""){
        echo "Receive: ".$data.PHP_EOL;
    });
    $client->on("error", function($cli){
        echo "connect fail\n";
    });
    $client->on("close", function($cli){
        echo "connection close\n";
    });
    $client->connect('127.0.0.1', 9501, 0.5);

    Http服务器

    $serv = new swoole_http_server("127.0.0.1", 9502);
    
    $serv->on('Request', function($request, $response) {
        var_dump($request->get);
        var_dump($request->post);
    
        $response->cookie("User", "Swoole");
        $response->header("X-Server", "Swoole");
        $response->end("Hello Swoole!");
    });
    
    $serv->start();

    WebSocket服务器

    $serv = new swoole_websocket_server("127.0.0.1", 9502);
    
    $serv->on('Open', function($server, $req) {
        echo "connection open: ".$req->fd;
    });
    
    $serv->on('Message', function($server, $frame) {
        echo "message: ".$frame->data;
        $server->push($frame->fd, json_encode(["hello", "world"]));
    });
    
    $serv->on('Close', function($server, $fd) {
        echo "connection close: ".$fd;
    });
    
    $serv->start();

    本文地址:https://www.codercto.com/soft/d/13644.html

    Introduction to Programming in Java

    Introduction to Programming in Java

    Robert Sedgewick、Kevin Wayne / Addison-Wesley / 2007-7-27 / USD 89.00

    By emphasizing the application of computer programming not only in success stories in the software industry but also in familiar scenarios in physical and biological science, engineering, and appli......一起来看看 《Introduction to Programming in Java》 这本书的介绍吧!

    图片转BASE64编码
    图片转BASE64编码

    在线图片转Base64编码工具

    Base64 编码/解码
    Base64 编码/解码

    Base64 编码/解码

    RGB CMYK 转换工具
    RGB CMYK 转换工具

    RGB CMYK 互转工具