简体   繁体   English

to脚不发送从客户端收到的消息

[英]Stomp not sending the messages recieved from client

im facing a problem with the integration of stompjs and spring boot app .I tried a code unfortunately does not work i did not the reason .Actually the client fill a form and after submitting he sent the number of order to the other connected users via sockJS. 我遇到了stompjs和spring boot app集成的问题。不幸的是,我尝试执行代码不起作用,我没有原因。实际上,客户填写了表格,提交后,他通过sockJS将订单号发送给其他连接的用户。 This is the code please give me some advises to make it work : 这是代码,请给我一些建议以使其工作:

 $('#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(); } } } } } 

I solve it the declaration of ws and stompClient variables must be at the top of the script . 我解决了问题,ws和stompClient变量的声明必须在脚本的顶部。 But when i submit the form , the message sent mutilple time , i don't know the reason any one had idea !!!! 但是,当我提交表单时,该消息发送了很多时间,我不知道任何人有主意的原因!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM