首页 > 解决方案 > 跺脚不发送从客户端收到的消息

问题描述

我面临着 stompjs 和 Spring Boot 应用程序集成的问题。不幸的是,我尝试了一个代码不起作用我没有原因。实际上,客户填写表格,提交后他通过 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();
				}
			}

		}

	}

}

标签: javascriptspring-bootwebsocketsockjsstompjs

解决方案


我解决了 ws 和 stompClient 变量的声明必须在脚本的顶部。但是当我提交表单时,消息发送了多次,我不知道任何人有想法的原因!!!!


推荐阅读