承接 initphp/socket 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

initphp/socket

最新稳定版本:1.1

Composer 安装命令:

composer require initphp/socket

包简介

Socket Server-Client Library

README 文档

README

PHP Socket (TCP, TLS, UDP, SSL) Server/Client Library

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Requirements

  • PHP 7.4 or higher
  • PHP Sockets Extension

Installation

composer require initphp/socket

Usage

Supported Types :

  • TCP
  • UDP
  • TLS
  • SSL

Factory

\InitPHP\Socket\Socket::class It allows you to easily create socket server or client.

Socket::server()

public static function server(int $handler = Socket::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketServerInterface
  • $handler : Socket::SSL, Socket::TCP, Socket::TLS or Socket::UDP
  • $host : Identifies the socket host. If not defined or left blank, it will throw an error.
  • $port : Identifies the socket port. If not defined or left blank, it will throw an error.
  • $argument : This value is the value that will be sent as 3 parameters to the constructor method of the handler.
    • SSL or TLS = (float) Defines the timeout period.
    • UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix"

Socket::client()

public static function client(int $handler = self::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketClientInterface
  • $handler : Socket::SSL, Socket::TCP, Socket::TLS or Socket::UDP
  • $host : Identifies the socket host. If not defined or left blank, it will throw an error.
  • $port : Identifies the socket port. If not defined or left blank, it will throw an error.
  • $argument : This value is the value that will be sent as 3 parameters to the constructor method of the handler.
    • SSL or TLS = (float) Defines the timeout period.
    • UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix"

Methods

connection() : Initiates the socket connection.

public function connection(): self;

disconnect() : Terminates the connection.

public function disconnect(): bool;

read() : Reads data from socket.

public function read(int $length = 1024): ?string;

write() : Writes data to the socket

public function write(string $string): ?int;

Server Methods

live() :

public function live(callable $callback): void;

wait() :

public function wait(int $second): void;

broadcast() :

public function broadcast(string $message, array|string|int|null $clients = null): bool;

Special methods for TLS and SSL.

TLS and SSL work similarly.

There are some additional methods you can use from TLS and SSL sockets.

timeout() : Defines the timeout period of the current.

public function timeout(int $second): self;

blocking() : Sets the blocking mode of the current.

public function blocking(bool $mode = true): self;

crypto() : Turns encryption on or off on a connected socket.

public function crypto(?string $method = null): self;

Possible values for $method are;

  • "sslv2"
  • "sslv3"
  • "sslv23"
  • "any"
  • "tls"
  • "tlsv1.0"
  • "tlsv1.1"
  • "tlsv1.2"
  • NULL

option() : Defines connection options for SSL and TLS. see; https://www.php.net/manual/en/context.ssl.php

public function option(string $key, mixed $value): self;

Socket Server

Example :

require_once "../vendor/autoload.php";
use \InitPHP\Socket\Socket;
use \InitPHP\Socket\Interfaces\{SocketServerInterface, SocketServerClientInterface};

$server = Socket::server(Socket::TLS, '127.0.0.1', 8080);
$server->connection();

$server->live(function (SocketServerInterface $socket, SocketServerClientInterface $client) {
    $read = $client->read();
    if (!$read) {
        return;
    }
    if (in_array($read, ['exit', 'quit'])) {
            $client->push("Goodbye!");
            $client->close();
        return;
    } else if (preg_match('/^REGISTER\s+([\w]{3,})$/i', $read, $matches)) {
        // REGISTER admin
        $name = trim(mb_substr($read, 9));
        $socket->clientRegister($name, $client);
    } else if (preg_match('/^SEND\s@([\w]+)\s(.*)$/i', $read, $matches)) {
        // SEND @admin Hello World
        $pushSocketName = $matches[1];
        $message = $matches[2];
        $socket->broadcast($message, [$pushSocketName])
    } else {
        $message = trim($read);
        !empty($message) && $socket->broadcast($message);
    }
});

Socket Client

Example :

require_once "../vendor/autoload.php";
use \InitPHP\Socket\Socket;

$client = Socket::client(Socket::SSL, 'smtp.gmail.com', 465);

$client->option('verify_peer', false)
    ->option('verify_peer_name', false);

$client->connection();

$client->write('EHLO [127.0.0.1]');

echo $client->read();

In the above example, a simple smtp connection to gmail is made.

Credits

License

Copyright © 2022 MIT License

统计信息

  • 总下载量: 3.82k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-18