我能够在Spring Web MVC应用程序中连接消息并将消息发送到Web套接字,但是客户端(Android)端未接收到发送消息。 我正在使用NaikSoftware/StompProtocolAndroid发送和接收消息。我正在尝试将其退还给我。我检查了数据库,并看到了发送消息,但是我无法接收它。这是我的用于接收/订阅的android代码
mStompClient.topic("/app/user/queue/")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(topicMessage -> {
M.log( "Received " + topicMessage.getPayload());
});
这是客户端的发送消息功能
public void sendMessage(String message, String user) {
ChatMessage chatMessage = new ChatMessage();
chatMessage.createdOn = LocalDateTime.now().toString();
chatMessage.from ="TFM1001";
chatMessage.to = user;
chatMessage.message = message;
mStompClient.send("/chat/room/+user, mGson.toJson(chatMessage))
.compose(applySchedulers())
.subscribe(() -> {
M.log( "STOMP echo send successfully");
}, throwable -> {
M.log("Error send STOMP echo", throwable.getMessage());
});
}
这是WebSocket配置
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/queue/");
config.setApplicationDestinationPrefixes("/chat");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").withSockJS();
}
这是我的聊天控制器
@Controller
public class ChatController {
@Autowired
private ChatRepository chatRepository;
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;
@MessageMapping("/room/{user}")
public void sendMessageToUser(@Payload ChatMesage message, @DestinationVariable("user") String username) {
simpMessagingTemplate.convertAndSend("/app/user/queue/"+username, message);
chatRepository.addChat(message);
}
}
我在这里不使用任何身份验证 谁知道我为什么没有在客户端收到消息