Skip to content

Commit f5909dc

Browse files
committed
监听websocket✅
1 parent 5402b32 commit f5909dc

File tree

7 files changed

+270
-12
lines changed

7 files changed

+270
-12
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
# swoole websocket and tcp and udp
1+
# swoole websocket and tcp and udp
2+
3+
## 任务
4+
- [x] 监听websocket
5+
- [ ] 监听tcp
6+
- [ ] 监听udp

composer.lock

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
require_once './vendor/autoload.php';
44

55
$config = [
6-
'websocket' => [
6+
'timezone' => 'Asia/Shanghai',
7+
'tick_interval_timer' => 30, //秒
8+
'websocket' => [
79
'enable' => true,
810
'host' => '0.0.0.0',
911
'port' => '9000',
@@ -15,7 +17,7 @@
1517
],
1618
'handler' => swoole_websocket_and_tcp_and_udp_test\webscoket::class,
1719
],
18-
'http' => [
20+
'http' => [
1921
'enable' => true,
2022
'host' => '0.0.0.0',
2123
'port' => '9001',
@@ -24,7 +26,7 @@
2426
'open_websocket_protocol' => true,
2527
],
2628
],
27-
'tcp' => [
29+
'tcp' => [
2830
'enable' => true,
2931
'host' => '0.0.0.0',
3032
'port' => '9002',
@@ -37,7 +39,7 @@
3739
'heartbeat_idle_time' => 60,
3840
],
3941
],
40-
'udp' => [
42+
'udp' => [
4143
'enable' => true,
4244
'host' => '0.0.0.0',
4345
'port' => '9003',
@@ -52,4 +54,4 @@
5254

5355
$server = new \swoole_websocket_and_tcp_and_udp\Server($config);
5456

55-
$server;
57+
$server->run();

src/Server.php

Lines changed: 124 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
namespace swoole_websocket_and_tcp_and_udp;
44

55

6+
use swoole_websocket_and_tcp_and_udp\common\Logger;
7+
use swoole_websocket_and_tcp_and_udp\common\ProcessTrait;
8+
69
class Server
710
{
11+
use ProcessTrait, Logger;
12+
813
protected $port;
914

1015
protected $config;
1116

17+
/**
18+
* @var \swoole_websocket_server | \swoole_http_server
19+
*/
1220
protected $server;
1321

1422
protected $primaryConfig = [];
@@ -18,14 +26,16 @@ class Server
1826
public function __construct($config)
1927
{
2028
$this->config = $config;
29+
ini_set('date.timezone', $this->config['timezone']);
30+
2131
if (isset($this->config['websocket'])) {
2232
$this->enableWebsocket = true;
2333
$this->primaryConfig = $this->config['websocket'];
24-
$server = swoole_websocket_server::class;
34+
$serverClass = \swoole_websocket_server::class;
2535
} else {
2636
if (isset($this->config['http'])) {
2737
$this->primaryConfig = $this->config['http'];
28-
$server = swoole_http_server::class;
38+
$serverClass = \swoole_http_server::class;
2939
}
3040
}
3141

@@ -34,13 +44,15 @@ public function __construct($config)
3444
$type = $this->primaryConfig['type'];
3545
$setting = $this->primaryConfig['setting'];
3646

37-
$this->server = new $server($host, $port, SWOOLE_PROCESS, $type);
47+
Logger::info("开始监听端口 {$host}:{$port}");
48+
$this->server = new $serverClass($host, $port, SWOOLE_PROCESS, $type);
3849
$this->server->set($setting);
3950

4051

4152
$this->bindBaseEvent();
4253
$this->bindHttpEvent();
4354
$this->bindTaskEvent();
55+
$this->bindWebsocketEvent();
4456
}
4557

4658
/**
@@ -55,6 +67,7 @@ protected function bindBaseEvent()
5567
$this->server->on('ManagerStop', [$this, 'ManagerStop']);
5668
$this->server->on('WorkerStart', [$this, 'WorkerStart']);
5769
$this->server->on('WorkerStop', [$this, 'WorkerStop']);
70+
$this->server->on('WorkerExit', [$this, 'WorkerExit']);
5871
$this->server->on('WorkerError', [$this, 'WorkerError']);
5972
$this->server->on('PipeMessage', [$this, 'PipeMessage']);
6073
}
@@ -75,9 +88,9 @@ protected function bindWebsocketEvent()
7588
if ($this->enableWebsocket) {
7689
$handlerClass = $this->primaryConfig['handler'];
7790
$handler = new $handlerClass();
78-
if (!($handler instanceof protocol\WebSocketHandlerInterface)) {
79-
throw new \Exception(sprintf('%s 当前类不属于 interface %s',
80-
$handlerClass, protocol\WebSocketHandlerInterface::class));
91+
if (!($handler instanceof protocol\WebsocketEvent)) {
92+
throw new \Exception(sprintf('%s 当前类不属于 %s',
93+
$handlerClass, protocol\WebsocketEvent::class));
8194
}
8295

8396
$eventHandler = function ($method, array $params) use ($handler) {
@@ -102,5 +115,110 @@ protected function bindWebsocketEvent()
102115
}
103116
}
104117

118+
public function start(\swoole_http_server $server)
119+
{
120+
foreach (spl_autoload_functions() as $function) {
121+
spl_autoload_unregister($function);
122+
}
123+
124+
$this->setProcessName('master process');
125+
126+
if (version_compare(swoole_version(), '1.10.4', '<')) {
127+
file_put_contents($this->config['pid_file'], $server->master_pid);
128+
}
129+
}
130+
131+
public function shutdown(\swoole_http_server $server)
132+
{
133+
134+
}
135+
136+
public function ManagerStart(\swoole_http_server $server)
137+
{
138+
139+
}
140+
141+
public function ManagerStop(\swoole_http_server $server)
142+
{
143+
144+
}
145+
146+
public function WorkerStart(\swoole_http_server $server, $worker_id)
147+
{
148+
if ($worker_id >= (swoole_cpu_num() * 2)) {
149+
$process = 'task worker';
150+
} else {
151+
$process = 'worker';
152+
}
153+
154+
$this->setProcessName(sprintf('%s process %d', $process,
155+
$worker_id));
156+
157+
if (!$server->taskworker) {
158+
$server->task(1);
159+
}
160+
}
161+
162+
public function WorkerStop(\swoole_http_server $server, $worker_id)
163+
{
164+
165+
}
166+
167+
public function WorkerExit(\swoole_http_server $server, $worker_id)
168+
{
169+
170+
}
171+
172+
public function WorkerError(
173+
\swoole_http_server $server,
174+
$worker_id,
175+
$worker_pid,
176+
$exit_code,
177+
$signal
178+
) {
179+
Logger::err(sprintf('worker[%d] error: exitCode=%s, signal=%s',
180+
$worker_id,
181+
$exit_code, $signal));
182+
}
183+
184+
public function PipeMessage(
185+
\swoole_http_server $server,
186+
$src_worker_id,
187+
$message
188+
) {
189+
190+
}
191+
192+
public function task(
193+
\swoole_http_server $server,
194+
$task_id,
195+
$src_worker_id,
196+
$data
197+
) {
198+
if ($src_worker_id == 1) {
199+
swoole_timer_tick($this->config['tick_interval_timer'] * 1000,
200+
function ($timer_id) {
201+
Logger::debug('此处可定时清理任务');
202+
});
203+
}
204+
}
205+
206+
public function finish(\swoole_http_server $server, $task_id, $data)
207+
{
208+
209+
}
210+
211+
public function request(
212+
\swoole_http_request $request,
213+
\swoole_http_response $response
214+
) {
215+
216+
}
217+
218+
public function run()
219+
{
220+
Logger::info('运行服务...');
221+
$this->server->start();
222+
}
105223

106224
}

src/common/Logger.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace swoole_websocket_and_tcp_and_udp\common;
4+
5+
6+
trait Logger
7+
{
8+
static function info($msg)
9+
{
10+
$time = date('Y-m-d H:i:s');
11+
echo '[' . $time . '] INFO ' . $msg . PHP_EOL;
12+
}
13+
14+
static function debug($msg)
15+
{
16+
$time = date('Y-m-d H:i:s');
17+
echo '[' . $time . '] DEBUG ' . $msg . PHP_EOL;
18+
}
19+
20+
static function warn($msg)
21+
{
22+
$time = date('Y-m-d H:i:s');
23+
echo '[' . $time . '] WARN ' . $msg . PHP_EOL;
24+
}
25+
26+
static function err($msg)
27+
{
28+
$time = date('Y-m-d H:i:s');
29+
echo '[' . $time . '] ERROR ' . $msg . PHP_EOL;
30+
}
31+
}

src/common/ProcessTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace swoole_websocket_and_tcp_and_udp\common;
4+
5+
6+
trait ProcessTrait
7+
{
8+
public function setProcessName($name)
9+
{
10+
if (PHP_OS == 'Darwin') {
11+
return;
12+
}
13+
14+
if (function_exists('\swoole_set_process_name')) {
15+
\swoole_set_process_name($name);
16+
}
17+
}
18+
}

test/webscoket.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,46 @@
33
namespace swoole_websocket_and_tcp_and_udp_test;
44

55

6+
use swoole_websocket_and_tcp_and_udp\common\Logger;
67
use swoole_websocket_and_tcp_and_udp\protocol\WebsocketEvent;
78

89
class webscoket extends WebsocketEvent
910
{
11+
use Logger;
1012

13+
public function __construct()
14+
{
15+
}
16+
17+
public function open(
18+
\swoole_websocket_server $server,
19+
\swoole_http_request $request
20+
) {
21+
$fd = $request->fd;
22+
$info = $server->connection_info($fd);
23+
Logger::info("{$info['remote_ip']}:{$info['remote_port']}, Connect");
24+
}
25+
26+
public function message(
27+
\swoole_websocket_server $server,
28+
\swoole_websocket_frame $frame
29+
) {
30+
$fd = $frame->fd;
31+
$data = $frame->data;
32+
$info = $server->connection_info($fd);
33+
34+
if ($data == 'exit') {
35+
$server->push($fd, '再见👋');
36+
$server->close($fd);
37+
}
38+
$server->push($fd, '你好呀');
39+
40+
Logger::info("{$info['remote_ip']}:{$info['remote_port']}, Message [fd{$fd}]: {$data}");
41+
}
42+
43+
public function close(\swoole_websocket_server $server, $fd)
44+
{
45+
$info = $server->connection_info($fd);
46+
Logger::info("{$info['remote_ip']}:{$info['remote_port']}, Close");
47+
}
1148
}

0 commit comments

Comments
 (0)