2.124. Vert.x WebSocket
此扩展允许您创建 WebSocket 端点作为 WebSocket 服务器,或作为客户端来连接现有的 WebSocket。
它基于 quarkus-vertx-http
扩展提供的 Eclipse Vert.x HTTP 服务器构建。
2.124.1. 什么是内部
-
Vert.x WebSocket 组件, URI 语法:
vertx-websocket:host:port/path
有关用法和配置详情,请参阅上述链接。
2.124.2. Maven 协调
在 code.quarkus.redhat.com 上使用此扩展创建新项目
或者将协调添加到现有项目中:
<dependency> <groupId>org.apache.camel.quarkus</groupId> <artifactId>camel-quarkus-vertx-websocket</artifactId> </dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-vertx-websocket</artifactId>
</dependency>
2.124.3. 使用方法
2.124.3.1. Vert.x WebSocket consumers
当您创建带有 from ("vertx-websocket")
的 Vert.x WebSocket consumer (E.g)时,URI 中的主机和端口配置将冗余,因为 WebSocket 将始终托管在 Quarkus HTTP 服务器上。
可以简化消费者的配置,使其仅包含 WebSocket 的资源路径。例如:
from("vertx-websocket:/my-websocket-path") .setBody().constant("Hello World");
from("vertx-websocket:/my-websocket-path")
.setBody().constant("Hello World");
虽然您不需要在 vertx-websocket 消费者上显式配置 host/port。如果选择,主机和端口必须与 quarkus.http.host
和 quarkus.http.port
的 Quarkus HTTP 服务器配置值完全匹配。否则,将在运行时抛出异常。
2.124.3.2. Vert.x WebSocket producers
与以上类似,如果要生成信息到内部 Vert.x WebSocket 消费者,则可以从端点 URI 省略主机和端口。
from("vertx-websocket:/my-websocket-path") .log("Got body: ${body}"); from("direct:sendToWebSocket") .log("vertx-websocket:/my-websocket-path");
from("vertx-websocket:/my-websocket-path")
.log("Got body: ${body}");
from("direct:sendToWebSocket")
.log("vertx-websocket:/my-websocket-path");
或者,您可以引用 Quarkus HTTP 服务器的完整主机和端口配置。
from("direct:sendToWebSocket") .log("vertx-websocket:{{quarkus.http.host}}:{{quarkus.http.port}}/my-websocket-path");
from("direct:sendToWebSocket")
.log("vertx-websocket:{{quarkus.http.host}}:{{quarkus.http.port}}/my-websocket-path");
在向外部 WebSocket 服务器生成消息时,您必须始终提供主机名和端口(如果需要)。
2.124.4. 其他 Camel Quarkus 配置
2.124.4.1. Vert.x WebSocket 服务器配置
Vert.x WebSocket 服务器的配置由 Quarkus 管理。有关配置选项的完整列表,请参阅 Quarkus HTTP 配置指南。
要为 Vert.x WebSocket 服务器配置 SSL,请按照 与 SSL 指南 的安全连接 操作。请注意,目前不支持使用 SSLContextParameters
为 SSL 配置服务器。
2.124.4.2. 字符编码
如果您希望应用程序使用非默认编码发送或接收请求,请参阅 Native 模式指南的 Character encodings 部分。