# onMessage
## 说明:
~~~
callback Worker::$onMessage
~~~
当客户端通过连接发来数据时(Workerman收到数据时)触发的回调函数
## 回调函数的参数
`$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)等
`$data`
客户端连接上发来的数据,如果Worker指定了协议,则$data是对应协议decode(解码)了的数据
## 范例
~~~
$worker = new Worker('tcp://0.0.0.0:8484');
$worker->onMessage = function($connection, $data)
{
var_dump($data);
$connection->send('receive success');
};
// 运行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()