本文介绍了Javascript Rsocket 自定义元数据 - RSocket 错误 0x201(APPLICATION_ERROR):目标没有处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试遵循 spring-flights 示例,但我不断收到 RSocket 错误 0x201 (APPLICATION_ERROR):没有目的地处理程序.

I tried following the spring-flights example but i keep getting RSocket error 0x201 (APPLICATION_ERROR): No handler for destination.

元数据.js

import {JsonSerializer} from 'rsocket-core';

export class Metadata extends Map {

  constructor(json) {
      super();
      if (json != null) {
          for (let [key, value] of json) {
              this.set(key, value);
          }
      }
  }

  toJSON() {
      const result = {};
      for (let [key, value] of this.entries()) {
          result[key] = value;
      }
      return result;
  }

}

Metadata.ROUTE = "route";
Metadata.AUTHENTICATION_BEARER = "message/x.rsocket.authentication.bearer.v0";

export const JsonMetadataSerializer = {

  deserialize(data) {
    if (data == null) {
        return null;
    }
    let json = JsonSerializer.deserialize(data);
    return new Metadata(json);
  },

  serialize(metadata) {
    if (metadata == null) {
        return null;
    }
    let json = metadata.toJSON();
    return JsonSerializer.serialize(json);
  }

};

JsonMetadataSerializer.MIME_TYPE = "application/vnd.spring.rsocket.metadata+json";

但如果没有自定义元数据,它也能很好地工作.

But without the custom Metadata it works perfectly well.

serializers: {
            data: JsonSerializer,
            metadata: IdentitySerializer
}
metadataMimeType: 'message/x.rsocket.routing.v0',
socket.requestStream({ 
   metadata: String.fromCharCode('greet-stream'.length) + 'greet-stream',
})

我需要自定义元数据方面的帮助,以便我可以使用不记名身份验证

I need a help with a custom metadata so i can use bearer authentication

推荐答案

  1. 下载这个项目https://github.com/gregwhitaker/springboot-rsocketjwt-example 并运行 README 中描述的所有示例,以确保一切正常.
  2. hello-service项目的application.properties中添加spring.rsocket.server.transport=websocket.
  3. 像这样编写 rsocket-js 客户端:
  1. Download this project https://github.com/gregwhitaker/springboot-rsocketjwt-example and run all the examples described in README to be sure that everything works correctly.
  2. In the application.properties of hello-service project add spring.rsocket.server.transport=websocket.
  3. Write you rsocket-js client like this:
      const host = "127.0.0.1";
      const port = 8080;
      const keepAlive = 60000;
      const lifetime = 180000;
      const dataMimeType = "application/octet-stream";
      const metadataMimeType = MESSAGE_RSOCKET_COMPOSITE_METADATA.string;
      const route = "hello.secure";
      const bearerMimeType = "message/x.rsocket.authentication.bearer.v0";
      const jwtToken =
        "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyIiwiYXVkIjoiaGVsbG8tc2VydmljZSIsInNjb3BlIjoiVVNFUiIsImlzcyI6ImhlbGxvLXNlcnZpY2UtZGVtbyIsImV4cCI6MTU4ODMxNzE1OSwianRpIjoiZjQxZmZiYWUtNTk5My00YWU5LTkwMWMtMjU1ODY4NzEyYzZhIn0.VZlQ_rFTtwGukdVMBy_3VUI1EhfdzXLNPkylygUJUbQ";

      const client = new RSocketClient({
        setup: {
          keepAlive,
          lifetime,
          dataMimeType,
          metadataMimeType
        },
        transport: new RSocketWebSocketClient(
          {
            wsCreator: () => new WebSocket("ws://localhost:7000"),
            debug: true
          },
          BufferEncoders
        )
      });

      // Open the connection
      client.connect().then(socket => {
        socket
          .requestResponse({
            data: new Buffer("request-response JWT JS client"),
            metadata: encodeAndAddWellKnownMetadata(
              encodeAndAddCustomMetadata(
                Buffer.alloc(0),
                bearerMimeType,
                Buffer.from(jwtToken)
              ),
              MESSAGE_RSOCKET_ROUTING,
              Buffer.from(String.fromCharCode(route.length) + route)
            )
          })
          .subscribe({
            onComplete: data => {
              console.log("got response with requestResponse");
              const response = data.data.toString();
              console.log(response);
            },
            onError: error => {
              console.log("got error with requestResponse");
              console.error(error);
            },
            onSubscribe: cancel => {
              /* call cancel() to stop onComplete/onError */
            }
          });
      });

在浏览器控制台中,您应该看到:

In the browser console you should see:

got response with requestResponse
Hello, request-response JWT JS client! - from secured method

这篇关于Javascript Rsocket 自定义元数据 - RSocket 错误 0x201(APPLICATION_ERROR):目标没有处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:12