异步发送验证码没返回结果
来源:7-14 登录模块优化(一)
MrWhite丶
2018-06-12
http.php
<?php
class Http {
CONST HOST = '0.0.0.0';
CONST PORT = 8811;
public $http = null;
public function __construct() {
$this->http = new swoole_http_server(self::HOST,self::PORT);
$this->http->set(
[
'enable_static_handler' => true,
'document_root' => "/data/www/swoole/thinkphp/public/static",
]
);
$this->http->on('workerstart',[$this,'onWorkerStart']);
$this->http->on('request',[$this,'onRequest']);
$this->http->on('task',[$this,'onTask']);
$this->http->on('finish',[$this,'onFinish']);
$this->http->on('close',[$this,'onClose']);
$this->http->start();
}
public function onWorkerStart($server, $worker_id) {
define('APP_PATH',__DIR__.'/../application/');
require __DIR__.'/../thinkphp/start.php';
}
public function onRequest($request,$response) {
$_SERVER = [];
if(isset($request->server)) {
foreach($request->server as $key => $val){
$_SERVER[strtoupper($key)] = $val;
}
}
if(isset($request->header)) {
foreach($request->header as $key => $val){
$_SERVER[strtoupper($key)] = $val;
}
}
$_POST = [];
if(isset($request->post)) {
foreach($request->post as $key => $val){
$_POST[$key] = $val;
}
}
$_GET = [];
if(isset($request->get)) {
foreach($request->get as $key => $val){
$_GET[$key] = $val;
}
}
$_POST['http_server'] = $this->http;
ob_start();
try{
think\Container::get('app', [defined('APP_PATH') ? APP_PATH : ''])
->run()
->send();
}catch (Exception $e) {
}
$res = ob_get_contents();
ob_end_clean();
$response->end($res);
}
//
public function onTask($server,$taskId,$workerId,$data) {
try{
$sms = new app\common\lib\Sms();
$result = $sms->sendSms($data['phone'],[$data['code']]);
$result = json_decode(json_encode($result),true);
if($result['statusCode'] != 0) {
return Util::show($result['statusCode'],'短信SMS发送异常!');
}else{
//redis
$redis = new \Swoole\Coroutine\Redis();
$redis->connect(config('redis.host'),config('redis.port'));
$redis->set(Redis::smsKey($phoneNum),$code,config('redis.out_time'));
}
}catch(\Exception $e) {
var_dump($e->getMessage());
return Util::show(config('code.error'),'短信SMS发送异常!');
}
}
public function onFinish($server,$taskId,$data) {
echo "taskId:{$taskId}\n";
echo "finish data is :{$data}";
}
// 关闭
public function onClose($ws,$fd) {
echo "clientid:{$fd}\n";
}
}
new Http(); public function sms(){
//$phoneNum = request()->get('phone_num',0,'intval');
$phoneNum = intval($_GET['phone_num']);
if(empty($phoneNum)) {
return Util::show(config('code.error'),'手机号不能为空!');
}
// if(strlen($phone_num) != 11) {
// echo Util::show(config('code.error'),'手机号长度不正确!');
// }
$code = rand(1000,9999);
$data = [
'phone' => $phoneNum,
'code' => $code
];
$_POST['http_server']->task($data);
return Util::show(config('code.success'),'发送成功!');
}network:


都是按照视频的写的
但是短信验证码我是用的容联云。目测和这个没关系。因为前面是可以发送的
另外就是根本不进task方法
最重要的就是。啥错都不报。。
写回答
2回答
-
zxr615
2018-08-21
我也遇到这个问题了,解决方法如图,需要设置 worker_num 和 task_worker_num

过程:
和题主一样,没有任何报错,后来我在每个onxxx回调函数内echo了一个内容,如图:

发现控制台请求的时候终端并没有触发onTask方法,只触发了链接后的onWorkerStart和请求后的onRequest方法,然后我就在onRequest方法中直接创建onTask方法,如图

这时再重启服务请求就终端就报错了,从而找到问题所在了

琢磨出来的一点点小经验,和大家分享分享。
10 -
singwa
2018-06-13
你要看看 你的Linux终端 里面有没有报错信息的提示?
012018-06-24
相似问题



