|
35 | 35 |
|
36 | 36 | $http = new Server('0.0.0.0', 8080); |
37 | 37 | $http->set([ |
38 | | - 'worker_num' => swoole_cpu_num(), |
39 | | - 'enable_reuse_port' => true, |
40 | | - 'enable_coroutine' => false, |
41 | | - 'package_max_length' => 30 * 1024 * 1024, |
42 | | - 'http_compression_level' => 0 |
| 38 | + 'worker_num' => swoole_cpu_num(), |
| 39 | + 'enable_reuse_port' => true, |
| 40 | + 'enable_coroutine' => false, |
| 41 | + 'package_max_length' => 30 * 1024 * 1024 |
43 | 42 | ]); |
44 | 43 |
|
45 | 44 | $http->on('workerStart', function (Server $server, int $workerId) { |
|
67 | 66 |
|
68 | 67 | if (preg_match('#^/json/(\d+)$#', $path, $matches)) { |
69 | 68 | $count = min((int)$matches[1], count($dataset)); |
70 | | - $m = (int)($request->get['m'] ?? 1); |
| 69 | + $m = (int)($request->get['m'] ?? 1); |
71 | 70 | if ($m === 0) $m = 1; |
72 | 71 | $items = []; |
73 | 72 | for ($i = 0; $i < $count; $i++) { |
74 | | - $item = $dataset[$i]; |
| 73 | + $item = $dataset[$i]; |
75 | 74 | $item['total'] = $item['price'] * $item['quantity'] * $m; |
76 | | - $items[] = $item; |
| 75 | + $items[] = $item; |
77 | 76 | } |
78 | 77 | $response->header['Content-Type'] = 'application/json'; |
79 | 78 | $response->end(json_encode(['items' => $items, 'count' => $count], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); |
|
120 | 119 |
|
121 | 120 | $port = $http->listen('0.0.0.0', 8443, SWOOLE_TCP | SWOOLE_SSL); |
122 | 121 | $port->set([ |
123 | | - 'open_http2_protocol' => true, |
124 | | - 'ssl_cert_file' => '/certs/server.crt', |
125 | | - 'ssl_key_file' => '/certs/server.key', |
126 | | - 'package_max_length' => 30 * 1024 * 1024, |
127 | | - 'http_compression_level' => 0, |
| 122 | + 'open_http2_protocol' => true, |
| 123 | + 'ssl_cert_file' => '/certs/server.crt', |
| 124 | + 'ssl_key_file' => '/certs/server.key', |
| 125 | + 'package_max_length' => 30 * 1024 * 1024 |
128 | 126 | ]); |
129 | 127 |
|
| 128 | +$port2 = $http->listen('0.0.0.0', 8081, SWOOLE_TCP | SWOOLE_SSL); |
| 129 | +$port2->set([ |
| 130 | + 'ssl_cert_file' => '/certs/server.crt', |
| 131 | + 'ssl_key_file' => '/certs/server.key', |
| 132 | + 'package_max_length' => 30 * 1024 * 1024, |
| 133 | + 'http_compression' => false, |
| 134 | + 'open_http_protocol' => true, |
| 135 | +]); |
| 136 | +$port2->on('request', function (Request $request, Response $response) use ($dataset) { |
| 137 | + $path = $request->server['request_uri']; |
| 138 | + if (preg_match('#^/json/(\d+)$#', $path, $matches)) { |
| 139 | + $count = min((int)$matches[1], count($dataset)); |
| 140 | + $m = (int)($request->get['m'] ?? 1); |
| 141 | + if ($m === 0) $m = 1; |
| 142 | + $items = []; |
| 143 | + for ($i = 0; $i < $count; $i++) { |
| 144 | + $item = $dataset[$i]; |
| 145 | + $item['total'] = $item['price'] * $item['quantity'] * $m; |
| 146 | + $items[] = $item; |
| 147 | + } |
| 148 | + $response->header['Content-Type'] = 'application/json'; |
| 149 | + $response->end(json_encode(['items' => $items, 'count' => $count], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + $response->status(404); |
| 154 | + $response->header['Content-Type'] = 'text/plain'; |
| 155 | + $response->end('404 Not Found'); |
| 156 | +}); |
| 157 | + |
130 | 158 | $http->start(); |
0 commit comments