AsyncWebScocketMsgSend.java
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.glsx.usercenter.component;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.websocket.EncodeException;
import javax.websocket.Session;
import cn.hutool.json.JSONUtil;
import org.springframework.data.redis.connection.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* @author zhanghj
* @version 1.0.0
* @Title AsyncWebScocketMsgSend.java
* @Package com.glsx.usercenter.component
* @Description TODO 异步websocke消息发送
* @date 2019年8月16日 下午4:11:55
*/
@Slf4j
@Component
public class AsyncWebScocketMsgSend {
/**
* 群发自定义消息
*
* @throws EncodeException
*/
@Async
public void sendInfo(JSONArray orderList) {
ConcurrentHashMap<Integer, Session> webSocketSet = MyWebSocket.webSocketSet;
for (Map.Entry<Integer, Session> param : webSocketSet.entrySet()) {
for (int i = 0; i < orderList.size(); i++) {
try {
JSONObject orderObj = orderList.getJSONObject(i);
Integer managerId = orderObj.getInteger("managerId");
if (param.getKey().equals(managerId)) {
Session session = param.getValue();
new MyWebSocket().sendMessage(orderObj, session);
}
} catch (Exception e) {
log.error("发送websocket消息异常" + e.getMessage(), e);
}
}
}
}
/**
* 发送消息
*
* @param message 发送的消息
*/
@Async
public void sendMessage(Message message) {
log.info("开始调用sendMessage:" + message.toString());
ConcurrentHashMap<Integer, Session> webSocketSet = MyWebSocket.webSocketSet;
log.info("webSocketSet:" + JSONUtil.toJsonStr(webSocketSet));
for (Map.Entry<Integer, Session> param : webSocketSet.entrySet()) {
try {
log.info("开始调用websocket:" + message);
// // 获取用户Id 通过用户id判断是否是管理员角色 管理员角色才会推送数据到浏览器
Integer userId = param.getKey();
log.info("sendMessage userId===" + userId);
Session session = param.getValue();
new MyWebSocket().sendMessage(message.toString(), session);
} catch (Exception e) {
log.error("发送websocket消息异常" + e.getMessage(), e);
}
}
}
}