gpt4 book ai didi

java - Flutter stomp 客户端无法与 spring stomp 客户端连接

转载 作者:行者123 更新时间:2023-12-01 16:14:10 28 4
gpt4 key购买 nike

我是 Flutter 新手。我正在尝试连接到使用 stomp 的 spring websocket,但连接失败。我正在使用 stomp_dart_client 0.3.3。 Spring服务器工作正常。我可以在浏览器上看到消息。

这是我的 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 Controller :

@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()) + "!");
}

flutter 实现:

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

最佳答案

将其拉回到提到 stomp_dart_client 的原始帖子,我努力在互联网和 Stack Overflow 上找到这方面的工作示例,所以这是我的解决方案,可能会有所帮助。

我正在使用带有 Web STOMP 插件的 RabbitMQ:https://www.rabbitmq.com/web-stomp.html ,将 JSON 数据包发布到名为“ping”的队列上。 stomp_dart_client 的示例和文档不清楚如何连接到安全 session ,我通过在 header 中添加身份验证来解决这个问题:

import 'dart:convert';

import 'package:stomp_dart_client/stomp.dart';
import 'package:stomp_dart_client/stomp_config.dart';
import 'package:stomp_dart_client/stomp_frame.dart';

dynamic onConnect(StompClient client, StompFrame frame) {
client.subscribe(
destination: 'ping',
callback: (StompFrame frame) {
var jsonData = json.decode(frame.body);
print(jsonData);
});
}

final stompClient = StompClient(
config: StompConfig(
url: 'ws://x.x.x.x:15674/ws',
onConnect: onConnect,
onWebSocketError: (dynamic error) => print(error.toString()),
stompConnectHeaders: {
'login': 'someuserlogin',
'passcode': 'somepassword'
},
));

void main() {
stompClient.activate();
}

关于java - Flutter stomp 客户端无法与 spring stomp 客户端连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62448665/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com