RSocket Net 客户端请求流路由元数据到 spring 引导 @MessageMapping 路由

RSocket Net client request stream routing metadata to spring boot @MessageMapping routes

类似于 但对于 RSocket 网络客户端,我们使用 spring 引导 @MessageMapping 端口 7000 上的 websocket 端点路由,return webfluxes 取决于路由。例如:

@MessageMapping(value = "helloWorld")
public Flux<String> getFluxSocket() {
    log.traceEntry();
    log.info("In hello world");
    
    return Flux.just("{\"Hello\":\"World\"}");
}

当 spring 引导服务器在本地 运行 时,要获得此通量,您可以使用 rsc client

java -jar rsc.jar --debug --request --route helloWorld ws://localhost:7000

或者对于流

java -jar rsc.jar --debug --stream --route myStream ws://localhost:7000

要在 C# Net 中以编程方式执行此操作,它表示 here RSocket Net 尚不支持请求路由,但可以使用元数据负载。有没有人得到与此等效的网络?

CompositeByteBuf metadata = ByteBufAllocator.DEFAULT.compositeBuffer();
RoutingMetadata routingMetadata = TaggingMetadataCodec.createRoutingMetadata(ByteBufAllocator.DEFAULT, List.of("/route"));
CompositeMetadataCodec.encodeAndAddMetadata(metadata,
        ByteBufAllocator.DEFAULT,
        WellKnownMimeType.MESSAGE_RSOCKET_ROUTING,
        routingMetadata.getContent());

谢谢

在 .NET 库正式支持路由/复合元数据之前,您可以实施路由元数据。 如果不需要发送除路由元数据之外的任何元数据,则不需要创建复合元数据。仅发送路由元数据非常简单。

从规范中可以看出,只需将路由名称的长度添加到第一个字节即可。 https://github.com/rsocket/rsocket/blob/master/Extensions/Routing.md

我不了解 .NET,所以我将向您展示如何在 Java 和 JavaScript 中实现它。仅供参考。

https://github.com/making/demo-rsocket/blob/master/vanilla-rsocket-client/src/main/java/com/example/vanillarsocketclient/VanillaRsocketClientApplication.java

static ByteBuffer routingMetadata(String tag) {
    final byte[] bytes = tag.getBytes(StandardCharsets.UTF_8);
    final ByteBuffer buffer = ByteBuffer.allocate(1 + bytes.length);
    buffer.put((byte) bytes.length);
    buffer.put(bytes);
    buffer.flip();
    return buffer;
}

https://github.com/making/demo-rsocket-jsug/blob/master/frontend/vanilla/src/index.js

const routingMetadata = (route) => {
    return String.fromCharCode(route.length) + route;
};

要获得适用于字符串客户端的 RSocket 网络路由,请使用

var client = new RSocketClient(new WebSocketTransport("ws://127.0.0.1:7000/"));

Console.WriteLine("Connect Async");
await client.ConnectAsync(new RSocketOptions()
{
    MetadataMimeType = "message/x.rsocket.routing.v0",      
    DataMimeType = "application/json"
});

String json = "{\"Hello\":\"World\"}

byte[] intBytes = BitConverter.GetBytes(6);
string stringBytes = Encoding.Default.GetString(intBytes, 0, 1);
string metaData = stringBytes + route;

var stringclient = new RSocketClient.ForStrings(client);
await stringclient.RequestStream(json, metaData)
.ForEachAsync((result) =>
{
    Console.WriteLine($"Result ===> {result}");
});

要使 RSocket 网络路由适用于二进制客户端,请使用

var client = new RSocketClient(new WebSocketTransport("ws://localhost:8080/"));
await client.ConnectAsync(new RSocketOptions()
{
    MetadataMimeType = "message/x.rsocket.routing.v0",
    DataMimeType = "application/octet-stream"
});

Console.WriteLine("Requesting Raw Protobuf Stream...");

var route = new ReadOnlySequence<byte>(new byte[]
{
    (byte) Encoding.UTF8.GetByteCount("request.stream")
}.Concat(Encoding.UTF8.GetBytes("request.stream")).ToArray());

//Make a Raw binary call
var stream = client.RequestStream(
    resultmapper: result => 
(Data: Encoding.UTF8.GetString(result.data.ToArray()), 
Metadata: Encoding.UTF8.GetString(result.metadata.ToArray())),
data: new ReadOnlySequence<byte>(Encoding.UTF8.GetBytes("test")), 
metadata: route);

await stream.ForEachAsync(persons => 
Console.WriteLine($"RawDemo.OnNext===>[{persons.Metadata}]{persons.Data}"));