Skip to content

Commit 176c147

Browse files
committed
websocket 支持
1 parent 4a7d063 commit 176c147

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1880
-141
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
os: [ ubuntu-latest, macos-latest, windows-latest ]
15+
os: [ ubuntu-latest, macos-latest ]
1616
php: [ "8.2", "8.3", "8.4" ]
1717

1818
name: PHP ${{ matrix.php }} - ${{ matrix.os }}

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,54 @@ return [
7373

7474
```
7575

76+
### websocket
77+
78+
> 使用路由调度的方式,可以让不同路径的websocket服务响应不同的事件
79+
80+
#### 配置
81+
82+
```
83+
swoole.websocket = true 时开启
84+
```
85+
86+
#### 路由定义
87+
```php
88+
Route::get('path1','controller/action1');
89+
Route::get('path2','controller/action2');
90+
```
91+
92+
#### 控制器
93+
94+
```php
95+
use \think\swoole\Websocket;
96+
use \think\swoole\websocket\Event;
97+
use \Swoole\WebSocket\Frame;
98+
99+
class Controller {
100+
101+
public function action1(){
102+
103+
return \think\swoole\helper\websocket()
104+
->onOpen(...)
105+
->onMessage(function(Websocket $websocket, Frame $frame){
106+
...
107+
})
108+
->onClose(...);
109+
}
110+
111+
public function action2(){
112+
113+
return \think\swoole\helper\websocket()
114+
->onOpen(...)
115+
->onMessage(function(Websocket $websocket, Frame $frame){
116+
...
117+
})
118+
->onClose(...);
119+
}
120+
}
121+
```
122+
123+
76124
## 自定义worker
77125
监听`worker.init`事件 注入`Manager`对象,调用addWorker方法添加
78126
~~~php

composer.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
],
1111
"require": {
1212
"php": ">=8.2",
13-
"workerman/workerman": "^4.2",
14-
"topthink/framework": "^8.0"
13+
"workerman/workerman": "^5.0.0-rc",
14+
"topthink/framework": "^8.0",
15+
"revolt/event-loop": "^1.0",
16+
"workerman/redis": "^2.0"
1517
},
1618
"autoload": {
1719
"psr-4": {
@@ -32,7 +34,8 @@
3234
"pestphp/pest": "^3.7",
3335
"guzzlehttp/guzzle": "^7.0",
3436
"topthink/think-queue": "^3.0",
35-
"phpstan/phpstan": "^2.0"
37+
"phpstan/phpstan": "^2.0",
38+
"ratchet/pawl": "^0.4.1"
3639
},
3740
"autoload-dev": {
3841
"psr-4": {

phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ parameters:
1212
- '#Function config_path not found.#'
1313
- '#Function public_path not found.#'
1414
- '#Function json not found.#'
15-
- '#While loop condition is always true#'
15+
- '#Function runtime_path not found.#'
1616
- '#Constant STUB_DIR not found.#'

src/Conduit.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace think\worker;
4+
5+
/**
6+
* @mixin \think\worker\conduit\Driver
7+
*/
8+
class Conduit extends \think\Manager
9+
{
10+
11+
protected $namespace = "\\think\\worker\\conduit\\driver\\";
12+
13+
protected function resolveConfig(string $name)
14+
{
15+
return $this->app->config->get("worker.conduit.{$name}", []);
16+
}
17+
18+
public function getDefaultDriver()
19+
{
20+
return $this->app->config->get('worker.conduit.type', 'socket');
21+
}
22+
}

src/Ipc.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace think\worker;
4+
5+
class Ipc
6+
{
7+
protected $workerId;
8+
9+
public function __construct(protected Manager $manager, protected Conduit $conduit)
10+
{
11+
12+
}
13+
14+
public function listenMessage()
15+
{
16+
$this->subscribe();
17+
return $this->workerId;
18+
}
19+
20+
public function sendMessage($workerId, $message)
21+
{
22+
if ($workerId === $this->workerId) {
23+
$this->manager->triggerEvent('message', $message);
24+
} else {
25+
$this->publish($workerId, $message);
26+
}
27+
}
28+
29+
public function subscribe()
30+
{
31+
$this->workerId = $this->conduit->inc('ipc:worker');
32+
$this->conduit->subscribe("ipc:message:{$this->workerId}", function ($message) {
33+
$this->manager->triggerEvent('message', unserialize($message));
34+
});
35+
}
36+
37+
public function publish($workerId, $message)
38+
{
39+
$this->conduit->publish("ipc:message:{$workerId}", serialize($message));
40+
}
41+
}

src/Manager.php

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace think\worker;
44

5+
use think\worker\concerns\InteractsWithConduit;
56
use think\worker\concerns\InteractsWithHttp;
67
use think\worker\concerns\InteractsWithQueue;
78
use think\worker\concerns\InteractsWithServer;
@@ -13,12 +14,14 @@ class Manager
1314
use InteractsWithServer,
1415
InteractsWithHttp,
1516
InteractsWithQueue,
17+
InteractsWithConduit,
1618
WithApplication,
1719
WithContainer;
1820

1921
protected function initialize(): void
2022
{
2123
$this->prepareHttp();
2224
$this->prepareQueue();
25+
$this->prepareConduit();
2326
}
2427
}

0 commit comments

Comments
 (0)