EcEntrustAttentionHandler.java
3.62 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.cjs.site.biz.ec;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import com.cjs.site.dao.user.interact.UserAttentionDao;
import com.cjs.site.model.entrust.EcEntrustInfo;
import com.cjs.site.model.user.UserAttentionInfo;
import com.cjs.site.util.lang.JsonUtil;
import com.cjs.site.util.redis.JedisTemplate;
/**
* 官网买卖,用户关注藏品
*
* @author tongyufu
*
*/
@Service
public class EcEntrustAttentionHandler extends TextWebSocketHandler {
/**官网买卖,用户关注藏品查询*/
public static Map<WebSocketSession, List<UserAttentionInfo>> WEBSOCKET_SESSIONS = new HashMap<WebSocketSession, List<UserAttentionInfo>>();
Logger log = LogManager
.getLogger();
@Autowired
private JedisTemplate jedisTemplate;
@Autowired
private UserAttentionDao userAttentionDao;
@Autowired
private EcEntrustInfoBiz ecEntrustInfoBiz;
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
super.afterConnectionEstablished(session);
}
@SuppressWarnings("unchecked")
@Override
protected void handleTextMessage(WebSocketSession session,
TextMessage message) throws Exception {
super.handleTextMessage(session, message);
try {
String userId = message.getPayload();
List<UserAttentionInfo> attentions = null;
if (StringUtils.isBlank(userId)) {
return;
}
attentions = userAttentionDao.queryByUserId(userId);
WEBSOCKET_SESSIONS.put(session, attentions);
//连接时先推送一次
String payload = jedisTemplate.get(EcEntrustAllHandler.ENTRUST_ENTRUSTING);
if (StringUtils.isNotBlank(payload)) {
if (payload.indexOf("[") != -1) {
payload = payload.substring(payload.indexOf("["));
}
List<EcEntrustInfo> entrusts = JsonUtil.fromJson(payload, ArrayList.class,
EcEntrustInfo.class);
ecEntrustInfoBiz.sendToClient(session, entrusts, attentions);
}
} catch (Exception e) {
log.error("官网买卖,用户关注藏品连接出错。", e);
throw e;
}
}
@Override
public void afterConnectionClosed(WebSocketSession session,
CloseStatus status) throws Exception {
super.afterConnectionClosed(session, status);
WEBSOCKET_SESSIONS.remove(session);
}
@Override
public void handleTransportError(WebSocketSession session,
Throwable exception) throws Exception {
super.handleTransportError(session, exception);
log.error("WebScoket传输出错[" + session.getId() + "]:" + exception.getMessage());
if (session.isOpen()) {
session.close();
}
WEBSOCKET_SESSIONS.remove(session);
}
}