WebSocketServer.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.ydtech.modules.admin.controller.websocket;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.ydtech.config.SpringContextUtils;
  5. import com.ydtech.modules.admin.model.SysGeographyPosition;
  6. import com.ydtech.modules.admin.model.SysGeographyPositionRecord;
  7. import com.ydtech.modules.admin.model.SysUser;
  8. import com.ydtech.modules.admin.service.SysGeographyPositionRecordService;
  9. import com.ydtech.modules.admin.service.SysGeographyPositionService;
  10. import com.ydtech.modules.admin.service.SysUserService;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.stereotype.Component;
  13. import javax.websocket.*;
  14. import javax.websocket.server.PathParam;
  15. import javax.websocket.server.ServerEndpoint;
  16. import java.util.Date;
  17. import java.util.Map;
  18. import java.util.concurrent.ConcurrentHashMap;
  19. import java.util.concurrent.atomic.AtomicInteger;
  20. @Slf4j
  21. @ServerEndpoint(value = "/websocket/{userId}")
  22. @Component
  23. public class WebSocketServer {
  24. private String userId;
  25. private Session session;
  26. private static Map<String, Session> sessionClientMap = new ConcurrentHashMap<>();
  27. /**静态变量,用来记录当前在线连接数*/
  28. private static final AtomicInteger onlineCount = new AtomicInteger(0);
  29. /**
  30. * 连接成功调用的方法
  31. * @param userId
  32. * @param session
  33. */
  34. @OnOpen
  35. public void onOpen(@PathParam("userId") String userId, Session session) {
  36. this.session=session;
  37. this.userId=userId;
  38. if (sessionClientMap.containsKey(userId)) {
  39. sessionClientMap.remove(userId);
  40. // 加入map中
  41. sessionClientMap.put(userId, session);
  42. } else {
  43. // 加入map中
  44. sessionClientMap.put(userId, session);
  45. // 在线数加1
  46. onlineCount.incrementAndGet();
  47. }
  48. log.info("用户连接:" + userId + ",当前在线人数为:" + onlineCount);
  49. sendMessage("连接成功");
  50. }
  51. @OnMessage
  52. public void onMessage(String message, Session session) {
  53. JSONObject jsonObject = JSONObject.parseObject(message);
  54. String userId = jsonObject.getString("userId");
  55. String longitude = jsonObject.getString("longitude");
  56. String latitude = jsonObject.getString("latitude");
  57. String address = jsonObject.getString("address");
  58. saveLocationData(userId, latitude, longitude,address);
  59. log.info("服务端收到消息:{} ,主体:{}", message, userId);
  60. session.getAsyncRemote().sendText("服务端已收到信息:" + message);
  61. }
  62. @OnClose
  63. public void onClose(@PathParam("userId") String userId, Session session) {
  64. sessionClientMap.remove(userId);
  65. log.info("断联成功!");
  66. }
  67. @OnError
  68. public void onError(Throwable throwable) {
  69. log.info("websocket异常,错误信息:{}", throwable.getMessage());
  70. }
  71. public void sendMessage(String message) {
  72. this.session.getAsyncRemote().sendText(message);
  73. }
  74. public void saveLocationData(String userId, String latitude, String longitude,String address) {
  75. SysGeographyPositionRecordService sysGeographyPositionRecordService = SpringContextUtils.getBean(SysGeographyPositionRecordService.class);
  76. SysGeographyPositionService sysGeographyPositionService= SpringContextUtils.getBean(SysGeographyPositionService.class);
  77. SysGeographyPosition sysGeographyPosition1 = sysGeographyPositionService.getById(userId);
  78. if (sysGeographyPosition1 == null) {
  79. //不存在新增
  80. SysUserService sysUserService = SpringContextUtils.getBean(SysUserService.class);
  81. SysUser sysUser = sysUserService.getById(userId);
  82. SysGeographyPosition sysGeographyPosition = new SysGeographyPosition();
  83. sysGeographyPosition.setId(userId);
  84. sysGeographyPosition.setUserName(sysUser.getName());
  85. sysGeographyPosition.setLatitude(latitude);
  86. sysGeographyPosition.setLongitude(longitude);
  87. sysGeographyPosition.setAddress(address);
  88. sysGeographyPosition.setCreateTime(new Date());
  89. sysGeographyPosition.setCreateBy(sysUser.getName());
  90. sysGeographyPositionService.save(sysGeographyPosition);
  91. SysGeographyPositionRecord sysGeographyPositionRecord = BeanUtil.copyProperties(sysGeographyPosition, SysGeographyPositionRecord.class);
  92. sysGeographyPositionRecord.setId(null);
  93. sysGeographyPositionRecord.setUserId(sysGeographyPosition.getId());
  94. sysGeographyPositionRecordService.save(sysGeographyPositionRecord);
  95. }else {
  96. //存在更新
  97. sysGeographyPosition1.setLongitude(longitude);
  98. sysGeographyPosition1.setLatitude(latitude);
  99. sysGeographyPosition1.setAddress(address);
  100. sysGeographyPosition1.setCreateTime(new Date());
  101. sysGeographyPositionService.updateById(sysGeographyPosition1);
  102. SysGeographyPositionRecord sysGeographyPositionRecord = BeanUtil.copyProperties(sysGeographyPosition1, SysGeographyPositionRecord.class);
  103. sysGeographyPositionRecord.setId(null);
  104. sysGeographyPositionRecord.setUserId(sysGeographyPosition1.getId());
  105. sysGeographyPositionRecordService.save(sysGeographyPositionRecord);
  106. }
  107. }
  108. }