[TOC]
* * * * *
## 1 源代码
### (onWorkerStart)worker启动回调
~~~
public $onWorkerStart = null;
worker::run()
if ($this->onWorkerStart) {
try {
call_user_func($this->onWorkerStart, $this);
} catch (\Exception $e) {
echo $e;
exit(250);
}
}
~~~
### (onWorkerReload)worker重载回调
~~~
public $onWorkerReload = null;
worker::reload()
if ($worker->onWorkerReload) {
try {
call_user_func($worker->onWorkerReload, $worker);
} catch (\Exception $e) {
echo $e;
exit(250);
}
}
if ($worker->reloadable) {
self::stopAll();
}
~~~
### (onWorkerStop)worker停止回调函数
~~~
public $onWorkerStop = null;
worker::stop()
if ($this->onWorkerStop) {
try {
call_user_func($this->onWorkerStop, $this);
} catch (\Exception $e) {
echo $e;
exit(250);
}
}
~~~
### (onConnect)客户端连接时回调函数
~~~
public $onConnect = null;
worker::acceptConnection()
if ($this->onConnect) {
try {
call_user_func($this->onConnect, $connection);
} catch (\Exception $e) {
echo $e;
exit(250);
}
}
~~~
### (onMessage)接受到客户端数据时回调函数
~~~
public $onMessage = null;
worker::__construct()
{
......
$this->onMessage = function () {
};
}
~~~
~~~
worker::acceptConnection()
{
......
$connection->onMessage = $this->onMessage;
......
}
~~~
~~~
worker::acceptUdpConnection()
{
......
if ($this->onMessage) {
if ($this->protocol) {
$parser = $this->protocol;
$recv_buffer = $parser::decode($recv_buffer, $connection);
}
ConnectionInterface::$statistics['total_request']++;
try {
call_user_func($this->onMessage, $connection, $recv_buffer);
} catch (\Exception $e) {
echo $e;
exit(250);
}
}
.....
}
~~~
### (onClose)客户端连接断开时回调
~~~
public $onClose = null;
worker::acceptConnection()
$connection->onClose = $this->onClose;
~~~
### (onBufferFull )发送缓冲区数据达到上限回调函数
~~~
public $onBufferFull = null;
worker::acceptConnection()
$connection->onBufferFull = $this->onBufferFull;
~~~
### (onBufferDrain )发送缓冲区数据发送完毕回调函数
~~~
public $onBufferDrain = null;
worker::acceptConnection()
$connection->onBufferDrain = $this->onBufferDrain;
~~~
### (onError ) 客户端连接发送错误时回调
~~~
public $onError = null;
worker::acceptConnection()
$connection->onError = $this->onError;
~~~
## 2 文件分析
### 1 worker相关
onWokerStart: worker启动回调
onWorkerReload: worker重载回调
onWokrerStop: worker停止回调
### 2 connect相关
onConnet: 客户端连接建立时回调
onMessage: 服务器接受到数据时回调
onClose: 客户端连接关闭时回调
onBufferFull: 数据缓冲区达到上限时回调
onBufferDrain: 数据缓冲区发送完毕时回调
onError: 客户端连接错误时回调
## 3 总结
事件接口注册相关函数。
Worker接口 3个
Connect接口 4个