package com.casic.missiles.utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @Description: Websocket ServerEndpoint
* @Author: wangpeng
* @Date: 2023/2/23 11:48
*/
@Slf4j
@Component
@ServerEndpoint("/websocket/{userId}")
public class WebSocket {
private Session session;
private static CopyOnWriteArraySet<WebSocket> webSockets = new CopyOnWriteArraySet<>();
private static Map<String, Session> sessionPool = new HashMap<>();
@OnOpen
public void onOpen(Session session, @PathParam(value = "userId") String userId) {
this.session = session;
webSockets.add(this);
sessionPool.put(userId, session);
log.info(userId + "【websocket消息】有新的连接,总数为:" + webSockets.size());
// System.out.println(userId + "【websocket消息】有新的连接,总数为:" + webSockets.size());
}
@OnClose
public void onClose() {
webSockets.remove(this);
log.info("【websocket消息】连接断开,总数为:" + webSockets.size());
// System.out.println("【websocket消息】连接断开,总数为:" + webSockets.size());
}
@OnMessage
public void onMessage(String message) {
log.info("【websocket消息】收到客户端消息:" + message);
// System.out.println("【websocket消息】收到客户端消息:" + message);
}
// 此为广播消息
public void sendAllMessage(String message) {
for (WebSocket webSocket : webSockets) {
log.info("【websocket消息】广播消息:" + message);
// System.out.println("【websocket消息】广播消息:" + message);
try {
webSocket.session.getAsyncRemote().sendText(message);
} catch (Exception e) {
log.error("【websocket消息】广播消息发送异常");
e.printStackTrace();
}
}
}
// 发送列表消息
public void sendListMessage(List<String> userIds, String message) {
log.info("【websocket消息】列表消息:" + message);
// System.out.println("【websocket消息】列表消息:" + message);
for (String userId : userIds) {
Session session = sessionPool.get(userId);
if (session != null) {
try {
session.getAsyncRemote().sendText(message);
} catch (Exception e) {
log.error("【websocket消息】列表消息发送异常");
e.printStackTrace();
}
}
}
}
// 此为单点消息
public void sendOneMessage(String userId, String message) {
log.info("【websocket消息】单点消息:" + message);
// System.out.println("【websocket消息】单点消息:" + message);
Session session = sessionPool.get(userId);
if (session != null) {
try {
session.getAsyncRemote().sendText(message);
} catch (Exception e) {
log.error("【websocket消息】单点消息发送异常");
e.printStackTrace();
}
}
}
// 向不同用户发送不同消息
// public void sendMessage(List<WorkbenchRemindMessage> list) {
// for (WorkbenchRemindMessage message : list) {
// Session session = sessionPool.get(String.valueOf(message.getRemindId()));
// if (session != null) {
// try {
// session.getAsyncRemote().sendText(JSONObject.toJSONString(message));
// } catch (Exception e) {
// log.error("【websocket消息】列表消息发送异常");
// e.printStackTrace();
// }
// }
// }
// }
}