+
95
-

swoole如何编写一个高性能api接口服务?

php

请问swoole如何编写一个高性能api接口服务?

网友回复

+
15
-
<?php

$serv = new swoole_server('0.0.0.0', 9911, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);
$serv->set(array(
    'worker_num' => 4,
    'daemonize' => false,
    'backlog' => 128,
));

$serv->on('Connect', 'onConnect');
$serv->on('Receive', 'onReceive');
$serv->on('Close', 'onClose');

$serv->start();

/**
*Http建立连接回调函数
*/
function onConnect($serv, $fd) {
    echo "Server is running on 0.0.0.0:9911".PHP_EOL;
}

/**
* 开始接受客户端信息回调函数
*/
function onReceive($serv, $fd, $from_id, $data) {
    $_username = request($serv, $fd, $data, "username");
    $_password = request($serv, $fd, $data, "password");
    response($serv, $fd, json_encode(["youname" => $_username]));
}

/**
* 连接关闭后回调函数
*/
function onClose($serv, $fd) {
    return false;
}

/**
* 封装response 的内容
*/
function response($serv, $fd, $res_data = '') {

    //响应行
    $response = array(
        'HTTP/1.1 200',
    );
   ...

点击查看剩余70%

我知道答案,我要回答