Skip to content

Commit a271ce4

Browse files
committed
监听http✅
1 parent f5909dc commit a271ce4

File tree

9 files changed

+152
-36
lines changed

9 files changed

+152
-36
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# swoole websocket and tcp and udp
22

3+
## 介绍
4+
5+
6+
##
7+
38
## 任务
49
- [x] 监听websocket
10+
- [x] 监听http
511
- [ ] 监听tcp
612
- [ ] 监听udp

example.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@
1515
'open_websocket_protocol' => true,
1616
'task_worker_num' => 1,
1717
],
18-
'handler' => swoole_websocket_and_tcp_and_udp_test\webscoket::class,
18+
'handler' => \swoole_websocket_and_tcp_and_udp_test\webscoket::class,
1919
],
2020
'http' => [
2121
'enable' => true,
2222
'host' => '0.0.0.0',
2323
'port' => '9001',
2424
'type' => SWOOLE_SOCK_TCP,
2525
'setting' => [
26-
'open_websocket_protocol' => true,
26+
'open_http_protocol' => true,
27+
'daemonize' => false,
2728
],
29+
'handler' => \swoole_websocket_and_tcp_and_udp_test\http::class,
2830
],
2931
'tcp' => [
3032
'enable' => true,

src/Server.php

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
use swoole_websocket_and_tcp_and_udp\common\Logger;
77
use swoole_websocket_and_tcp_and_udp\common\ProcessTrait;
8+
use swoole_websocket_and_tcp_and_udp\handler\HttpHandler;
9+
use swoole_websocket_and_tcp_and_udp\handler\WebsocketHandler;
810

911
class Server
1012
{
@@ -23,6 +25,8 @@ class Server
2325

2426
protected $enableWebsocket = false;
2527

28+
protected $enableTask = false;
29+
2630
public function __construct($config)
2731
{
2832
$this->config = $config;
@@ -44,15 +48,19 @@ public function __construct($config)
4448
$type = $this->primaryConfig['type'];
4549
$setting = $this->primaryConfig['setting'];
4650

51+
if (isset($setting['task_worker_num'])) {
52+
$this->enableTask = true;
53+
}
54+
4755
Logger::info("开始监听端口 {$host}:{$port}");
4856
$this->server = new $serverClass($host, $port, SWOOLE_PROCESS, $type);
4957
$this->server->set($setting);
5058

5159

5260
$this->bindBaseEvent();
53-
$this->bindHttpEvent();
5461
$this->bindTaskEvent();
55-
$this->bindWebsocketEvent();
62+
$this->bindMasterEvent();
63+
$this->bindOtherEvent();
5664
}
5765

5866
/**
@@ -72,34 +80,19 @@ protected function bindBaseEvent()
7280
$this->server->on('PipeMessage', [$this, 'PipeMessage']);
7381
}
7482

75-
protected function bindHttpEvent()
76-
{
77-
$this->server->on('Request', [$this, 'request']);
78-
}
79-
8083
protected function bindTaskEvent()
8184
{
8285
$this->server->on('Task', [$this, 'task']);
8386
$this->server->on('Finish', [$this, 'finish']);
8487
}
8588

86-
protected function bindWebsocketEvent()
89+
protected function bindMasterEvent()
8790
{
88-
if ($this->enableWebsocket) {
89-
$handlerClass = $this->primaryConfig['handler'];
90-
$handler = new $handlerClass();
91-
if (!($handler instanceof protocol\WebsocketEvent)) {
92-
throw new \Exception(sprintf('%s 当前类不属于 %s',
93-
$handlerClass, protocol\WebsocketEvent::class));
94-
}
91+
$handlerClass = $this->primaryConfig['handler'];
9592

96-
$eventHandler = function ($method, array $params) use ($handler) {
97-
try {
98-
call_user_func_array([$handler, $method], $params);
99-
} catch (\Exception $e) {
100-
exit($e);
101-
}
102-
};
93+
if ($this->enableWebsocket) {
94+
$websocketHandler = new WebsocketHandler($handlerClass);
95+
$eventHandler = $websocketHandler->make();
10396

10497
$this->server->on('Open', function () use ($eventHandler) {
10598
$eventHandler('open', func_get_args());
@@ -112,9 +105,21 @@ protected function bindWebsocketEvent()
112105
$this->server->on('Close', function () use ($eventHandler) {
113106
$eventHandler('close', func_get_args());
114107
});
108+
} else {
109+
$httpHandler = new HttpHandler($handlerClass);
110+
$eventHandler = $httpHandler->make();
111+
112+
$this->server->on('request', function () use ($eventHandler) {
113+
$eventHandler('request', func_get_args());
114+
});
115115
}
116116
}
117117

118+
protected function bindOtherEvent()
119+
{
120+
121+
}
122+
118123
public function start(\swoole_http_server $server)
119124
{
120125
foreach (spl_autoload_functions() as $function) {
@@ -154,8 +159,10 @@ public function WorkerStart(\swoole_http_server $server, $worker_id)
154159
$this->setProcessName(sprintf('%s process %d', $process,
155160
$worker_id));
156161

157-
if (!$server->taskworker) {
158-
$server->task(1);
162+
if ($this->enableTask) {
163+
if (!$server->taskworker) {
164+
$server->task(1);
165+
}
159166
}
160167
}
161168

@@ -208,13 +215,6 @@ public function finish(\swoole_http_server $server, $task_id, $data)
208215

209216
}
210217

211-
public function request(
212-
\swoole_http_request $request,
213-
\swoole_http_response $response
214-
) {
215-
216-
}
217-
218218
public function run()
219219
{
220220
Logger::info('运行服务...');

src/handler/Handler.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace swoole_websocket_and_tcp_and_udp\handler;
4+
5+
6+
use swoole_websocket_and_tcp_and_udp\protocol\HttpEvent;
7+
use swoole_websocket_and_tcp_and_udp\protocol\WebsocketEvent;
8+
9+
abstract class Handler
10+
{
11+
protected function make()
12+
{
13+
}
14+
15+
/**
16+
* @param WebsocketEvent | HttpEvent $handler
17+
*
18+
* @return \Closure
19+
*/
20+
protected function eventCallbak($handler)
21+
{
22+
return function ($method, $params) use ($handler) {
23+
try {
24+
call_user_func_array([$handler, $method], $params);
25+
} catch (\Exception $e) {
26+
exit($e);
27+
}
28+
};
29+
}
30+
}

src/handler/HttpHandler.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace swoole_websocket_and_tcp_and_udp\handler;
4+
5+
6+
use swoole_websocket_and_tcp_and_udp\protocol\HttpEvent;
7+
8+
class HttpHandler extends Handler
9+
{
10+
public function __construct($handlerClass)
11+
{
12+
$this->handlerClass = $handlerClass;
13+
}
14+
15+
public function make()
16+
{
17+
$handler = new $this->handlerClass();
18+
19+
if (!($handler instanceof HttpEvent)) {
20+
throw new \Exception(sprintf('%s 当前类不属于 %s',
21+
$this->handlerClass, HttpEvent::class));
22+
}
23+
24+
return $this->eventCallbak($handler);
25+
}
26+
}

src/handler/WebsocketHandler.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace swoole_websocket_and_tcp_and_udp\handler;
4+
5+
6+
use swoole_websocket_and_tcp_and_udp\protocol\WebsocketEvent;
7+
8+
class WebsocketHandler extends Handler
9+
{
10+
public function __construct($handlerClass)
11+
{
12+
$this->handlerClass = $handlerClass;
13+
}
14+
15+
public function make()
16+
{
17+
$handler = new $this->handlerClass();
18+
19+
if (!($handler instanceof WebsocketEvent)) {
20+
throw new \Exception(sprintf('%s 当前类不属于 %s',
21+
$this->handlerClass, WebsocketEvent::class));
22+
}
23+
24+
return $this->eventCallbak($handler);
25+
}
26+
27+
}

src/protocol/HttpEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(\swoole_server_port $port)
2525
}
2626

2727
public function request(
28-
\swoole_http_server $server,
28+
\swoole_http_request $request,
2929
\swoole_http_response $response
3030
) {
3131
// TODO: Implement request() method.

src/protocol/HttpInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
interface HttpInterface
1111
{
1212
/**
13-
* @param \swoole_http_server $server
13+
* @param \swoole_http_request $request
1414
* @param \swoole_http_response $response
1515
*
1616
* @return mixed
1717
*/
1818
function request(
19-
\swoole_http_server $server,
19+
\swoole_http_request $request,
2020
\swoole_http_response $response
2121
);
2222
}

test/http.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace swoole_websocket_and_tcp_and_udp_test;
4+
5+
6+
use swoole_websocket_and_tcp_and_udp\common\Logger;
7+
use swoole_websocket_and_tcp_and_udp\protocol\HttpEvent;
8+
9+
class http extends HttpEvent
10+
{
11+
use Logger;
12+
13+
public function __construct()
14+
{
15+
}
16+
17+
public function request(
18+
\swoole_http_request $request,
19+
\swoole_http_response $response
20+
) {
21+
$response->end('<h1>Dva爱你哟❤️</h1>');
22+
23+
Logger::info("{$request->server['remote_addr']}:{$request->server['remote_port']}, Request {$request->server['request_uri']} {$request->server['request_method']} {$request->server['server_protocol']} {$request->header['user-agent']}");
24+
}
25+
}

0 commit comments

Comments
 (0)