webSocket应用
1.依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2.webSocketConfig
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
3.SocketManager
@Service
public class WebSocketManager {
@Resource
private RedisTemplate<String, List<Session>> redisTemplate ;
public static Long onLineCount = 0L;
private static final String SESSION_KEY = "SESSIONKEY";
public void addSession(Long userId, String appCode, Session session) {
HashOperations<String, String, List<Session>> clients = redisTemplate.opsForHash();
String key = StringUtils.join(appCode, ",", userId.toString());
boolean hasKey = clients.hasKey(SESSION_KEY, key);
if (hasKey) {
List<Session> sessions = clients.get(SESSION_KEY, key);
if (CollectionUtils.isEmpty(sessions)) {
List<Session> sessionList = new ArrayList<>();
sessionList.add(session);
clients.put(SESSION_KEY, key, sessionList);
} else {
sessions.add(session);
clients.put(SESSION_KEY, key, sessions);
}
} else {
List<Session> sessionList = new ArrayList<>();
sessionList.add(session);
clients.put(SESSION_KEY, key, sessionList);
}
onLineCount++;
}
public void deleteSession(Long userId, String appCode, Session session) {
String key = StringUtils.join(appCode, ",", userId.toString());
HashOperations<String, String, List<Session>> clients = redisTemplate.opsForHash();
if (null == clients) {
return;
}
boolean hasKey = redisTemplate.hasKey(key);
if (hasKey) {
List<Session> sessions = clients.get(SESSION_KEY, key);
if (CollectionUtils.isEmpty(sessions)) {
clients.delete(SESSION_KEY, key);
} else {
if (sessions.contains(session)) {
if (1 == sessions.size()) {
clients.delete(SESSION_KEY, key);
}
sessions.remove(session);
clients.put(SESSION_KEY, key, sessions);
}
}
}
onLineCount--;
}
public Map<String, List<Session>> getClients() {
HashOperations<String, String, List<Session>> clients = redisTemplate.opsForHash();
Map<String, List<Session>> lists = clients.entries(SESSION_KEY);
return lists;
}
public List<Session> getSession(Long userId, String appCode) {
String key = StringUtils.join(appCode, ",", userId.toString());
HashOperations<String, String, List<Session>> clients = redisTemplate.opsForHash();
return clients.get(SESSION_KEY, key);
}
public List<Session> getAllSession() {
HashOperations<String, String, List<Session>> clients = redisTemplate.opsForHash();
List<Session> allSession = new ArrayList<>();
List<List<Session>> lists = clients.values(SESSION_KEY);
for (List<Session> list : lists) {
allSession.addAll(list);
}
return allSession;
}
}
4.SocketWeb
@ServerEndpoint(value = "/message/{code1}/{code2}")
@Component
public class SocketWeb{
@OnOpen
public void onOpen(@PathParam("code1") String code1,
@PathParam("code2") String code2, Session session) {
//socket 连接触发
}
@OnMessage
public void OnMessage(@PathParam("code1") String code1,
@PathParam("code2") String code2, Session session, String message) {
//socket和客户端交互触发
}
@OnClose
public void OnClose(@PathParam("code1") String code1,
@PathParam("code2") String code2, Session session) {
//socket关闭触发
}
@OnError
public void OnError(Throwable throwable, Session session) {
//异常触发
LOG.error(throwable.getMessage(), throwable);
}
}
4.webSocket 测试工具
http://www.blue-zero.com/WebSocket/