我正在尝试使用 WebSocket踩踏客户端实现Spring Boot聊天应用程序。如果我将邮件从一台设备发送到4,5台设备,则有些收到消息,有些则没有。有些可以发送消息,但不接收任何消息,有些则可以正常工作。我的应用程序在Wildfly服务器上运行,并且URL通过 https 。
这是我的js文件。在我的JSP页面上,我使用所有参数调用sendMsg,并通过render方法,使用Handlebars将响应附加到JSP。
if (!window.location.origin) {
window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
}
const url = window.location.origin+contextPath;
let stompClient;
let selectedUser;
let newMessages = new Map();
function connectToChat(userName, topicName) {
console.log("connecting to chat...")
let socket = new SockJS(url + '/chat');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log("connected to: " + frame);
stompClient.subscribe("/topic/decision-log", function (response) {
let data = JSON.parse(response.body);
var msg = data.message;
var fromlogin = data.message;
render(data.username, msg, fromlogin);
});
});
}
connectToChat("1", "decision-log");
function sendMsg(from, text, username) {
stompClient.send("/app/chat/" + from, {}, JSON.stringify({
fromLogin: from,
message: text,
topicName: topicName,
username: username
}));
}
function render(username, message, projectId) {
var templateResponse = Handlebars.compile($("#message-response-template").html());
var contextResponse = {
username: username,
response: message,
date: date,
projectId: projectId
};
setTimeout(function () {
$chatHistoryList.append(templateResponse(contextResponse));
scrollToBottom();
}.bind(this), 1500);
}
这是我的WebSocket配置文件:
@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfiguration implements WebSocketMessageBrokerConfigurer{
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat").setAllowedOrigins("*").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app").enableSimpleBroker("/topic");
}
}
这是控制器。我总是将所有通过WebSocket传递的消息保存在数据库中,这就是为什么我可以确保所有设备都可以发送已保存在数据库中的消息的原因。
@Controller
@AllArgsConstructor
public class MessageController {
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;
private final DecisionLogService decisionLogService;
@MessageMapping("/chat/{to}")
public void sendMessage(@DestinationVariable String to, MessageModel message, Authentication authentication ) {
simpMessagingTemplate.convertAndSend("/topic/decision-log", message);
AuthResponse userDetails = (AuthResponse) authentication.getDetails();
DecisionLogCreateRequest decisionLogCreateRequest = new DecisionLogCreateRequest();
decisionLogCreateRequest.setDecision(message.getMessage());
decisionLogCreateRequest.setProjectId(to);
ServiceResponseExtended<Boolean> response = decisionLogService.addDecisionLog(userDetails.getAccessToken(), decisionLogCreateRequest);
}
}
我找不到与此问题类似的东西。请为我提供正确的信息和建议,如果有人遇到相同的问题,请与我分享。
答案 0 :(得分:0)
在将RabbitMQ Stomp Broker配置为消息代理而不是SimpleBroker之后,此问题已解决。
当前的WebSocket配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebsocketConfiguration implements WebSocketMessageBrokerConfigurer{
@Value("${stomp-broker-relay-host}")
private String RELAY_HOST;
@Value("${stomp-broker-relay-port}")
private String RELAY_PORT;
@Value("${stomp-broker-login-user}")
private String USER;
@Value("${stomp-broker-login-pass}")
private String PASS;
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat").setAllowedOrigins("*").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableStompBrokerRelay("/topic").setRelayHost(RELAY_HOST).setRelayPort(Integer.parseInt(RELAY_PORT)).setClientLogin(USER)
.setClientPasscode(PASS);
}
}