Code Monkey home page Code Monkey logo

Comments (6)

linux-china avatar linux-china commented on June 26, 2024

@halimpuckjava if you use Spring RSocket with Spring Boot, then you should use Spring's RSocketRequester to call RSocket Service and link is https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#rsocket-requester

from rsocket.

Kotlin-GDE avatar Kotlin-GDE commented on June 26, 2024

@linux-china thanks for response

yes i know with RSocketRequester you can resolve this issue, so if my client is android or desktop how i can communicate with spring boot rsocket without add RSocketRequester to client dependencies ?

with RSocketRequester you can specify distination (path of handler) with route(), however rsocket don"t have this feature

from rsocket.

linux-china avatar linux-china commented on June 26, 2024

@halimpuckjava you can refer following code.

import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.rsocket.RSocket;
import io.rsocket.RSocketFactory;
import io.rsocket.metadata.CompositeMetadataFlyweight;
import io.rsocket.metadata.TaggingMetadataFlyweight;
import io.rsocket.metadata.WellKnownMimeType;
import io.rsocket.uri.UriTransportRegistry;
import io.rsocket.util.DefaultPayload;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.Collections;

public class RSocketClientTest {
    private ObjectMapper objectMapper = new ObjectMapper();
    private static RSocket rsocket;

    @BeforeAll
    public static void setUp() throws Exception {
        rsocket = RSocketFactory
                .connect()
                .dataMimeType(WellKnownMimeType.APPLICATION_JSON.getString())
                .metadataMimeType(WellKnownMimeType.MESSAGE_RSOCKET_COMPOSITE_METADATA.getString())
                .transport(UriTransportRegistry.clientForUri("ws://127.0.0.1:8088/rsocket"))
                .start()
                .block();
    }

    @AfterAll
    public static void tearDown() {
        rsocket.dispose();
    }

    @Test
    public void testRequestResponse() throws Exception {
        CompositeByteBuf compositeByteBuf = compositeMetadataWithRouting("org.mvnsearch.account.AccountService.findById");
        byte[] jsonData = objectMapper.writeValueAsBytes(1);
        rsocket.requestResponse(DefaultPayload.create(Unpooled.wrappedBuffer(jsonData), compositeByteBuf))
                .subscribe(payload -> {
                    System.out.println(payload.getDataUtf8());
                });
        Thread.sleep(500000);
    }

    private CompositeByteBuf compositeMetadataWithRouting(String routingKey) {
        PooledByteBufAllocator allocator = PooledByteBufAllocator.DEFAULT;
        CompositeByteBuf compositeByteBuf = allocator.compositeDirectBuffer();
        ByteBuf routingMetadata = TaggingMetadataFlyweight.createTaggingContent(allocator, Collections.singletonList(routingKey));
        CompositeMetadataFlyweight.encodeAndAddMetadata(compositeByteBuf, allocator, WellKnownMimeType.MESSAGE_RSOCKET_ROUTING, routingMetadata);
        return compositeByteBuf;
    }
}

Just three things:

  • Call ".dataMimeType(WellKnownMimeType.APPLICATION_JSON.getString())" to connect Spring Boot RSocket Service, and payload's bytes data should be encoded with this type for next calls.
  • Create a composite metadata with routing info for Payload and method is compositeMetadataWithRouting
  • Check Spring RSocket document to choose correct RSocket call: resquestResponse, requestStream, fireAndForget and requestChannel

from rsocket.

linux-china avatar linux-china commented on June 26, 2024

@halimpuckjava my bad. you are using rsocket-kotlin to call Spring Boot RSocket service, and you can construct basic composite metadata to implement routing.

rsocket/rsocket-kotlin#75

from rsocket.

Kotlin-GDE avatar Kotlin-GDE commented on June 26, 2024

@linux-china thanks

from rsocket.

OlegDokuka avatar OlegDokuka commented on June 26, 2024

closing this one as not recent activity

from rsocket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.