慕码人2483693
class msgServer{private $serv;function __construct(){$this->serv = new Swoole\Server("127.0.0.1", 9501);//创建一个服务$this->serv->set(array('task_worker_num' => 4)); //配置task进程的数量$this->serv->on('receive', array($this, 'onReceive'));//有数据进来的时候执行$this->serv->on('task', array($this, 'onTask'));//有任务的时候执行$this->serv->on('finish', array($this, 'onFinish'));//任务结束时执行$this->serv->start();}public function onReceive($serv, $fd, $from_id, $data){$data = json_decode($data, true);$task_id = $serv->task($data);//这里发起了任务,于是上面的on('task', array($this, 'onTask'))就会执行}public function onTask($serv, $task_id, $from_id, $data){$data['send_res'] = $this->sendMsg($data); //发送短信//1.7.3之前,是$serv->finish("result");return "result.";//这里告诉任务结束,于是上面的on('finish', array($this, 'onFinish'))就会执行}public function onFinish($serv, $task_id, $data){$this->addSendLog($data); //添加短信发送记录}}