navarr/sockets

PHP中的Sockets

维护者

详细信息

github.com/navarr/Sockets

源代码

问题

资助包维护!
navarr

v0.3.0 2022-01-16 19:25 UTC

This package is auto-updated.

Last update: 2024-09-17 01:35:13 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License
Tests Code Coverage Mutation Score

Sockets 是一个旨在使 PHP Sockets 的工作更容易的 PHP 库,包括 Socket 服务器的创建和管理。

进行中

代码目前仍在进行中,Socket 类本身尚未完全完成。我还需要了解很多关于如何在 PHP 和可能在 C 中工作才能让一切运行得非常出色。

并非所有内容都经过测试,并非所有内容都能正常工作。

建议在创建 git tag 1.0.0 之前不要严重使用此库。在那之前将会有破坏性的更改。

SocketServer 的使用

使用 SocketServer 应该是一个简单且容易的任务(并且类应该有足够的文档来理解它在做什么,而无需我解释)。

ECHO 服务器示例

<?php

use Navarr\Socket\Socket;
use Navarr\Socket\Server;

class EchoServer extends Server
{
    const DEFAULT_PORT = 7;

    public function __construct($ip = null, $port = self::DEFAULT_PORT)
    {
        parent::__construct($ip, $port);
        $this->addHook(Server::HOOK_CONNECT, array($this, 'onConnect'));
        $this->addHook(Server::HOOK_INPUT, array($this, 'onInput'));
        $this->addHook(Server::HOOK_DISCONNECT, array($this, 'onDisconnect'));
        $this->run();
    }

    public function onConnect(Server $server, Socket $client, $message)
    {
        echo 'Connection Established',"\n";
    }

    public function onInput(Server $server, Socket $client, $message)
    {
        echo 'Received "',$message,'"',"\n";
        $client->write($message, strlen($message));
    }

    public function onDisconnect(Server $server, Socket $client, $message)
    {
        echo 'Disconnection',"\n";
    }
}

$server = new EchoServer('0.0.0.0');

开发

文档

此项目使用 phpdoc 生成文档。要生成文档,您需要满足一些依赖项。首先,您需要获取 graphviz。它可以通过大多数 Linux 发行版获得,但如果您不在 Linux 上,您也可以从网站下载并安装它。如果您手动安装,请确保二进制文件已添加到您的 PATH 中。接下来,在以下目录中运行以下命令(假设您已经安装了 composer 并将其作为 composer 添加到您的路径)。

composer install # this will install all of the development dependencies for this project
vendor/bin/phpdoc -d ./src -t ./docs # this will generate the documentation into a docs directory