我尝试在 Java 中实现 RSocket 服务器,在 JavaScript 中实现客户端,但我无法在后端调用任何方法。
Java服务器
public final class RawServer {
public static void main(String[] args) {
RSocketFactory.receive()
.acceptor((setup, sendingSocket) -> Mono.just(new DefaultSimpleService()))
.transport(WebsocketServerTransport.create("localhost", 8801))
.start()
.block()
.onClose()
.block();
}
private static final class DefaultSimpleService extends AbstractRSocket {
private ObjectMapper jsonMapper = new ObjectMapper();
@Override
public Flux<Payload> requestStream(Payload payload) {
return Mono.just(payload.getDataUtf8())
.map(json -> {
try {
return jsonMapper.readValue(json, Message.class);
} catch (IOException e) {
e.printStackTrace();
return null;
}
})
.doOnNext(msg -> System.out.println("got message " + msg.message))
.flatMapMany(msg -> Flux.range(0, 5)
.map(count -> msg.message + " #" + count))
.map(message -> DefaultPayload.create(message));
}
}
}
public class Message {
public final String message;
@JsonCreator
public Message(@JsonProperty("message") String message) {
this.message = message;
}
}
JavaScript 客户端
import { RSocketClient, JsonSerializers } from "rsocket-core";
import RSocketWebSocketClient from "rsocket-websocket-client";
const transport = new RSocketWebSocketClient({
url: "ws://localhost:8801"
});
好像建立了 WebSocket 连接,但没有消息推送到服务器。我怎样才能做到这一点?
我还用 Java 实现了客户端,它工作正常。我找到的 JavaScript 示例是https://github.com/rsocket/rsocket-js/blob/master/docs/01-client-configuration.md,但我无法使其工作。
相关分类