我需要一个移动应用程序将websocket添加到后端以实现聊天。我找到了一个示例https://github.com/callicoder/spring-boot-websocket-chat-demo,但是这里的聊天不是针对移动应用的,我不太了解如何请求聊天?应该添加api,但是在哪里?
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/topic");
}
我有一个addEndpoint
,我应该在其中写api吗?
由于我的应用程序无法与浏览器一起使用,如何替换.withSockJS ()
?
@RestController
@RequestMapping(name = "api/chat")
public class ChatController {
@MessageMapping("/chat.sendMessage")
@SendTo("/topic/public")
public ChatMessage sendMessage(@Payload ChatMessage chatMessage) {
return chatMessage;
}
@SendTo("/topic/public")
public ChatMessage addUser(@Payload ChatMessage chatMessage,
SimpMessageHeaderAccessor headerAccessor) {
headerAccessor.getSessionAttributes().put("username", chatMessage.getSender());
return chatMessage;
}
}
还是数据应该通过此api?
我不明白如何通过api从移动应用程序中通过websocket接收数据以保存数据