Giter VIP home page Giter VIP logo

tcp-sender's Introduction

TcpSender

Latest Stable Version Total Downloads GitHub license

Another TcpSender component for Hyperf.

Installation

  • Installation
composer require friendsofhyperf/tcp-sender

Usage

config/autoload/servers.php

'servers' => [
        [
            'name' => 'tcp',
            'type' => Server::SERVER_BASE,
            'host' => '0.0.0.0',
            'port' => 9401,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_CONNECT => [TcpServer::class,'onConnect'],
                Event::ON_CLOSE => [TcpServer::class,'onClose'],
                Event::ON_RECEIVE => [TcpServer::class,'onReceive'],
            ],
            'options' => [
                // Whether to enable request lifecycle event
                'enable_request_lifecycle' => false,
            ],
        ]
    ],

多进程模型

TcpServer

<?php

namespace App;

use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnReceiveInterface;
use FriendsOfHyperf\TcpSender\Sender;
use Swoole\Server;

class TcpServer implements OnCloseInterface,OnReceiveInterface
{
    public function __construct(private Sender $sender)
    {
    }

    /**
     * @param Server $server
     */
    public function onConnect($server, $fd, $reactorId): void
    {
        $server->send($fd, sprintf('Client %s connected.'.PHP_EOL, $fd));
    }

    public function onClose($server, int $fd, int $reactorId): void
    {
        $server->send($fd, sprintf('Client %s closed.'.PHP_EOL, $fd));
    }

    public function onReceive($server, int $fd, int $reactorId, string $data): void
    {
        $server->send($fd, sprintf('Client %s send: %s'.PHP_EOL, $fd, $data));
        var_dump($data);
    }


}

协程风格服务 单进程模型

TcpServer

namespace App;

use Hyperf\Contract\OnReceiveInterface;
use FriendsOfHyperf\TcpSender\Sender;
use Swoole\Coroutine\Server\Connection;
use Swoole\Server;

class TcpServer implements OnReceiveInterface
{
    public function __construct(private Sender $sender)
    {
    }

    public function onConnect(Connection $connection, $fd): void
    {
        // 设置 fd 和 connection 的映射关系
        $this->sender->setResponse($fd,$connection);
        $connection->send(sprintf('Client %s connected.'.PHP_EOL, $fd));
    }

    public function onClose($connection, int $fd): void
    {
        // 删除 fd 和 connection 的映射关系
        $this->sender->setResponse($fd,null);
    }

    public function onReceive($server, int $fd, int $reactorId, string $data): void
    {
        $server->send($fd, sprintf('Client %s send: %s'.PHP_EOL, $fd, $data));
    }


}

YourService or YourController

<?php

declare(strict_types=1);

namespace App\Controller;

use FriendsOfHyperf\TcpSender\Sender;

class IndexController extends AbstractController
{
    public function __construct(private Sender $sender)
    {
    }

    public function index()
    {
        // 向指定的fd发送消息
        $this->sender->send(1, 'Hello Hyperf.');
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}

Contact

License

MIT

tcp-sender's People

Contributors

gumphp avatar zds-s avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.