@Component
@ServerEndpoint(value = "/webSocket/time/out/{doctorCode}")
public class TimeOutWebSocketServer {
/**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
private static int onlineCount = 0;
/**concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。*/
private static ConcurrentHashMap<String, TimeOutWebSocketServer> webSocketMap = new ConcurrentHashMap<>();
/**与某个客户端的连接会话,需要通过它来给客户端发送数据 phone-Session*/
private static ConcurrentHashMap<String, Session> sessionDoctorMap = new ConcurrentHashMap<>();
/**
*功能描述 连接时执行
* @author zhaozm
* @date 2022/11/9
* @param session:
* @return void
*/
@OnOpen
public void onOpen(@PathParam(value = "doctorCode") String doctorCode, Session session){
log.info("打开websocket连接");
if(sessionDoctorMap.containsKey(doctorCode)){
sessionDoctorMap.remove(doctorCode);
sessionDoctorMap.put(doctorCode, session);
} else {
sessionDoctorMap.put(doctorCode, session);
}
if (webSocketMap.containsKey(doctorCode)){
webSocketMap.remove(doctorCode);
webSocketMap.put(doctorCode, this);
} else {
webSocketMap.put(doctorCode, this);
//在线数加1
addOnlineCount();
}
log.info("用户连接:" + session.getId() + ",当前在线人数为:" + getOnlineCount());
}
/**
*功能描述 收到消息时执行
* @author zhaozm
* @date 2022/11/9
* @param message:
* @return void
*/
@OnMessage
public void onMessage(@PathParam(value = "doctorCode") String doctorCode, String message) {
try {
log.info("收到{}的消息:{}", doctorCode, message);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*功能描述 关闭时执行
* @author zhaozm
* @date 2022/11/9
* @param
* @return void
*/
@OnClose
public void onClose(@PathParam(value = "doctorCode") String doctorCode){
//医生端
if (sessionDoctorMap.containsKey(doctorCode)){
sessionDoctorMap.remove(doctorCode);
}
if(webSocketMap.containsKey(doctorCode)){
webSocketMap.remove(doctorCode);
//从set中删除
subOnlineCount();
}
log.info("用户退出:" + doctorCode + ",当前在线人数为:" + getOnlineCount());
}
/**
*功能描述 连接错误时执行
* @author zhaozm
* @date 2022/11/9
* @param doctorCode:
* @param error:
* @return void
*/
@OnError
public void onError(@PathParam(value = "doctorCode") String doctorCode, Throwable error){
log.error("用户错误:{},原因-Exception:{}", doctorCode, error.getMessage());
//医生端
if (sessionDoctorMap.containsKey(doctorCode)){
sessionDoctorMap.remove(doctorCode);
}
if(webSocketMap.containsKey(doctorCode)){
webSocketMap.remove(doctorCode);
//从set中删除
subOnlineCount();
}
}
/**
* 实现服务器主动推送
*/
public void sendMessage(Session session,String message) throws IOException {
if (session != null){
synchronized (this){
session.getBasicRemote().sendText(message);
}
} else {
log.error("发送session不能为空");
}
}
/**
* 功能描述 发送消息
* @author zhaozm
* @date 2022/11/9
* @param doctorCode:
* @param message:
* @return void
*/
public static void sendSelfMessage(String doctorCode, String message){
if (StringUtils.isEmpty(message) || StringUtils.isEmpty(doctorCode)){
return;
}
System.out.println("doctorCode:"+doctorCode);
System.out.println("message:"+message);
try {
if (webSocketMap.containsKey(doctorCode) && sessionDoctorMap.containsKey(doctorCode)){
webSocketMap.get(doctorCode).sendMessage(sessionDoctorMap.get(doctorCode), message);
}
} catch (IOException e) {
log.error("给指定用户发时,用户{}出现错误,错误信息是:{}!", doctorCode, e.getMessage());
}
}
/**
* 功能描述 发送消息
* @author zhaozm
* @date 2022/11/9
* @return void
*/
public static void sendSelfMessage(){
String doctorCode = null;
try {
Enumeration enu = webSocketMap.keys();
while (enu.hasMoreElements()) {
doctorCode = enu.nextElement().toString();
if (webSocketMap.containsKey(doctorCode) && sessionDoctorMap.containsKey(doctorCode)){
//获取该token的提示信息
String message = "";
webSocketMap.get(doctorCode).sendMessage(sessionDoctorMap.get(doctorCode), message);
}
}
} catch (IOException e) {
log.error("给指定用户发时,用户{}出现错误,错误信息是:{}!", doctorCode, e.getMessage());
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
TimeOutWebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
TimeOutWebSocketServer.onlineCount--;
}
}
webSocket模板
最新推荐文章于 2025-04-02 19:15:55 发布