多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
>[danger] 该组件为系统组件,在组件树中只可命名为 registry,不可修改为其他名称。 ## 注册器 注册器负责指定 URL 的由具体哪个 Handler 来执行,与 HTTP 的路由类似。 | 类 | 调用 | | --- | --- | | Mix\WebSocket\Registry | app()->registry | ## 依赖注入配置 [>> 到 GitHub 查看默认配置 <<](https://github.com/mix-php/mix/blob/v2/applications/websocket/config/main_coroutine.php#L160) ## 注册规则 ``` // 注册规则 'rules' => [ '/websocket' => ['WebSocketHandler', 'interceptor' => 'WebSocketInterceptor'], ], ``` 规则详解: - `/websocket` 为绑定的 URL。 - `WebSocketHandler` 为处理器类名,与 `handlerNamespace` 参数组成完整的类路径。 - `WebSocketInterceptor` 为拦截器类名,与 `interceptorNamespace` 参数组成完整的类路径。 ## Handler 当 swoole_websocket_server 服务器触发以下事件时,注册器会调用处理器的以下方法: - `onOpen ` 事件调用 `open ` 方法,当配置中开启 `enable_handshake` 时不触发 [查看](https://github.com/mix-php/mix/blob/v2/applications/websocket/config/websocketd.php#L19)。 - `onMessage ` 事件调用 `message` 方法 - `onClose ` 事件调用 `close` 方法 [>> 到 GitHub 查看使用范例 <<](https://github.com/mix-php/mix/blob/v2/applications/websocket/src/Handlers/WebSocketHandler.php) ## Interceptor 当配置中开启 `enable_handshake` 时 [查看](https://github.com/mix-php/mix/blob/v2/applications/websocket/config/websocketd.php#L19),拦截器才会执行,拦截器负责 WebSocket 的握手处理。 当 swoole_websocket_server 服务器触发以下事件时,注册器会调用拦截器的以下方法: - `onHandshake ` 事件调用 `handshake` 方法 ### v13 握手 框架提供了拦截器基类,默认提供了 v13 的握手处理,用户只需继承即可使用: [>> 到 GitHub 查看使用范例 <<](https://github.com/mix-php/mix/blob/v2/applications/websocket/src/Interceptors/WebSocketInterceptor.php)