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路径中的某个位置。接下来,在目录中运行以下命令(假设您已经安装并可以通过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