Download the PHP package easyswoole/socket without Composer

On this page you can find all versions of the php package easyswoole/socket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package socket

EasySwoole Socket

基于 Swoole Server 的 TCP、UDP 和 WebSocket 请求分发组件。它负责把原始数据解析为控制器调用、执行控制器 action,并按照响应状态完成编码、发送或关闭连接。

环境要求

安装:

核心流程

一次请求依次经过:

  1. Dispatcher 根据强类型入口创建 TCP、UDP 或 WebSocket 客户端信息对象。
  2. Parser 的 decode() 把原始数据转换为 Caller。
  3. Dispatcher 实例化 Caller 指定的 Controller。
  4. Controller 校验 action 白名单并执行 action。
  5. Parser 的 encode() 把 Response 转换为字符串。
  6. Dispatcher 根据 ResponseStatus 决定是否发送和关闭连接。

Parser

Parser 负责协议解析和响应编码:

decode() 返回 null 时不会执行控制器。encode() 返回 null 时,本次响应不会发送数据。

WebSocket 客户端类继承自 Tcp,因此接口统一使用 Tcp|Udp。需要读取 WebSocket 帧信息时可使用 instanceof WebSocket 判断。

Controller

Controller 会自动把业务控制器的 public 方法识别为可访问 action。需要禁止外部调用的方法应声明为 protected 或 private;Controller 基类方法和魔术方法不会作为 action:

responseImmediately() 直接返回 Swoole send()、sendto() 或 push() 的布尔结果。它不会检查连接是否存在;连接状态、失败处理和重试策略由控制器负责。

Config

Config 创建时必须同时提供 Socket 类型和 Parser:

异常处理器

异常处理器必须是 callable。未配置时,Dispatcher 会关闭 TCP/WebSocket 连接并重新抛出异常;UDP 没有连接关闭操作。

传入 null 可移除异常处理器。

Response 状态

ResponseStatus 提供四种状态:

状态 编码并发送 TCP/WebSocket 关闭 UDP 行为
OK 是 否 发送响应
WITHOUT_RESPONSE 否 否 不发送
RESPONSE_AND_CLOSE 是 是 仅发送响应
CLOSE 否 是 不发送

TCP 重置关闭:

WebSocket 关闭参数:

WebSocket 推送使用当前 Swoole flags 语义:

TCP 服务

TCP 粘包与拆包

TCP 是字节流协议,本身没有消息边界。一次 receive 可能只包含半个业务包,也可能同时包含多个业务包。项目不会替业务协议自动决定如何拆包,服务端必须根据实际协议选择固定长度、结束符或长度字段等方案。

例如,可以使用“4 字节网络字节序长度头 + 包体”的规则:

各配置项含义:

该配置与项目的 Protocol 工具保持一致:

启用上述服务端配置后,传入 Parser 的 TCP 原始数据包含 4 字节长度头。Parser 应先解包,再解析业务数据:

服务端响应也必须使用相同的长度协议。对应 Parser 的 encode() 可以这样实现:

客户端发送和接收示例:

客户端发送时也必须使用相同的封包规则。Tools\Client::send() 已自动调用 Protocol::pack();其他客户端需要自行封包。safeSend 默认为 false,此时只调用一次底层 send();设置为 true 时会循环发送,直到完整数据包写入、发送失败或超过整体超时时间。安全发送默认超时为 3 秒,可通过 timeout 参数调整;该参数在 safeSend=false 时不生效。大包或需要确保完整写入的场景建议启用安全发送。若安全发送在部分数据已经写入后失败或超时,不应直接重发整个业务请求,应先按业务协议确认服务端是否已经收到数据,避免重复处理。若业务使用结束符或其他自定义协议,应改用对应的 Swoole 配置和 Parser 实现,不要同时启用不匹配的拆包规则。

UDP 服务

WebSocket 服务

长度协议工具

Protocol 使用 4 字节网络字节序无符号整数作为包体长度:

默认最大包体为 1 MiB。socketReader() 可以为单次读取指定上限:

方法会拒绝短协议头、长度不匹配和超过上限的数据。

协程客户端

Tools\Client 同时支持 Unix Socket 和 TCP,并自动使用上述长度协议。

Unix Socket:

TCP:

连接失败时构造函数会抛出 RuntimeException,其中包含 Swoole 错误码和错误信息。

测试

测试覆盖 TCP、UDP、WebSocket 客户端信息、请求分发、响应发送、关闭路径、异常处理、协议边界和协程客户端连接失败。


All versions of socket with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1.0
ext-swoole Version *
easyswoole/spl Version ^2.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package easyswoole/socket contains the following files

Loading the files please wait ...