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
|
|
|
|
|
|
2019-06-29 16:50:15 +08:00
|
|
|
|
'servers' => [
|
|
|
|
|
[
|
|
|
|
|
'name' => 'ws',
|
|
|
|
|
'type' => Server::SERVER_WEBSOCKET,
|
|
|
|
|
'host' => '0.0.0.0',
|
|
|
|
|
'port' => 9502,
|
|
|
|
|
'sock_type' => SWOOLE_SOCK_TCP,
|
|
|
|
|
'callbacks' => [
|
|
|
|
|
SwooleEvent::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
|
|
|
|
|
SwooleEvent::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
|
|
|
|
|
SwooleEvent::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
```
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2019-10-08 12:43:48 +08:00
|
|
|
|
public function onMessage(WebSocketServer $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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function onClose(Server $server, int $fd, int $reactorId): void
|
|
|
|
|
{
|
|
|
|
|
var_dump('closed');
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 12:43:48 +08:00
|
|
|
|
public function onOpen(WebSocketServer $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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 16:24:04 +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
|
|
|
|
```
|