WebSocket 服务

Hyperf 提供了对 WebSocket Server 的封装,可基于 hyperf/websocket-server 组件快速搭建一个 WebSocket 应用。

安装

  1. composer require hyperf/websocket-server

配置 Server

修改 config/autoload/server.php,增加以下配置。

  1. <?php
  2. 'servers' => [
  3. [
  4. 'name' => 'ws',
  5. 'type' => Server::SERVER_WEBSOCKET,
  6. 'host' => '0.0.0.0',
  7. 'port' => 9502,
  8. 'sock_type' => SWOOLE_SOCK_TCP,
  9. 'callbacks' => [
  10. SwooleEvent::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
  11. SwooleEvent::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
  12. SwooleEvent::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
  13. ],
  14. ],
  15. ],

配置路由

目前暂时只支持配置文件的模式配置路由,后续会提供注解模式。

config/routes.php 文件内增加对应 ws 的 Server 的路由配置,这里的 ws 值取决于您在 config/autoload/server.php 内配置的 WebSocket Server 的 name 值。

  1. <?php
  2. Router::addServer('ws', function () {
  3. Router::get('/', 'App\Controller\WebSocketController');
  4. });

创建对应控制器

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Hyperf\Contract\OnCloseInterface;
  5. use Hyperf\Contract\OnMessageInterface;
  6. use Hyperf\Contract\OnOpenInterface;
  7. use Swoole\Http\Request;
  8. use Swoole\Server;
  9. use Swoole\Websocket\Frame;
  10. use Swoole\WebSocket\Server as WebSocketServer;
  11. class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
  12. {
  13. public function onMessage(WebSocketServer $server, Frame $frame): void
  14. {
  15. $server->push($frame->fd, 'Recv: ' . $frame->data);
  16. }
  17. public function onClose(Server $server, int $fd, int $reactorId): void
  18. {
  19. var_dump('closed');
  20. }
  21. public function onOpen(WebSocketServer $server, Request $request): void
  22. {
  23. $server->push($request->fd, 'Opened');
  24. }
  25. }

接下来启动 Server,便能看到对应启动了一个 WebSocket Server 并监听于 9502 端口,此时您便可以通过各种 WebSocket Client 来进行连接和进行数据传输了。

  1. $ php bin/hyperf.php start
  2. [INFO] Worker#0 started.
  3. [INFO] WebSocket Server listening at 0.0.0.0:9502
  4. [INFO] HTTP Server listening at 0.0.0.0:9501

连接上下文

WebSocket 服务的 onOpen, onMessage, onClose 回调并不在同一个协程下触发,因此不能直接使用协程上下文存储状态信息。WebSocket Server 组件提供了 连接级 的上下文,API 与协程上下文完全一样。

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Hyperf\Contract\OnMessageInterface;
  5. use Hyperf\Contract\OnOpenInterface;
  6. use Hyperf\WebSocketServer\Context;
  7. use Swoole\Http\Request;
  8. use Swoole\Websocket\Frame;
  9. use Swoole\WebSocket\Server as WebSocketServer;
  10. class WebSocketController implements OnMessageInterface, OnOpenInterface
  11. {
  12. public function onMessage(WebSocketServer $server, Frame $frame): void
  13. {
  14. $server->push($frame->fd, 'Username: ' . Context::get('username'));
  15. }
  16. public function onOpen(WebSocketServer $server, Request $request): void
  17. {
  18. Context::set('username', $request->cookie['username']);
  19. }
  20. }

多 server 配置

  1. # /etc/nginx/conf.d/ng_socketio.conf
  2. # 多个 ws server
  3. upstream io_nodes {
  4. server ws1:9502;
  5. server ws2:9502;
  6. }
  7. server {
  8. listen 9502;
  9. # server_name your.socket.io;
  10. location / {
  11. proxy_set_header Upgrade "websocket";
  12. proxy_set_header Connection "upgrade";
  13. # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14. # proxy_set_header Host $host;
  15. # proxy_http_version 1.1;
  16. # 转发到多个 ws server
  17. proxy_pass http://io_nodes;
  18. }
  19. }