我正在尝试使用Spring websockets建立私人一对一聊天。我正在尝试使用SimpMessagingTemplate.convertAndSendToUser,但它没有将消息中继给用户。我可以建立连接,并且我的addActiveUser端点工作正常,但是SimpMessagingTemplate.convertAndSendToUser不会将我的消息发送回客户端。
谁能看到我想念的东西?这是我的代码。 注意:如果有帮助,我的客户代码正在使用angular 7
服务器端 pom.xml
fn main() {
let a = [10, 20, 30, 40, 50];
let mut index = 0;
let mut sum = 0;
for element in a.iter() {
index += 1;
sum = sum + a[index];
}
println!("the total sum of array is: {}", sum);
}
WebSocketConfig
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gibsams</groupId>
<artifactId>wschatapplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WSChatApplication</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!-- For Working with Json Web Tokens (JWT) -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
ChatController
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/socket")
.setAllowedOrigins("*")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/chat", "/queue");
registry.setUserDestinationPrefix("/user");
}
}
ChatMessage
@Controller
@RequestMapping("/api/chat")
public class ChatController {
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;
@MessageMapping("/send/message")
public void sendMessage(@Payload ChatMessage chatMessage,
SimpMessageHeaderAccessor headerAccessor
) throws Exception {
simpMessagingTemplate.convertAndSendToUser(chatMessage.getRecipient(), "/queue/reply", chatMessage);
}
@MessageMapping("/send/addActiveUser")
@SendTo("/chat/")
public ChatMessage addActiveUser(@Payload ChatMessage chatMessage,
SimpMessageHeaderAccessor headerAccessor
) {
String sender = chatMessage.getSender();
headerAccessor.getSessionAttributes().put("username", sender);
return chatMessage;
}
}
客户端
聊天服务
public class ChatMessage {
private String sender;
private String recipient;
private String content;
public ChatMessage() {}
public ChatMessage(String sender, String recipient, String content) {
this.sender = sender;
this.recipient = recipient;
this.content = content;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}