PHP Class MyQEE\Server\Worker

Datei anzeigen Open project: myqee/server

Public Properties

Property Type Description
$id integer 当前进程的唯一ID
$name string 工作进程服务对象的name, 主端口为 Main
$serverId integer 当前进程的服务器ID
$serverName string 服务器名
$worker WorkerMain | WorkerTCP | WorkerUDP | WorkerWebSocket 在自定义对象里有次对象

Protected Properties

Property Type Description
$server Swoole\Server | Swoole\Http\Server | Swoole\Websocket\Server
$startTime integer 当前进程启动时间
$time integer 当前时间

Public Methods

Method Description
__construct ( Swoole\Server $server ) WorkerBase constructor.
log ( string $label, array $data = null, string $type = 'other', string $color = '[36m' ) 输出自定义log
onClose ( $server, $fd, $fromId ) 关闭连接回调
onConnect ( $server, $fd, $fromId ) 连接服务器回调
onFinish ( $server, $taskId, $data )
onPipeMessage ( Swoole\Server $server, integer $fromWorkerId, mixed $message, integer $serverId ) : mixed 接受到任意进程的调用
onStart ( ) 进程启动后执行 (空方法, 可自行扩展)
onStop ( ) 退出程序是回调
sendMessage ( $data, integer $workerId, integer $serverId, string $serverGroup = null ) : boolean 向任意 worker 进程或者 task 进程发送消息
task ( $data, integer $workerId, Closure $callback = null, integer $serverId, string $serverGroup = null ) : boolean | integer 投递任务
taskWait ( mixed $taskData, float $timeout = 0.5, integer $workerId, integer $serverId, string $serverGroup = null ) : mixed 阻塞的投递信息

Protected Methods

Method Description
debug ( string | array $labelOrData, array $data = null ) 调试信息
info ( string | array $labelOrData, array $data = null ) 输出信息
timeTick ( integer $interval, string | array | Closure $callback, mixed | null $params = null ) 增加一个优化执行时间间隔的定时器
trace ( string | array $labelOrData, array $data = null ) 跟踪信息
warn ( string | array $labelOrData, array $data = null ) 错误信息

Method Details

__construct() public method

WorkerBase constructor.
public __construct ( Swoole\Server $server )
$server Swoole\Server

debug() protected method

调试信息
protected debug ( string | array $labelOrData, array $data = null )
$labelOrData string | array
$data array

info() protected method

输出信息
protected info ( string | array $labelOrData, array $data = null )
$labelOrData string | array
$data array

log() public method

输出自定义log
public log ( string $label, array $data = null, string $type = 'other', string $color = '[36m' )
$label string
$data array
$type string
$color string

onClose() public method

关闭连接回调
public onClose ( $server, $fd, $fromId )
$server
$fd
$fromId

onConnect() public method

连接服务器回调
public onConnect ( $server, $fd, $fromId )
$server
$fd
$fromId

onFinish() public method

public onFinish ( $server, $taskId, $data )
$server
$taskId
$data

onPipeMessage() public method

接受到任意进程的调用
public onPipeMessage ( Swoole\Server $server, integer $fromWorkerId, mixed $message, integer $serverId ) : mixed
$server Swoole\Server
$fromWorkerId integer
$message mixed
$serverId integer
return mixed

onStart() public method

进程启动后执行 (空方法, 可自行扩展)
public onStart ( )

onStop() public method

退出程序是回调
public onStop ( )

sendMessage() public method

和 swoole 不同的是, 它支持服务器集群下向任意集群去投递数据
public sendMessage ( $data, integer $workerId, integer $serverId, string $serverGroup = null ) : boolean
$data
$workerId integer
$serverId integer
$serverGroup string
return boolean

task() public method

和 swoole 不同的是, 它支持服务器集群下向任意集群去投递数据
public task ( $data, integer $workerId, Closure $callback = null, integer $serverId, string $serverGroup = null ) : boolean | integer
$data
$workerId integer
$callback Closure
$serverId integer 默认 -1 则优先本地投递
$serverGroup string
return boolean | integer

taskWait() public method

阻塞的投递信息
public taskWait ( mixed $taskData, float $timeout = 0.5, integer $workerId, integer $serverId, string $serverGroup = null ) : mixed
$taskData mixed
$timeout float
$workerId integer
$serverId integer
$serverGroup string
return mixed

timeTick() protected method

如果你有一个定时器任务会在每个进程上运行, 但是又不希望所有的定时器在同一刹那执行, 那么用这个方法非常适合, 它可以根据进程数将定时器执行的时间分散开. 例如你启动了10个worker进程, 定时器是间隔10秒执行1次, 那么正常情况下, 这10个进程会在同1秒执行, 在下一个10秒又同时执行... 而通过本方法添加的定时器是这样执行的: 进程1会在 00, 10, 20, 30, 40, 50秒执行, 进程2会在 01, 11, 21, 31, 41, 51秒执行, .... 进程9会在 09, 19, 29, 39, 49, 59秒执行. 每个进程运行的间隔仍旧是10秒钟, 但是它不会和其它进程在同一时间执行
protected timeTick ( integer $interval, string | array | Closure $callback, mixed | null $params = null )
$interval integer 时间间隔, 单位: 毫秒
$callback string | array | Closure 回调函数
$params mixed | null

trace() protected method

跟踪信息
protected trace ( string | array $labelOrData, array $data = null )
$labelOrData string | array
$data array

warn() protected method

错误信息
protected warn ( string | array $labelOrData, array $data = null )
$labelOrData string | array
$data array

Property Details

$id public_oe property

当前进程的唯一ID
public int $id
return integer

$name public_oe property

工作进程服务对象的name, 主端口为 Main
public string $name
return string

$server protected_oe property

protected Server,Swoole|Server,Swoole\Http|Server,Swoole\Websocket $server
return Swoole\Server | Swoole\Http\Server | Swoole\Websocket\Server

$serverId public_oe property

当前进程的服务器ID
public int $serverId
return integer

$serverName public_oe static_oe property

服务器名
public static string $serverName
return string

$startTime protected_oe static_oe property

当前进程启动时间
protected static int $startTime
return integer

$time protected_oe static_oe property

当前时间
protected static int $time
return integer

$worker public_oe property

在自定义对象里有次对象
public WorkerMain|WorkerTCP,MyQEE\Server|WorkerUDP,MyQEE\Server|WorkerWebSocket,MyQEE\Server $worker
return WorkerMain | WorkerTCP | WorkerUDP | WorkerWebSocket