簡體   English   中英

to腳不發送從客戶端收到的消息

[英]Stomp not sending the messages recieved from client

我遇到了stompjs和spring boot app集成的問題。不幸的是,我嘗試執行代碼不起作用,我沒有原因。實際上,客戶填寫了表格,提交后,他通過sockJS將訂單號發送給其他連接的用戶。 這是代碼,請給我一些建議以使其工作:

 $('#btn-save').on('click', function (e) { sendForm(); }); var ws; var stompClient; ws=new SockJS("/formordre"); stompClient = Stomp.over(ws); stompClient.connect({},function(frame){ stompClient.subscribe("/topic/formordre",function(message){ console.log("Received:" + message) ; toastr.options = { "closeButton": true, "debug": false, "newestOnTop": false, "progressBar": false, "positionClass": "toast-top-right", "preventDuplicates": false, "onclick": null, "showDuration": "300", "hideDuration": "1000", "timeOut": "0", "extendedTimeOut": "0", "showEasing": "swing", "hideEasing": "linear", "showMethod": "fadeIn", "hideMethod": "fadeOut" } toastr.info( message.body); }); },function(error){ console.log("Stomp protocol error "+ error); }); }); function sendForm(){ stompClient.send("/topic/formordre",{},$('#num_ord').val()); }; 
 package com.example.dot.web; import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; @Configuration @EnableWebSocketMessageBroker public class WebSocketBrokerConfig extends AbstractWebSocketMessageBrokerConfigurer{ @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/formordre").withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.setApplicationDestinationPrefixes("/app") .enableSimpleBroker("/topic","/queue"); } } 
 package com.example.dot.web; import java.io.IOException; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; import org.springframework.web.socket.handler.TextWebSocketHandler; @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(new QuestionHandler(), "/formordre").withSockJS(); } class QuestionHandler extends TextWebSocketHandler { private List<WebSocketSession> sessions = new CopyOnWriteArrayList<>(); @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { sessions.add(session); } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) { for (WebSocketSession s : sessions) { try { s.sendMessage(message); } catch (IOException e) { e.printStackTrace(); } } } } } 

我解決了問題,ws和stompClient變量的聲明必須在腳本的頂部。 但是,當我提交表單時,該消息發送了很多時間,我不知道任何人有主意的原因!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM