Клиент Flutter Stomp не может подключиться к клиенту Spring Stomp - PullRequest
0 голосов
/ 18 июня 2020

Я впервые во Flutter. Я пытаюсь подключиться к весеннему веб-сокету, который использует топт, но соединение не удается. Я использую Stomp_dart_client 0.3.3. Spring sever работает нормально. Я могу видеть сообщения в браузере.

Это моя реализация Spring:

Класс конфигурации Spring:

@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/app");
}

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/gs-guide-websocket").withSockJS();
}

Контроллер Spring:

@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
    Thread.sleep(1000); // simulated delay
    return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
}

Реализация флаттера:

StompClient stompClient = StompClient(
        config: StompConfig(
          url: 'ws://10.0.2.2:7000',
          onConnect:(ss, dd)=> ss.subscribe (
            destination: '/topic/greetings/gs-guide-websocket',
            callback: (StompFrame frame) {
              print(frame.body);
            },
          ),
          onWebSocketError: (dynamic error) => print(error.toString()),
          onStompError: (d) => print("stomp error"),
          onDisconnect: (d)=> print("disconnect"),
        ));

   stompClient.activate();

   stompClient.subscribe (
        destination: '/topic/greetings',
        callback: (StompFrame frame) {
          print(frame.body);
          List<dynamic> result = json.decode(frame.body);
          return result;
        },
    );

это ошибка:

E/flutter (30825): [ERROR:flutter/shell/common/shell.cc(213)] Dart Error: Unhandled exception:
E/flutter (30825): NoSuchMethodError: The method 'subscribe' was called on null.
E/flutter (30825): Receiver: null
E/flutter (30825): Tried calling: subscribe(callback: Closure: (StompFrame) => List<dynamic>, destination: "/topic/greetings", headers: null)
E/flutter (30825): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
E/flutter (30825): #1      StompClient.subscribe (package:stomp_dart_client/stomp.dart:74:21)
E/flutter (30825): #2      _MyHomePageState._receivedMessage (package:tutorial_projecect/main.dart:154:16)
E/flutter (30825): #3      _MyHomePageState.build (package:tutorial_projecect/main.dart:90:41)
E/flutter (30825): #4      StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
E/flutter (30825): #5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
E/flutter (30825): #6      StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
E/flutter (30825): #7      Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
E/flutter (30825): #8      StatefulElement.update (package:flutter/src/widgets/framework.dart:4707:5)
E/flutter (30825): #9      Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #10     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
E/flutter (30825): #11     Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #12     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
E/flutter (30825): #13     Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
E/flutter (30825): #14     StatelessElement.update (package:flutter/src/widgets/framework.dart:4583:5)
E/flutter (30825): #15     Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #16     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
E/flutter (30825): #17     Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #18     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
E/flutter (30825): #19     Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #20     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
E/flutter (30825): #21     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
E/flutter (30825): #22     Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
E/flutter (30825): #23     StatefulElement.update (package:flutter/src/widgets/framework.dart:4707:5)
E/flutter (30825): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #25     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
E/flutter (30825): #26     Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #27     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
E/flutter (30825): #28     Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #29     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
E/flutter (30825): #30     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
E/flutter (30825): #31     Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
E/flutter (30825): #32     StatefulElement.update (package:flutter/src/widgets/framework.dart:4707:5)
E/flutter (30825): #33     Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #34     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
E/flutter (30825): #35     Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
E/flutter (30825): #36     StatelessElement.update (package:flutter/src/widgets/framework.dart:4583:5)
E/flutter (30825): #37     Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #38     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
E/flutter (30825): #39     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
E/flutter (30825): #40     Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
E/flutter (30825): #41     StatefulElement.update (package:flutter/src/widgets/framework.dart:4707:5)
E/flutter (30825): #42     Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
E/flutter (30825): #43     SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
E/flutter (30825): #44     Element.updateCh

Ответы [ 2 ]

1 голос
/ 12 августа 2020

Во-первых.

Ваш исходный пост должен использовать stompClient.subscribe в обратном вызове onConnect. Если вы подписались до подключения, клиентский объект будет нулевым.

Во-вторых.

Поскольку вы изменили файл lib. Вы должны показать свой новый код. Я пробовал как lib. И топать: ^ 0.8.0 дает

java.lang.IllegalArgumentException: Invalid character found in method name [STOMP0x0aaccept-version:1.20x0a0x0a...]. HTTP method names must be tokens

Все еще не удается заставить его работать ...

0 голосов
/ 20 июня 2020

После некоторого расследования и повторных попыток я обнаружил проблему. registerStompEndpoints не должны возвращать Sock Js.

И я использовал другую библиотеку Stomp: Stomp: ^ 0.8.0

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/gs-guide-websocket");
}
...