提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.websocket.core.sender.rabbitmq; |
H |
2 |
|
|
3 |
import com.iailab.framework.websocket.core.sender.AbstractWebSocketMessageSender; |
|
4 |
import com.iailab.framework.websocket.core.sender.WebSocketMessageSender; |
|
5 |
import com.iailab.framework.websocket.core.session.WebSocketSessionManager; |
|
6 |
import lombok.extern.slf4j.Slf4j; |
|
7 |
import org.springframework.amqp.core.TopicExchange; |
|
8 |
import org.springframework.amqp.rabbit.core.RabbitTemplate; |
|
9 |
|
|
10 |
/** |
|
11 |
* 基于 RabbitMQ 的 {@link WebSocketMessageSender} 实现类 |
|
12 |
* |
|
13 |
* @author iailab |
|
14 |
*/ |
|
15 |
@Slf4j |
|
16 |
public class RabbitMQWebSocketMessageSender extends AbstractWebSocketMessageSender { |
|
17 |
|
|
18 |
private final RabbitTemplate rabbitTemplate; |
|
19 |
|
|
20 |
private final TopicExchange topicExchange; |
|
21 |
|
|
22 |
public RabbitMQWebSocketMessageSender(WebSocketSessionManager sessionManager, |
|
23 |
RabbitTemplate rabbitTemplate, |
|
24 |
TopicExchange topicExchange) { |
|
25 |
super(sessionManager); |
|
26 |
this.rabbitTemplate = rabbitTemplate; |
|
27 |
this.topicExchange = topicExchange; |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public void send(Integer userType, Long userId, String messageType, String messageContent) { |
|
32 |
sendRabbitMQMessage(null, userId, userType, messageType, messageContent); |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public void send(Integer userType, String messageType, String messageContent) { |
|
37 |
sendRabbitMQMessage(null, null, userType, messageType, messageContent); |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public void send(String sessionId, String messageType, String messageContent) { |
|
42 |
sendRabbitMQMessage(sessionId, null, null, messageType, messageContent); |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* 通过 RabbitMQ 广播消息 |
|
47 |
* |
|
48 |
* @param sessionId Session 编号 |
|
49 |
* @param userId 用户编号 |
|
50 |
* @param userType 用户类型 |
|
51 |
* @param messageType 消息类型 |
|
52 |
* @param messageContent 消息内容 |
|
53 |
*/ |
|
54 |
private void sendRabbitMQMessage(String sessionId, Long userId, Integer userType, |
|
55 |
String messageType, String messageContent) { |
|
56 |
RabbitMQWebSocketMessage mqMessage = new RabbitMQWebSocketMessage() |
|
57 |
.setSessionId(sessionId).setUserId(userId).setUserType(userType) |
|
58 |
.setMessageType(messageType).setMessageContent(messageContent); |
|
59 |
rabbitTemplate.convertAndSend(topicExchange.getName(), null, mqMessage); |
|
60 |
} |
|
61 |
|
|
62 |
} |