1

类似于为 Spring Rsocket Server 使用 RSocket-Java 的 rsocket 路由元数据,但对于 RSocket Net Client,我们使用 spring boot @MessageMapping 用于端口 7000 上的 websocket 端点路由,这取决于路由返回 webfluxes。例如:

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

当 Spring Boot 服务器在本地运行时,要获得此通量,您可以使用rsc 客户端

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

或者对于流

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

要在 C# Net 中以编程方式执行此操作,它在此处表示 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());

谢谢

4

2 回答 2

2

在 .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;
};
于 2021-02-04T04:31:29.707 回答
0

要使 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}"));
于 2021-02-12T15:38:32.350 回答