swoole安装直接在宝塔后台-》安装扩展
phpStorm服务端代码同步设置
https://www.cnblogs.com/yszr/p/9009961.html 软件下载地址
代码上传下载,鼠标右击任何一个文件或者文件夹,选择
Development->上传/下载/同步
点击工具栏Tools->Development->Automatic Upload
<?php
$http = new Swoole\Http\Server('0.0.0.0', 9501);
$http->on('Request', function ($request, $response) {
echo "接收到了请求", PHP_EOL;
$response->header('Content-Type', 'text/html; charset=utf-8');
$response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
});
echo "服务启动", PHP_EOL;
$http->start();
http://192.168.1.4:9501/http.php
异步通信
<?php
$http = new Swoole\Http\Server('0.0.0.0', 9501);
$http->set([
'task_worker_num'=>4,
]);
$http->on('Request', function ($request, $response) use($http) {
echo "接收到了请求", PHP_EOL;
$response->header('Content-Type', 'text/html; charset=utf-8');
$http->task("发送邮件");
$http->task("发送广播");
$http->task("执行队列");
$response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
});
//处理异步任务(此回调函数在task进程中执行)
$http->on('Task', function ($serv, $task_id, $reactor_id, $data) {
echo "New AsyncTask[id={$task_id}]".PHP_EOL;
sleep(rand(1,5));
//返回任务执行的结果
$serv->finish("{$data} -> OK");
});
//处理异步任务的结果(此回调函数在worker进程中执行)
$http->on('Finish', function ($serv, $task_id, $data) {
echo "AsyncTask[{$task_id}] Finish: {$data}".PHP_EOL;
});
echo "服务启动", PHP_EOL;
$http->start();