【问题标题】:Error on Delivery Message on Spring WebSocket using StompClient使用 StompClient 在 Spring WebSocket 上传递消息时出错
【发布时间】:2014-04-09 03:39:25
【问题描述】:

我正在尝试向我的主题发送消息,但问题是当我发送消息时没有任何反应...我使用的是 apache tomcat 7.0.53

更新:04/15:测试链接:

http://ec2-54-187-72-145.us-west-2.compute.amazonaws.com:8080/kupo

登录:管理员

密码:管理员

访问 TOMCAT 日志的链接:

http://ec2-54-187-72-145.us-west-2.compute.amazonaws.com:28778/

P.S:您需要选中侧边栏上的组合框才能开始观看消息

Github 链接:https://github.com/tiarebalbi/kupo

日志:

DEBUG - gWebSocketHandlerDecorator - Connection established, SockJS session id=_mqg8qer, uri=/kupo/application/807/_mqg8qer/websocket
DEBUG - StompDecoder               - Decoded [Payload byte[0]][Headers=    {stompCommand=CONNECT, nativeHeaders={heart-beat=[10000,10000], accept-version=[1.1,1.0]},     simpMessageType=CONNECT, id=e79a615e-5522-a0f9-aecf-6ea5a54b3d9b, timestamp=1397013491497}]
DEBUG - StompEncoder               - Encoded STOMP command=CONNECTED headers={user-name=[balbi], heart-beat=[0,0], version=[1.1]}
DEBUG - StompDecoder               - Decoded [Payload byte[0]][Headers={stompCommand=SUBSCRIBE, nativeHeaders={id=[sub-0], destination=[/topic/greetings]}, simpMessageType=SUBSCRIBE, simpSubscriptionId=sub-0, simpDestination=/topic/greetings, id=42c2019d-96a0-95f0-29aa-2bcc62d6d721, timestamp=1397013491501}]

代码:

@Service
public class ExampleServiceImpl implements ApplicationListener<BrokerAvailabilityEvent> {

private AtomicBoolean brokerAvailable = new AtomicBoolean();

@Autowired
private MessageSendingOperations<String> messagingTemplate;

@Override
public void onApplicationEvent(BrokerAvailabilityEvent event) {
    this.brokerAvailable.set(event.isBrokerAvailable());
}

@Scheduled(fixedDelay=3000)
public void testing() {
    if (this.brokerAvailable.get()) {
        this.messagingTemplate.convertAndSend("/topic/greetings", "Testing....");
    }
}

Javascript 连接:

        var socket = new SockJS('/kupo/application'); // <!-- My endpoint
        var stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            
            var username = frame.headers['user-name'];
            console.log("User connected: " + username);
            
            stompClient.subscribe("/topic/greetings", function(message) { // <-- Topic where I want to received the message
                console.log("TOPIC:",message);
            });
            
        } , function(error) {
            console.log("STOMP protocol error " + error);
        });

浏览器控制台:

Opening Web Socket... stomp.min.js:8
Web Socket Opened... stomp.min.js:8
>>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000

<<< CONNECTED
user-name:balbi
heart-beat:0,0
version:1.1

connected to server undefined stomp.min.js:8
User connected: balbi 
>>> SUBSCRIBE
id:sub-0
destination:/topic/greetings

Websocket 上下文配置:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketApplicationContext extends AbstractWebSocketMessageBrokerConfigurer {

@Autowired
private Environment env;

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    if (env.acceptsProfiles("test.tomcat")) {
        registry.addEndpoint("/application")
                .setHandshakeHandler(
                        new DefaultHandshakeHandler(new TomcatRequestUpgradeStrategy()))
                .withSockJS();
    } else {
        registry.addEndpoint("/application").withSockJS();
    }
}

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

【问题讨论】:

  • 您的浏览器控制台中有日志吗?你能在你的网络标签中看到一个 websocket 连接吗?
  • 完成,我已经包含了浏览器控制台日志。
  • 将代码的github链接贴出来可行吗?
  • 你认为你可以在 github 上发布一个最小的问题示例吗?整个应用程序我无法运行它,它会尝试连接到数据库。还有 InicializacaoView.js,其中问题的代码似乎没有在应用程序的任何地方使用

标签: java spring websocket stomp spring-websocket


【解决方案1】:

连接到您的应用程序时,我设法从 JavaScript 控制台向主题发送了一条消息,并在网页中返回一条消息。

var socket = new SockJS('/kupo/application');
var stompClient = Stomp.over(socket);
stompClient.send('/topic/greetings',{},"hello");

我收到了:

<<< MESSAGE
subscription:sub-0
content-length:5
message-id:ts3oov6b-1
destination:/topic/greetings
content-length:5

hello 

您的调度任务是否按预期调用?

为什么你在你的主要配置中是importing some Configurations and still scanning the configuration package?不应该是其中之一吗?

【讨论】:

  • 感谢您对配置文件的评论,我已经修复了。调度工作正常。问题是当我需要从服务器向客户端发送消息时。我正在创建一个访问 tomcat 日志的链接。
  • 点击链接从服务器访问 de catalina.out:ec2-54-187-72-145.us-west-2.compute.amazonaws.com:28778
  • 为 org.springframework.messaging 启用跟踪级别日志记录。然后,您可以跟踪服务器端的消息发生了什么。
  • 对我来说,“DefaultSubscriptionRegistry - 为destination=/topic/greetings 找到0 个订阅”看起来更有可能是罪魁祸首。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 2014-06-03
  • 2020-02-03
  • 1970-01-01
  • 2014-06-27
  • 2018-02-05
相关资源
最近更新 更多