|
|
@@ -0,0 +1,141 @@
|
|
|
+package com.ydtech.modules.admin.controller.websocket;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.ydtech.config.SpringContextUtils;
|
|
|
+import com.ydtech.modules.admin.constants.SystemType;
|
|
|
+import com.ydtech.modules.admin.dao.SysDeptMapper;
|
|
|
+import com.ydtech.modules.admin.model.SysDept;
|
|
|
+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.model.dto.IpInfoDto;
|
|
|
+import com.ydtech.modules.admin.model.po.SysPcAddress;
|
|
|
+import com.ydtech.modules.admin.service.SysGeographyPositionRecordService;
|
|
|
+import com.ydtech.modules.admin.service.SysGeographyPositionService;
|
|
|
+import com.ydtech.modules.admin.service.SysPcAddressService;
|
|
|
+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 = "/pcWebsocket/{userId}")
|
|
|
+@Component
|
|
|
+public class PCSocketServer {
|
|
|
+
|
|
|
+ private String userId;
|
|
|
+
|
|
|
+ private Session session;
|
|
|
+
|
|
|
+ private static Map<String, Session> 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();
|
|
|
+ }
|
|
|
+ editStatus(userId);
|
|
|
+ log.info("PC用户连接:" + 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("lng");
|
|
|
+ String latitude = jsonObject.getString("lat");
|
|
|
+ String province = jsonObject.getString("prov");
|
|
|
+ String city = jsonObject.getString("city");
|
|
|
+ String district = jsonObject.getString("district");
|
|
|
+ String ip = jsonObject.getString("ip");
|
|
|
+ String systemType = jsonObject.getString("systemType");
|
|
|
+ saveLocationData(userId, latitude, longitude,province,city,district,ip,systemType);
|
|
|
+ log.info("服务端收到PC用户消息:{} ,主体:{}", message, userId);
|
|
|
+ sendMessage("服务端已收到PC用户信息:" +message);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @OnClose
|
|
|
+ public void onClose(@PathParam("userId") String userId, Session session) {
|
|
|
+ delStatus(userId);
|
|
|
+ sessionClientMap.remove(userId);
|
|
|
+ log.info("PC用户断联成功!");
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnError
|
|
|
+ public void onError(Throwable throwable) {
|
|
|
+ log.info("websocket异常,错误信息:{}", throwable.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void delStatus(String userId) {
|
|
|
+ SysPcAddressService sysPcAddressService= SpringContextUtils.getBean(SysPcAddressService.class);
|
|
|
+ SysPcAddress sysPcAddress = sysPcAddressService.getById(userId);
|
|
|
+ if (sysPcAddress != null) {
|
|
|
+ sysPcAddress.setStatus(0);
|
|
|
+ sysPcAddressService.updateById(sysPcAddress);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void editStatus(String userId) {
|
|
|
+ SysPcAddressService sysPcAddressService= SpringContextUtils.getBean(SysPcAddressService.class);
|
|
|
+ SysPcAddress sysPcAddress = sysPcAddressService.getById(userId);
|
|
|
+ if (sysPcAddress != null) {
|
|
|
+ sysPcAddress.setStatus(1);
|
|
|
+ sysPcAddressService.updateById(sysPcAddress);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void sendMessage(String message) {
|
|
|
+ this.session.getAsyncRemote().sendText(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void saveLocationData(String userId, String latitude, String longitude,
|
|
|
+ String province,String city,String district,
|
|
|
+ String ip,String systemType) {
|
|
|
+ SysPcAddressService sysPcAddressService = SpringContextUtils.getBean(SysPcAddressService.class);
|
|
|
+ SysUserService sysUserService = SpringContextUtils.getBean(SysUserService.class);
|
|
|
+ if (SystemType.ST_1.getCode().equals(systemType)) {
|
|
|
+ try {
|
|
|
+ IpInfoDto ipInfoDto = new IpInfoDto();
|
|
|
+ ipInfoDto.setIp(ip);
|
|
|
+ ipInfoDto.setLng(longitude);
|
|
|
+ ipInfoDto.setLat(latitude);
|
|
|
+ ipInfoDto.setProv(province);
|
|
|
+ ipInfoDto.setCity(city);
|
|
|
+ ipInfoDto.setDistrict(district);
|
|
|
+
|
|
|
+ SysUser user = sysUserService.getById(userId);
|
|
|
+ // 保存登录信息
|
|
|
+ sysPcAddressService.saveSysPcAddress(ipInfoDto,user);
|
|
|
+ }catch (Exception e){}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|