package com.ydtech.modules.admin.controller.websocket; import cn.hutool.core.bean.BeanUtil; import com.alibaba.fastjson.JSONObject; import com.ydtech.config.SpringContextUtils; import com.ydtech.modules.admin.model.SysGeographyPosition; import com.ydtech.modules.admin.model.SysGeographyPositionRecord; import com.ydtech.modules.admin.model.SysUser; import com.ydtech.modules.admin.service.SysGeographyPositionRecordService; import com.ydtech.modules.admin.service.SysGeographyPositionService; import com.ydtech.modules.admin.service.SysUserService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.util.Date; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; @Slf4j @ServerEndpoint(value = "/websocket/{userId}") @Component public class WebSocketServer { private String userId; private Session session; private static Map sessionClientMap = new ConcurrentHashMap<>(); /**静态变量,用来记录当前在线连接数*/ private static final AtomicInteger onlineCount = new AtomicInteger(0); /** * 连接成功调用的方法 * @param userId * @param session */ @OnOpen public void onOpen(@PathParam("userId") String userId, Session session) { this.session=session; this.userId=userId; if (sessionClientMap.containsKey(userId)) { sessionClientMap.remove(userId); // 加入map中 sessionClientMap.put(userId, session); } else { // 加入map中 sessionClientMap.put(userId, session); // 在线数加1 onlineCount.incrementAndGet(); } log.info("用户连接:" + userId + ",当前在线人数为:" + onlineCount); sendMessage("连接成功"); } @OnMessage public void onMessage(String message, Session session) { JSONObject jsonObject = JSONObject.parseObject(message); String userId = jsonObject.getString("userId"); String longitude = jsonObject.getString("longitude"); String latitude = jsonObject.getString("latitude"); String address = jsonObject.getString("address"); saveLocationData(userId, latitude, longitude,address); log.info("服务端收到消息:{} ,主体:{}", message, userId); session.getAsyncRemote().sendText("服务端已收到信息:" + message); } @OnClose public void onClose(@PathParam("userId") String userId, Session session) { sessionClientMap.remove(userId); log.info("断联成功!"); } @OnError public void onError(Throwable throwable) { log.info("websocket异常,错误信息:{}", throwable.getMessage()); } public void sendMessage(String message) { this.session.getAsyncRemote().sendText(message); } public void saveLocationData(String userId, String latitude, String longitude,String address) { SysGeographyPositionRecordService sysGeographyPositionRecordService = SpringContextUtils.getBean(SysGeographyPositionRecordService.class); SysGeographyPositionService sysGeographyPositionService= SpringContextUtils.getBean(SysGeographyPositionService.class); SysGeographyPosition sysGeographyPosition1 = sysGeographyPositionService.getById(userId); if (sysGeographyPosition1 == null) { //不存在新增 SysUserService sysUserService = SpringContextUtils.getBean(SysUserService.class); SysUser sysUser = sysUserService.getById(userId); SysGeographyPosition sysGeographyPosition = new SysGeographyPosition(); sysGeographyPosition.setId(userId); sysGeographyPosition.setUserName(sysUser.getName()); sysGeographyPosition.setLatitude(latitude); sysGeographyPosition.setLongitude(longitude); sysGeographyPosition.setAddress(address); sysGeographyPosition.setCreateTime(new Date()); sysGeographyPosition.setCreateBy(sysUser.getName()); sysGeographyPositionService.save(sysGeographyPosition); SysGeographyPositionRecord sysGeographyPositionRecord = BeanUtil.copyProperties(sysGeographyPosition, SysGeographyPositionRecord.class); sysGeographyPositionRecord.setId(null); sysGeographyPositionRecord.setUserId(sysGeographyPosition.getId()); sysGeographyPositionRecordService.save(sysGeographyPositionRecord); }else { //存在更新 sysGeographyPosition1.setLongitude(longitude); sysGeographyPosition1.setLatitude(latitude); sysGeographyPosition1.setAddress(address); sysGeographyPosition1.setCreateTime(new Date()); sysGeographyPositionService.updateById(sysGeographyPosition1); SysGeographyPositionRecord sysGeographyPositionRecord = BeanUtil.copyProperties(sysGeographyPosition1, SysGeographyPositionRecord.class); sysGeographyPositionRecord.setId(null); sysGeographyPositionRecord.setUserId(sysGeographyPosition1.getId()); sysGeographyPositionRecordService.save(sysGeographyPositionRecord); } } }