im面临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();
}
}
}
}
}
答案 0 :(得分:0)
我解决了这个问题,ws和stompClient变量的声明必须在脚本的顶部。但是,当我提交表单时,该消息发送了很多时间,我不知道任何人有想法的原因!!!!