eventHandler

说明:

  1. callback BusinessWorker::$eventHandler

(注意:此特性需要GatewayWorker版本>=2.0.2,如何查看版本号参考《常见问题》一章)

(此属性一般不用设置)

设置使用哪个类来处理业务,默认值是Events,即默认使用Events.php中的Events类来处理业务。业务类至少要实现onMessage静态方法,onConnect和onClose静态方法可以不用实现。

(注意:Gateway 2.0.4版本以前业务处理类默认为Event,为了避免和Event扩展冲突,2.0.4版本以后统一改成Events)

范例

Applications\项目\start_businessworker.php

  1. use \Workerman\Worker;
  2. use \GatewayWorker\BusinessWorker;
  3. $worker = new BusinessWorker();
  4. $worker->name = 'ChatBusinessWorker';
  5. $worker->count = 4;
  6. $worker->registerAddress = '127.0.0.1:1236';
  7. // 设置处理业务的类为MyEvent
  8. $worker->eventHandler = 'MyEvent';
  9. if(!defined('GLOBAL_START'))
  10. {
  11. Worker::runAll();
  12. }

Applications\项目\MyEvent.php

  1. <?php
  2. use \GatewayWorker\Lib\Gateway;
  3. // MyEvent类
  4. class MyEvent
  5. {
  6. public static function onMessage($client_id, $message)
  7. {
  8. Gateway::sendToCurrentClient('works');
  9. }
  10. }