定制 jefyokta/oktaax 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jefyokta/oktaax

最新稳定版本:v3.7.0

Composer 安装命令:

composer require jefyokta/oktaax

包简介

Oktaax a openswoole http server library

README 文档

README

Version
PHP
OpenSwoole
MIT License

Oktaax is an OpenSwoole HTTP & WebSocket wrapper library. It is designed for developers who want to build high-performance, asynchronous PHP applications.

⚡ Quick Start

Requirements

  • PHP 8.1+
  • Swoole PHP extension

Installation

To install Oktaax, use Composer:

composer require jefyokta/oktaax

Hello World HTTP Example

Create a file named index.php with the following content:

<?php

require 'vendor/autoload.php';

use Oktaax\Oktaax;
use Oktaax\Http\Request;
use Oktaax\Http\Response;

$app = new Oktaax;

$app->get("/", function (Request $request, Response $response) {
    $response->end("Hello World");
});

$app->listen(3000);

Run the server with:

php index.php

Open your browser and navigate to http://localhost:3000.

🌐 All-In-One Server

If you need both HTTP and WebSocket support in one class, you can use the HasWebsocket trait:

<?php

require 'vendor/autoload.php';

use Oktaax\Oktaax;
use Oktaax\Websocket\HasWebsocket;
use Oktaax\Http\Request;
use Oktaax\Http\Response;
use Oktaax\Websocket\Client;
use Oktaax\Websocket\Server;

$app = new class extends Oktaax {
    use HasWebsocket;
};

$app->get("/user", function (Request $request, Response $response) {
    $response->end("User endpoint hit!");
});

$app->ws('welcome', function (Server $server, Client $client) {
    $server->reply($client, "Hi Client {$client->fd}");
});

$app->listen(3000);

Run the server as usual:

php index.php

📡 WebSocket

Here is an example of creating a WebSocket Channel:

Channel

Create a channel class that implements the Channel interface.

<?php

use Oktaax\Interfaces\Channel;
use Oktaax\Websocket\Client;

class EvenChannel implements Channel
{
    public function eligable(Client $client): bool
    {
        return $client->fd % 2 === 0;
    }
}
<?php

$app->ws('even', function (Server $server, Client $client) {
    $server->toChannel(EvenChannel::class)->broadcast(function ($client) {
        return "Hello {$client->fd}! You have an even fd!";
    });
});

HTTP

<?php

$app = new Oktaax;

$app
    ->get("/", fn($request, $response) => $response->end("Welcome to Oktaax"))
    ->get("/user/{username}", function ($request, $response) {
        $user = $request->params['username'];
        $response->end("Hello, {$user}");
    })
    ->post("/user", function ($request, $response) {
        $data = $request->post;
        $response->end("User created with data: " . json_encode($data));
    })
    ->put("/user/{id}", function ($request, $response) {
        $id = $request->params['id'];
        $response->end("User {$id} updated");
    })
    ->delete("/user/{id}", function ($request, $response) {
        $id = $request->params['id'];
        $response->end("User {$id} deleted");
    });

$app->listen(3000);

Trait and Server Options

Oktaax\Oktaax

is a class that include Requestable and WithBlade traits. You also can get it's instance with function oktaax()

统计信息

  • 总下载量: 85
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-07