-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpapi.php
109 lines (81 loc) · 3.47 KB
/
httpapi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
require_once 'autoload.php';
use Swoole\Http\Server;
Co::set(['hook_flags'=> SWOOLE_HOOK_TCP | SWOOLE_HOOK_SLEEP | SWOOLE_HOOK_CURL]);
$Lite->config->load(['httpapi']);
$httpApi = new LiHttpApi($Lite->config->get('httpapi'));
$httpApi->start();
class LiHttpApi {
public $server, $Lite;
public $commTable;
//ipv6 的 $host = '::', 协义使用:SWOOLE_SOCK_TCP6,Linux系统下监听IPv6端口后使用IPv4地址也可以进行连接
protected $host = '0.0.0.0';
protected $port = 9898;
protected $taskName = 'LiHttpApi';
protected $options = [
'log_file' => __DIR__.'/log/http.log',
'pid_file' => __DIR__.'/http.pid',
];
public function __construct($param = []) {
$this->port = isset($param['port']) ? $param['port'] : 9898 ;
if(isset($param['SSL']) && $param['SSL']==true){
$this->server = new Swoole\Http\Server($this->host, $this->port, SWOOLE_BASE, SWOOLE_SOCK_TCP | SWOOLE_SSL );
}else{
$this->server = new Swoole\Http\Server($this->host, $this->port, SWOOLE_BASE, SWOOLE_SOCK_TCP );
}
if (!empty($param)) {
$this->taskName = $param['taskName'];
$options = $param['options'];
$this->options = array_merge($this->options, $options);
}
$this->server -> set($this->options);
LiteApi\LiApiCommVar::$commTable = new Swoole\Table(1024);
LiteApi\LiApiCommVar::$commTable->column('num', Swoole\Table::TYPE_INT);
LiteApi\LiApiCommVar::$commTable->create();
$this->server->on("request", [$this, 'onRequest']);
$this->server->on("Task", [$this, 'onTask']);
$this->server->on("Finish", [$this, 'onTaskFinish']);
$this->server->on('WorkerStart', function ($serv, $worker_id){
$this->Lite = new LiteApi\LiteApi();
$this->Lite->config->load(['httpapi']);
$hApi = new LiteApi\HttpApi($this->Lite);
$hApi->onWorkerStart($serv, $worker_id);
unset($hApi);
});
$this->server->on('Start', function($serv){
swoole_set_process_name($this->taskName);
});
$this->server->on('shutdown',function ($serv) {
$hApi = new LiteApi\HttpApi($this->Lite);
$hApi->onShutdown($serv);
unset($hApi);
});
}
public function __destruct() {
}
public function onRequest($request, $response) {
if ($request->server['path_info'] == '/favicon.ico' || $request->server['request_uri'] == '/favicon.ico') {
$response->end();
return;
}
$hApi = new LiteApi\HttpApi($this->Lite);
$hApi->onRequest($request, $response);
unset($hApi);
}
public function onTask($serv, $task_id, $from_id, $data) {
$hApi = new LiteApi\HttpApi($this->Lite);
$fine = $hApi->onTask($serv, $task_id, $from_id, $data);
if(!empty($fine) && count($fine)){
$serv->finish($fine);
}
unset($hApi);
}
public function onTaskFinish($serv, $task_id, $data) {
$hApi = new LiteApi\HttpApi($this->Lite);
$hApi->onTaskFinish($serv, $task_id, $data);
unset($hApi);
}
public function start(){
$this->server->start();
}
}