2019-12-12 16:24:04 +08:00
|
|
|
|
# WebSocket 服務
|
2019-06-29 16:50:15 +08:00
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
Hyperf 提供了對 WebSocket Server 的封裝,可基於 [hyperf/websocket-server](https://github.com/hyperf/websocket-server) 元件快速搭建一個 WebSocket 應用。
|
2019-06-29 16:50:15 +08:00
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
## 安裝
|
2019-07-02 01:58:34 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
2019-06-29 16:50:15 +08:00
|
|
|
|
composer require hyperf/websocket-server
|
|
|
|
|
```
|
|
|
|
|
|
2019-07-02 01:58:34 +08:00
|
|
|
|
## 配置 Server
|
2019-06-29 16:50:15 +08:00
|
|
|
|
|
|
|
|
|
修改 `config/autoload/server.php`,增加以下配置。
|
|
|
|
|
|
2019-07-02 01:58:34 +08:00
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
2021-05-10 07:23:33 +08:00
|
|
|
|
return [
|
|
|
|
|
'servers' => [
|
|
|
|
|
[
|
|
|
|
|
'name' => 'ws',
|
|
|
|
|
'type' => Server::SERVER_WEBSOCKET,
|
|
|
|
|
'host' => '0.0.0.0',
|
|
|
|
|
'port' => 9502,
|
|
|
|
|
'sock_type' => SWOOLE_SOCK_TCP,
|
|
|
|
|
'callbacks' => [
|
|
|
|
|
Event::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
|
|
|
|
|
Event::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
|
|
|
|
|
Event::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
|
|
|
|
|
],
|
2019-06-29 16:50:15 +08:00
|
|
|
|
],
|
|
|
|
|
],
|
2021-05-10 07:23:33 +08:00
|
|
|
|
];
|
2019-06-29 16:50:15 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-07-02 01:58:34 +08:00
|
|
|
|
## 配置路由
|
2019-06-29 16:50:15 +08:00
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
> 目前暫時只支援配置檔案的模式配置路由,後續會提供註解模式。
|
2019-07-02 01:58:34 +08:00
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
在 `config/routes.php` 檔案內增加對應 `ws` 的 Server 的路由配置,這裡的 `ws` 值取決於您在 `config/autoload/server.php` 內配置的 WebSocket Server 的 `name` 值。
|
2019-07-02 01:58:34 +08:00
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
2019-06-29 16:50:15 +08:00
|
|
|
|
|
|
|
|
|
Router::addServer('ws', function () {
|
|
|
|
|
Router::get('/', 'App\Controller\WebSocketController');
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 16:24:04 +08:00
|
|
|
|
## 建立對應控制器
|
2019-06-29 16:50:15 +08:00
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
|
|
use Hyperf\Contract\OnCloseInterface;
|
|
|
|
|
use Hyperf\Contract\OnMessageInterface;
|
|
|
|
|
use Hyperf\Contract\OnOpenInterface;
|
|
|
|
|
use Swoole\Http\Request;
|
|
|
|
|
use Swoole\Server;
|
|
|
|
|
use Swoole\Websocket\Frame;
|
2019-10-08 12:43:48 +08:00
|
|
|
|
use Swoole\WebSocket\Server as WebSocketServer;
|
2019-06-29 16:50:15 +08:00
|
|
|
|
|
|
|
|
|
class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
|
|
|
|
|
{
|
2020-07-09 16:53:21 +08:00
|
|
|
|
public function onMessage($server, Frame $frame): void
|
2019-06-29 16:50:15 +08:00
|
|
|
|
{
|
2019-07-02 01:58:34 +08:00
|
|
|
|
$server->push($frame->fd, 'Recv: ' . $frame->data);
|
2019-06-29 16:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 16:53:21 +08:00
|
|
|
|
public function onClose($server, int $fd, int $reactorId): void
|
2019-06-29 16:50:15 +08:00
|
|
|
|
{
|
|
|
|
|
var_dump('closed');
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 16:53:21 +08:00
|
|
|
|
public function onOpen($server, Request $request): void
|
2019-06-29 16:50:15 +08:00
|
|
|
|
{
|
2019-07-02 01:58:34 +08:00
|
|
|
|
$server->push($request->fd, 'Opened');
|
2019-06-29 16:50:15 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-08-10 13:28:28 +08:00
|
|
|
|
接下來啟動 Server,便能看到對應啟動了一個 WebSocket Server 並監聽於 9502 埠,此時您便可以通過各種 WebSocket Client 來進行連線和資料傳輸了。
|
2019-06-29 16:50:15 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ php bin/hyperf.php start
|
|
|
|
|
|
|
|
|
|
[INFO] Worker#0 started.
|
|
|
|
|
[INFO] WebSocket Server listening at 0.0.0.0:9502
|
|
|
|
|
[INFO] HTTP Server listening at 0.0.0.0:9501
|
2019-07-02 01:58:34 +08:00
|
|
|
|
```
|
2020-04-30 17:09:23 +08:00
|
|
|
|
|
2020-08-10 13:28:28 +08:00
|
|
|
|
!> 當我們同時監聽了 HTTP Server 的 9501 埠和 WebSocket Server 的 9502 埠時, WebSocket Client 可以通過 9501 和 9502 兩個埠連線 WebSocket Server,即連線 `ws://0.0.0.0:9501` 和 `ws://0.0.0.0:9502` 都可以成功。
|
|
|
|
|
|
|
|
|
|
因為 Swoole\WebSocket\Server 繼承自 Swoole\Http\Server,可以使用 HTTP 觸發所有 WebSocket 的推送,瞭解詳情可檢視 [Swoole 文件](https://wiki.swoole.com/#/websocket_server?id=websocketserver) onRequest 回撥部分。
|
|
|
|
|
|
|
|
|
|
如需關閉,可以修改 `config/autoload/server.php` 檔案給 `http` 服務中增加 `open_websocket_protocol` 配置項。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
return [
|
|
|
|
|
// 這裡省略了該檔案的其它配置
|
|
|
|
|
'servers' => [
|
|
|
|
|
[
|
|
|
|
|
'name' => 'http',
|
|
|
|
|
'type' => Server::SERVER_HTTP,
|
|
|
|
|
'host' => '0.0.0.0',
|
|
|
|
|
'port' => 9501,
|
|
|
|
|
'sock_type' => SWOOLE_SOCK_TCP,
|
|
|
|
|
'callbacks' => [
|
2021-02-19 12:53:50 +08:00
|
|
|
|
Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
|
2020-08-10 13:28:28 +08:00
|
|
|
|
],
|
|
|
|
|
'settings' => [
|
|
|
|
|
'open_websocket_protocol' => false,
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
```
|
|
|
|
|
|
2020-04-30 17:09:23 +08:00
|
|
|
|
## 連線上下文
|
|
|
|
|
|
|
|
|
|
WebSocket 服務的 onOpen, onMessage, onClose 回撥並不在同一個協程下觸發,因此不能直接使用協程上下文儲存狀態資訊。WebSocket Server 元件提供了 **連線級** 的上下文,API 與協程上下文完全一樣。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
|
|
use Hyperf\Contract\OnMessageInterface;
|
|
|
|
|
use Hyperf\Contract\OnOpenInterface;
|
|
|
|
|
use Hyperf\WebSocketServer\Context;
|
|
|
|
|
use Swoole\Http\Request;
|
|
|
|
|
use Swoole\Websocket\Frame;
|
|
|
|
|
use Swoole\WebSocket\Server as WebSocketServer;
|
|
|
|
|
|
|
|
|
|
class WebSocketController implements OnMessageInterface, OnOpenInterface
|
|
|
|
|
{
|
2020-07-09 16:53:21 +08:00
|
|
|
|
public function onMessage($server, Frame $frame): void
|
2020-04-30 17:09:23 +08:00
|
|
|
|
{
|
|
|
|
|
$server->push($frame->fd, 'Username: ' . Context::get('username'));
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 16:53:21 +08:00
|
|
|
|
public function onOpen($server, Request $request): void
|
2020-04-30 17:09:23 +08:00
|
|
|
|
{
|
|
|
|
|
Context::set('username', $request->cookie['username']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2020-06-16 01:43:41 +08:00
|
|
|
|
|
|
|
|
|
## 多 server 配置
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
# /etc/nginx/conf.d/ng_socketio.conf
|
|
|
|
|
# 多個 ws server
|
|
|
|
|
upstream io_nodes {
|
|
|
|
|
server ws1:9502;
|
|
|
|
|
server ws2:9502;
|
|
|
|
|
}
|
|
|
|
|
server {
|
|
|
|
|
listen 9502;
|
|
|
|
|
# server_name your.socket.io;
|
|
|
|
|
location / {
|
|
|
|
|
proxy_set_header Upgrade "websocket";
|
|
|
|
|
proxy_set_header Connection "upgrade";
|
|
|
|
|
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
|
# proxy_set_header Host $host;
|
|
|
|
|
# proxy_http_version 1.1;
|
|
|
|
|
# 轉發到多個 ws server
|
|
|
|
|
proxy_pass http://io_nodes;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2020-07-27 15:52:51 +08:00
|
|
|
|
|
|
|
|
|
## 訊息傳送器
|
|
|
|
|
|
|
|
|
|
當我們想在 `HTTP` 服務中,關閉 `WebSocket` 連線時,可以直接使用 `Hyperf\WebSocketServer\Sender`。
|
|
|
|
|
|
|
|
|
|
`Sender` 會判斷 `fd` 是否被當前 `Worker` 所持有,如果是,則會直接傳送資料,如果不是,則會通過 `PipeMessage` 傳送給除自己外的所有 `Worker`,然後由其他 `Worker` 進行判斷,
|
|
|
|
|
如果是自己持有的 `fd`,就會發送對應資料到客戶端。
|
|
|
|
|
|
|
|
|
|
`Sender` 支援 `push` 和 `disconnect` 兩個 `API`,如下:
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
|
use Hyperf\HttpServer\Annotation\AutoController;
|
|
|
|
|
use Hyperf\WebSocketServer\Sender;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @AutoController
|
|
|
|
|
*/
|
|
|
|
|
class ServerController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @Inject
|
|
|
|
|
* @var Sender
|
|
|
|
|
*/
|
|
|
|
|
protected $sender;
|
|
|
|
|
|
|
|
|
|
public function close(int $fd)
|
|
|
|
|
{
|
|
|
|
|
go(function () use ($fd) {
|
|
|
|
|
sleep(1);
|
|
|
|
|
$this->sender->disconnect($fd);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function send(int $fd)
|
|
|
|
|
{
|
|
|
|
|
$this->sender->push($fd, 'Hello World.');
|
|
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|
2021-05-31 09:44:47 +08:00
|
|
|
|
|
|
|
|
|
## 在 WebSocket 服務中處理 HTTP 請求
|
|
|
|
|
|
|
|
|
|
我們除了可以將 HTTP 服務和 WebSocket 服務通過埠分開,也可以在 WebSocket 中監聽 HTTP 請求。
|
|
|
|
|
|
|
|
|
|
因為 `server.servers.*.callbacks` 中的配置項,都是單例的,所以我們需要在 `dependencies` 中配置一個單獨的例項。
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
return [
|
|
|
|
|
'HttpServer' => Hyperf\HttpServer\Server::class,
|
|
|
|
|
];
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
然後修改我們的 `WebSocket` 服務中的 `callbacks` 配置,以下隱藏了不相干的配置
|
|
|
|
|
|
|
|
|
|
```php
|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
use Hyperf\Server\Event;
|
|
|
|
|
use Hyperf\Server\Server;
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'mode' => SWOOLE_BASE,
|
|
|
|
|
'servers' => [
|
|
|
|
|
[
|
|
|
|
|
'name' => 'ws',
|
|
|
|
|
'type' => Server::SERVER_WEBSOCKET,
|
|
|
|
|
'host' => '0.0.0.0',
|
|
|
|
|
'port' => 9502,
|
|
|
|
|
'sock_type' => SWOOLE_SOCK_TCP,
|
|
|
|
|
'callbacks' => [
|
|
|
|
|
Event::ON_REQUEST => ['HttpServer', 'onRequest'],
|
|
|
|
|
Event::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
|
|
|
|
|
Event::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
|
|
|
|
|
Event::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
最後我們便可以在 `ws` 中,新增 `HTTP` 路由了。
|