# onError
## 说明:
~~~
callback Worker::$onError
~~~
当客户端的连接上发生错误时触发。
目前错误类型有
``` WM_ERROR_SESSION_CLOSED_BY_SERVER = 1001, //代表连接是被服务器关闭的,
WM_ERROR_SESSION_CLOSED_BY_CLIENT = 1002, //代表连接是被客户端关闭的。
WM_ERROR_SESSION_CLOSED = 1003, //代表连接是被客户端关闭的。
WM_ERROR_SEND_FAIL = 1004, //发送失败
WM_ERROR_SEND_BUFFER_FULL = 1005, //发送区满了
WM_ERROR_READ_FAIL = 1006, //接收失败
WM_ERROR_LOOP_FAIL = 1007, //LOOP相关错误
WM_ERROR_PROTOCOL_FAIL = 1008, //错误的协议
```
## 回调函数的参数
`$connection`
连接对象,即[TcpConnection实例](http://doc.workerman.net/tcp-connection.html),用于操作客户端连接,如[发送数据](http://doc.workerman.net/tcp-connection/send.html),[关闭连接](http://doc.workerman.net/tcp-connection/close.html)等
`$code`
错误码
`$msg`
错误消息
## 范例
~~~
$worker = new Worker('tcp://0.0.0.0:8484');
$worker->onError = function($connection, $code, $msg)
{
echo "error $code $msg\n";
};
// 运行worker
Worker::runAll();
~~~
提示:除了使用匿名函数作为回调,还可以[参考这里](http://doc.workerman.net/faq/callback_methods.html)使用其它回调写法。
- 序言
- 启动停止
- 简单的开发示例
- 性能测试
- 性能测试-数据库
- 通讯协议
- Coroutine类
- create
- getCid
- yield
- resume
- sleep
- wait
- getTotalNum
- isExist
- defer
- Runtime类
- Worker类
- 构造函数
- 属性
- workerId
- id
- name
- protocol
- transport
- reusePort
- backlog
- connections
- stdoutFile
- pidFile
- logFile
- user
- reloadable
- daemonize
- globalEvent
- 回调属性
- onWorkerStart
- onWorkerReload
- onConnect
- onMessage
- onClose
- onBufferFull
- onBufferDrain
- onError
- 方法
- rename
- runAll
- stopAll
- listen
- TcpConnection类
- 属性
- id
- protocol
- worker
- maxSendBufferSize
- defaultMaxSendBufferSize
- defaultMaxPackageSize
- 方法
- set
- send
- getRemoteIp
- getRemotePort
- close
- destroy
- pauseRecv
- resumeRecv
- pipe
- Timer定时器类
- add
- del
- 定时器注意事项
- Channel类
- 构造方法
- push
- pop
- length
- isEmpty()