Skip to content

Commit

Permalink
websocket 支持
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Dec 28, 2024
1 parent 4a7d063 commit 176c147
Show file tree
Hide file tree
Showing 44 changed files with 1,880 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest, macos-latest, windows-latest ]
os: [ ubuntu-latest, macos-latest ]
php: [ "8.2", "8.3", "8.4" ]

name: PHP ${{ matrix.php }} - ${{ matrix.os }}
Expand Down
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,54 @@ return [

```

### websocket

> 使用路由调度的方式,可以让不同路径的websocket服务响应不同的事件
#### 配置

```
swoole.websocket = true 时开启
```

#### 路由定义
```php
Route::get('path1','controller/action1');
Route::get('path2','controller/action2');
```

#### 控制器

```php
use \think\swoole\Websocket;
use \think\swoole\websocket\Event;
use \Swoole\WebSocket\Frame;

class Controller {

public function action1(){

return \think\swoole\helper\websocket()
->onOpen(...)
->onMessage(function(Websocket $websocket, Frame $frame){
...
})
->onClose(...);
}

public function action2(){

return \think\swoole\helper\websocket()
->onOpen(...)
->onMessage(function(Websocket $websocket, Frame $frame){
...
})
->onClose(...);
}
}
```


## 自定义worker
监听`worker.init`事件 注入`Manager`对象,调用addWorker方法添加
~~~php
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
],
"require": {
"php": ">=8.2",
"workerman/workerman": "^4.2",
"topthink/framework": "^8.0"
"workerman/workerman": "^5.0.0-rc",
"topthink/framework": "^8.0",
"revolt/event-loop": "^1.0",
"workerman/redis": "^2.0"
},
"autoload": {
"psr-4": {
Expand All @@ -32,7 +34,8 @@
"pestphp/pest": "^3.7",
"guzzlehttp/guzzle": "^7.0",
"topthink/think-queue": "^3.0",
"phpstan/phpstan": "^2.0"
"phpstan/phpstan": "^2.0",
"ratchet/pawl": "^0.4.1"
},
"autoload-dev": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ parameters:
- '#Function config_path not found.#'
- '#Function public_path not found.#'
- '#Function json not found.#'
- '#While loop condition is always true#'
- '#Function runtime_path not found.#'
- '#Constant STUB_DIR not found.#'
22 changes: 22 additions & 0 deletions src/Conduit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace think\worker;

/**
* @mixin \think\worker\conduit\Driver
*/
class Conduit extends \think\Manager
{

protected $namespace = "\\think\\worker\\conduit\\driver\\";

protected function resolveConfig(string $name)
{
return $this->app->config->get("worker.conduit.{$name}", []);
}

public function getDefaultDriver()
{
return $this->app->config->get('worker.conduit.type', 'socket');
}
}
41 changes: 41 additions & 0 deletions src/Ipc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace think\worker;

class Ipc
{
protected $workerId;

public function __construct(protected Manager $manager, protected Conduit $conduit)
{

}

public function listenMessage()
{
$this->subscribe();
return $this->workerId;
}

public function sendMessage($workerId, $message)
{
if ($workerId === $this->workerId) {
$this->manager->triggerEvent('message', $message);
} else {
$this->publish($workerId, $message);
}
}

public function subscribe()
{
$this->workerId = $this->conduit->inc('ipc:worker');
$this->conduit->subscribe("ipc:message:{$this->workerId}", function ($message) {
$this->manager->triggerEvent('message', unserialize($message));
});
}

public function publish($workerId, $message)
{
$this->conduit->publish("ipc:message:{$workerId}", serialize($message));
}
}
3 changes: 3 additions & 0 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace think\worker;

use think\worker\concerns\InteractsWithConduit;
use think\worker\concerns\InteractsWithHttp;
use think\worker\concerns\InteractsWithQueue;
use think\worker\concerns\InteractsWithServer;
Expand All @@ -13,12 +14,14 @@ class Manager
use InteractsWithServer,
InteractsWithHttp,
InteractsWithQueue,
InteractsWithConduit,
WithApplication,
WithContainer;

protected function initialize(): void
{
$this->prepareHttp();
$this->prepareQueue();
$this->prepareConduit();
}
}
Loading

0 comments on commit 176c147

Please sign in to comment.