2 回答

TA貢獻1860條經(jīng)驗 獲得超9個贊
class msgServer
{
private $serv;
function __construct()
{
$this->serv = new Swoole\Server("127.0.0.1", 9501);//創(chuàng)建一個服務(wù)
$this->serv->set(array('task_worker_num' => 4)); //配置task進程的數(shù)量
$this->serv->on('receive', array($this, 'onReceive'));//有數(shù)據(jù)進來的時候執(zhí)行
$this->serv->on('task', array($this, 'onTask'));//有任務(wù)的時候執(zhí)行
$this->serv->on('finish', array($this, 'onFinish'));//任務(wù)結(jié)束時執(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'))就會執(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'))就會執(zhí)行
}
public function onFinish($serv, $task_id, $data)
{
$this->addSendLog($data); //添加短信發(fā)送記錄
}
}
- 2 回答
- 0 關(guān)注
- 1215 瀏覽
添加回答
舉報