1 回答

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊
class msgServer
{
private $serv;
function __construct()
{
$this->serv = new Swoole\Server("127.0.0.1", 9501);//創(chuàng)建一個(gè)服務(wù)
$this->serv->set(array('task_worker_num' => 4)); //配置task進(jìn)程的數(shù)量
$this->serv->on('receive', array($this, 'onReceive'));//有數(shù)據(jù)進(jìn)來(lái)的時(shí)候執(zhí)行
$this->serv->on('task', array($this, 'onTask'));//有任務(wù)的時(shí)候執(zhí)行
$this->serv->on('finish', array($this, 'onFinish'));//任務(wù)結(jié)束時(shí)執(zhí)行
$this->serv->start();
}
public function onReceive($serv, $fd, $from_id, $data)
{
$data = json_decode($data, true);
$task_id = $serv->task($data);//這里發(fā)起了任務(wù),于是上面的on('task', array($this, 'onTask'))就會(huì)執(zhí)行
}
public function onTask($serv, $task_id, $from_id, $data)
{
$data['send_res'] = $this->sendMsg($data); //發(fā)送短信
//1.7.3之前,是$serv->finish("result");
return "result.";//這里告訴任務(wù)結(jié)束,于是上面的on('finish', array($this, 'onFinish'))就會(huì)執(zhí)行
}
public function onFinish($serv, $task_id, $data)
{
$this->addSendLog($data); //添加短信發(fā)送記錄
}
}
- 1 回答
- 0 關(guān)注
- 775 瀏覽
添加回答
舉報(bào)