Compare commits
2 Commits
branch_fir
...
master
Author | SHA1 | Date | |
---|---|---|---|
7e46ef5b57 | |||
3360613347 |
@ -83,10 +83,6 @@ public class CustomAuthenticationProvider implements AuthenticationProvider {
|
||||
throw new DisabledException("用户状态不正常,请联系管理员");
|
||||
}
|
||||
|
||||
if (DateTimeUtil.dateTimeToString(LocalDateTime.now()).compareTo(userEntity.getOverTime()) > 0) {
|
||||
throw new DisabledException("该账号已过期,请联系管理员");
|
||||
}
|
||||
|
||||
// 认证逻辑
|
||||
String encryptPassword = encry256(password + userEntity.getSalt());
|
||||
if (encryptPassword.equals(userEntity.getPassword())) {
|
||||
|
@ -0,0 +1,25 @@
|
||||
package com.xkrs.common.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.messaging.MessageSecurityMetadataSourceRegistry;
|
||||
import org.springframework.security.config.annotation.web.socket.AbstractSecurityWebSocketMessageBrokerConfigurer;
|
||||
|
||||
import static org.springframework.messaging.simp.SimpMessageType.MESSAGE;
|
||||
import static org.springframework.messaging.simp.SimpMessageType.SUBSCRIBE;
|
||||
|
||||
|
||||
/**
|
||||
* @author xkrs
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketSecurityConfig extends AbstractSecurityWebSocketMessageBrokerConfigurer {
|
||||
@Override
|
||||
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
|
||||
messages
|
||||
.nullDestMatcher().authenticated()
|
||||
.simpSubscribeDestMatchers("/user/queue/errors").permitAll()
|
||||
.simpTypeMatchers(MESSAGE, SUBSCRIBE).denyAll()
|
||||
.anyMessage().denyAll();
|
||||
|
||||
}
|
||||
}
|
@ -44,7 +44,7 @@ public class FirePointQo {
|
||||
/**
|
||||
* 植被类型
|
||||
*/
|
||||
private String landtype;
|
||||
private String landType;
|
||||
|
||||
/**
|
||||
* 置信度
|
||||
@ -64,7 +64,7 @@ public class FirePointQo {
|
||||
/**
|
||||
* 乡镇街道区划编码
|
||||
*/
|
||||
private String towncode;
|
||||
private String townCode;
|
||||
|
||||
public FirePointQo() {
|
||||
}
|
||||
@ -125,12 +125,12 @@ public class FirePointQo {
|
||||
this.satelliteType = satelliteType;
|
||||
}
|
||||
|
||||
public String getLandtype() {
|
||||
return landtype;
|
||||
public String getLandType() {
|
||||
return landType;
|
||||
}
|
||||
|
||||
public void setLandtype(String landtype) {
|
||||
this.landtype = landtype;
|
||||
public void setLandType(String landType) {
|
||||
this.landType = landType;
|
||||
}
|
||||
|
||||
public String getConfidence() {
|
||||
@ -157,16 +157,29 @@ public class FirePointQo {
|
||||
this.satelliteImage = satelliteImage;
|
||||
}
|
||||
|
||||
public String getTowncode() {
|
||||
return towncode;
|
||||
public String getTownCode() {
|
||||
return townCode;
|
||||
}
|
||||
|
||||
public void setTowncode(String towncode) {
|
||||
this.towncode = towncode;
|
||||
public void setTownCode(String townCode) {
|
||||
this.townCode = townCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FirePointQo{" + "fireCode='" + fireCode + '\'' + ", countyCode=" + countyCode + ", countyName='" + countyName + '\'' + ", satelliteTimeTs=" + satelliteTimeTs + ", longitude=" + longitude + ", latitude=" + latitude + ", satelliteType='" + satelliteType + '\'' + ", landtype='" + landtype + '\'' + ", confidence='" + confidence + '\'' + ", fireImage='" + fireImage + '\'' + ", satelliteImage='" + satelliteImage + '\'' + ", towncode='" + towncode + '\'' + '}';
|
||||
return "FirePointQo{" +
|
||||
"fireCode='" + fireCode + '\'' +
|
||||
", countyCode=" + countyCode +
|
||||
", countyName='" + countyName + '\'' +
|
||||
", satelliteTimeTs=" + satelliteTimeTs +
|
||||
", longitude=" + longitude +
|
||||
", latitude=" + latitude +
|
||||
", satelliteType='" + satelliteType + '\'' +
|
||||
", landType='" + landType + '\'' +
|
||||
", confidence='" + confidence + '\'' +
|
||||
", fireImage='" + fireImage + '\'' +
|
||||
", satelliteImage='" + satelliteImage + '\'' +
|
||||
", townCode='" + townCode + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -90,15 +90,14 @@ public class FirePointServiceImpl implements FirePointService {
|
||||
@CacheEvict(value = "FirePointServiceCache", allEntries = true)
|
||||
@Override
|
||||
public boolean insertFirePoint(FirePointQo firePointQo) {
|
||||
if ("水体".equals(firePointQo.getLandtype())) {
|
||||
if ("水体".equals(firePointQo.getLandType())) {
|
||||
return false;
|
||||
}
|
||||
if ("Himawari 8".equals(firePointQo.getSatelliteType())) {
|
||||
if ("L".equals(firePointQo.getConfidence()) || "N".equals(firePointQo.getConfidence())) {
|
||||
if ("N".equals(firePointQo.getConfidence()) || "L".equals(firePointQo.getConfidence())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
FirePointEntity firePointEntity = new FirePointEntity();
|
||||
firePointEntity.setFireCode(firePointQo.getFireCode());
|
||||
firePointEntity.setCountyCode(firePointQo.getCountyCode().toString());
|
||||
@ -107,7 +106,7 @@ public class FirePointServiceImpl implements FirePointService {
|
||||
firePointEntity.setLongitude(firePointQo.getLongitude());
|
||||
firePointEntity.setLatitude(firePointQo.getLatitude());
|
||||
firePointEntity.setSatelliteType(firePointQo.getSatelliteType());
|
||||
firePointEntity.setLandType(firePointQo.getLandtype());
|
||||
firePointEntity.setLandType(firePointQo.getLandType());
|
||||
firePointEntity.setConfidence(firePointQo.getConfidence());
|
||||
firePointEntity.setAddTime(dateTimeToString(LocalDateTime.now()));
|
||||
firePointEntity.setFireType("0");
|
||||
@ -121,8 +120,6 @@ public class FirePointServiceImpl implements FirePointService {
|
||||
}
|
||||
log.info("-------发现新火点");
|
||||
firePointDao.save(firePointEntity);
|
||||
//发送消息通知
|
||||
sendBroadcast(firePointEntity);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -294,7 +291,18 @@ public class FirePointServiceImpl implements FirePointService {
|
||||
String countyName = firePointEntity.getCountyName();
|
||||
String streetName = firePointEntity.getStreetName();
|
||||
String landType = firePointEntity.getLandType();
|
||||
return "星巡-全国火情播报系统\n" + firePointEntity.getSatelliteType() + "发现1个火点。\n卫星时间:" + satelliteTime + ";\nlongitude:" + formatLongitude + ";\nlatitude:" + formatLatitude + ";\ncountyName:" + countyName + ";\nstreetName:" + streetName + ";\nlandType:" + landType;
|
||||
return firePointEntity.getSatelliteType() + "发现1个火点。\n卫星时间:" + satelliteTime + ";\nlongitude:" + formatLongitude + ";\nlatitude:" + formatLatitude + ";\ncountyName:" + countyName + ";\nstreetName:" + streetName + ";\nlandType:" + landType;
|
||||
}
|
||||
|
||||
private String getMessageContent(ShanDongFirePointEntity firePointEntity) {
|
||||
java.text.DecimalFormat decimalFormat = new DecimalFormat("#.000000");
|
||||
String satelliteTime = firePointEntity.getSatelliteTime();
|
||||
String formatLongitude = decimalFormat.format(firePointEntity.getLongitude());
|
||||
String formatLatitude = decimalFormat.format(firePointEntity.getLatitude());
|
||||
String countyName = firePointEntity.getCountyName();
|
||||
String streetName = firePointEntity.getStreetName();
|
||||
String landType = firePointEntity.getLandType();
|
||||
return firePointEntity.getSatelliteType() + "发现1个待审核火点。\n卫星时间:" + satelliteTime + ";\nlongitude:" + formatLongitude + ";\nlatitude:" + formatLatitude + ";\ncountyName:" + countyName + ";\nstreetName:" + streetName + ";\nlandType:" + landType;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -111,15 +111,15 @@ public class StreetServiceImpl implements StreetService {
|
||||
// RegionBean countyBean = new RegionBean();
|
||||
// countyBean.setCode(county.get("code"));
|
||||
// countyBean.setName(county.get("name"));
|
||||
// List<Map<String, String>> streetList = selectStreetList(county.get("code"));
|
||||
// List<RegionBean> streetBeanList = new ArrayList<>();
|
||||
// for (Map<String, String> street : streetList) {
|
||||
// RegionBean streetBean = new RegionBean();
|
||||
// streetBean.setCode(street.get("code"));
|
||||
// streetBean.setName(street.get("name"));
|
||||
// streetBeanList.add(streetBean);
|
||||
// }
|
||||
// countyBean.setChildren(streetBeanList);
|
||||
//// List<Map<String, String>> streetList = selectStreetList(county.get("name"));
|
||||
//// List<RegionBean> streetBeanList = new ArrayList<>();
|
||||
//// for (Map<String, String> street : streetList) {
|
||||
//// RegionBean streetBean = new RegionBean();
|
||||
//// streetBean.setCode(street.get("code"));
|
||||
//// streetBean.setName(street.get("name"));
|
||||
//// streetBeanList.add(streetBean);
|
||||
//// }
|
||||
//// countyBean.setChildren(streetBeanList);
|
||||
// countyBeanList.add(countyBean);
|
||||
// }
|
||||
// cityBean.setChildren(countyBeanList);
|
||||
|
@ -126,7 +126,6 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
|
||||
String countyCode = sysUserQo.getCountyCode();
|
||||
String countyName = sysUserQo.getCountyName();
|
||||
countyName = adapterCountyNameByCountyCode(countyCode);
|
||||
if (TextUtils.isEmpty(countyCode)) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "省市区街道编码不能为空!", locale);
|
||||
}
|
||||
@ -142,10 +141,8 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
if (streetNameList == null || streetNameList.isEmpty()) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale);
|
||||
}
|
||||
sysUserEntity.setAccountType("街道级");
|
||||
sysUserEntity.setActiveFlag(0);
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(7L)));
|
||||
adapterStreetUser(sysUserEntity);
|
||||
sysUserEntity.setAccountType("街道级");
|
||||
} else if ("0000".equals(countyCode.substring(2))) {
|
||||
List<StreetEntity> proCodeList = streetDao.findByProCode(countyCode);
|
||||
if (proCodeList == null || proCodeList.size() == 0) {
|
||||
@ -155,10 +152,8 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
if (proNameList == null || proNameList.size() == 0) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale);
|
||||
}
|
||||
sysUserEntity.setAccountType("省级");
|
||||
sysUserEntity.setActiveFlag(1);
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(5L)));
|
||||
// adapterProUser(sysUserEntity);
|
||||
sysUserEntity.setAccountType("省级");
|
||||
} else if ("00".equals(countyCode.substring(4)) && !"0000".equals(countyCode.substring(2))) {
|
||||
List<StreetEntity> cityCodeList = streetDao.findByCityCode(countyCode);
|
||||
if (cityCodeList == null || cityCodeList.size() == 0) {
|
||||
@ -168,10 +163,8 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
if (cityNameList == null || cityNameList.size() == 0) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale);
|
||||
}
|
||||
sysUserEntity.setAccountType("市级");
|
||||
sysUserEntity.setActiveFlag(1);
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(5L)));
|
||||
adapterCityUser(sysUserEntity);
|
||||
sysUserEntity.setAccountType("市级");
|
||||
} else {
|
||||
List<StreetEntity> countyCodeList = streetDao.findByCountyCode(countyCode);
|
||||
if (countyCodeList == null || countyCodeList.isEmpty()) {
|
||||
@ -181,10 +174,13 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
if (countyNameList == null || countyNameList.isEmpty()) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale);
|
||||
}
|
||||
sysUserEntity.setAccountType("县级");
|
||||
sysUserEntity.setActiveFlag(0);
|
||||
sysUserEntity.setAccountType("县级");
|
||||
}
|
||||
if (9 == countyCode.length()) {//街道账号7天后过期
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(7L)));
|
||||
} else {//省市区县账号5天后过期
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(5L)));
|
||||
adapterCountyUser(sysUserEntity);
|
||||
}
|
||||
sysUserEntity.setStatusCode(0);
|
||||
sysUserEntity.setAddTime(dateTimeToString(LocalDateTime.now()));
|
||||
@ -213,81 +209,6 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "注册成功!", locale);
|
||||
}
|
||||
|
||||
private String adapterCountyNameByCountyCode(String countyCode) {
|
||||
if (9 == countyCode.length()) {
|
||||
List<StreetEntity> streetCodeList = streetDao.findByStreetCode(countyCode);
|
||||
if (streetCodeList == null || streetCodeList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return streetCodeList.get(0).getStreetName();
|
||||
} else if ("0000".equals(countyCode.substring(2))) {
|
||||
List<StreetEntity> proCodeList = streetDao.findByProCode(countyCode);
|
||||
if (proCodeList == null || proCodeList.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
return proCodeList.get(0).getProName();
|
||||
} else if ("00".equals(countyCode.substring(4)) && !"0000".equals(countyCode.substring(2))) {
|
||||
List<StreetEntity> cityCodeList = streetDao.findByCityCode(countyCode);
|
||||
if (cityCodeList == null || cityCodeList.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
return cityCodeList.get(0).getCityName();
|
||||
} else {
|
||||
List<StreetEntity> countyCodeList = streetDao.findByCountyCode(countyCode);
|
||||
if (countyCodeList == null || countyCodeList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return countyCodeList.get(0).getCountyName();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 适配2023-11-09到2023-11-12之间注册的街道用户
|
||||
*/
|
||||
private void adapterStreetUser(SysUserEntity sysUserEntity) {
|
||||
if (in1109To1112()) {
|
||||
sysUserEntity.setActiveFlag(0);
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(180L)));
|
||||
}
|
||||
}
|
||||
|
||||
// private void adapterProUser(SysUserEntity sysUserEntity) {
|
||||
// if (in1109To1112()) {
|
||||
// sysUserEntity.setActiveFlag(1);
|
||||
// sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(5L)));
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 适配2023-11-09到2023-11-12之间注册的市级用户
|
||||
*/
|
||||
private void adapterCityUser(SysUserEntity sysUserEntity) {
|
||||
if (in1109To1112()) {
|
||||
sysUserEntity.setActiveFlag(0);
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(30L)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 适配2023-11-09到2023-11-12之间注册的区县用户
|
||||
*/
|
||||
private void adapterCountyUser(SysUserEntity sysUserEntity) {
|
||||
if (in1109To1112()) {
|
||||
sysUserEntity.setActiveFlag(0);
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(30L)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前是否处于2023-11-09到2023-11-12之间注册的
|
||||
*/
|
||||
private boolean in1109To1112() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
LocalDateTime start1109 = LocalDateTime.of(2023, 11, 9, 0, 0, 0, 0);
|
||||
LocalDateTime end1112 = LocalDateTime.of(2023, 11, 13, 0, 0, 0, 0);
|
||||
return start1109.isBefore(now) && end1112.isAfter(now);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增用户
|
||||
*
|
||||
@ -326,9 +247,8 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
if (streetNameList == null || streetNameList.isEmpty()) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale);
|
||||
}
|
||||
sysUserEntity.setAccountType("街道级");
|
||||
sysUserEntity.setActiveFlag(0);
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(7L)));
|
||||
sysUserEntity.setAccountType("街道级");
|
||||
} else if ("0000".equals(countyCode.substring(2))) {
|
||||
List<StreetEntity> proCodeList = streetDao.findByProCode(countyCode);
|
||||
if (proCodeList == null || proCodeList.size() == 0) {
|
||||
@ -338,9 +258,8 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
if (proNameList == null || proNameList.size() == 0) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale);
|
||||
}
|
||||
sysUserEntity.setAccountType("省级");
|
||||
sysUserEntity.setActiveFlag(1);
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(5L)));
|
||||
sysUserEntity.setAccountType("省级");
|
||||
} else if ("00".equals(countyCode.substring(4)) && !"0000".equals(countyCode.substring(2))) {
|
||||
List<StreetEntity> cityCodeList = streetDao.findByCityCode(countyCode);
|
||||
if (cityCodeList == null || cityCodeList.size() == 0) {
|
||||
@ -350,9 +269,8 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
if (cityNameList == null || cityNameList.size() == 0) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale);
|
||||
}
|
||||
sysUserEntity.setAccountType("市级");
|
||||
sysUserEntity.setActiveFlag(1);
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(5L)));
|
||||
sysUserEntity.setAccountType("市级");
|
||||
} else {
|
||||
List<StreetEntity> countyCodeList = streetDao.findByCountyCode(countyCode);
|
||||
if (countyCodeList == null || countyCodeList.isEmpty()) {
|
||||
@ -362,8 +280,12 @@ public class SysUserServiceImpl implements SysUserService {
|
||||
if (countyNameList == null || countyNameList.isEmpty()) {
|
||||
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "您所选择的区域不存在!", locale);
|
||||
}
|
||||
sysUserEntity.setAccountType("县级");
|
||||
sysUserEntity.setActiveFlag(0);
|
||||
sysUserEntity.setAccountType("县级");
|
||||
}
|
||||
if (9 == countyCode.length()) {//街道账号7天后过期
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(7L)));
|
||||
} else {//省市区县账号5天后过期
|
||||
sysUserEntity.setOverTime(dateTimeToString(LocalDateTime.now().plusDays(5L)));
|
||||
}
|
||||
sysUserEntity.setStatusCode(0);
|
||||
|
@ -19,9 +19,9 @@ public class AliYunSmsUtils {
|
||||
//产品域名,开发者无需替换
|
||||
String domain = "dysmsapi.aliyuncs.com";
|
||||
//此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
|
||||
String accessKeyId = "LTAI5tMa5Va4CSwFwXxKpKfc";
|
||||
String accessKeyId = "LTAI5tMSjLfu8Xu2w6WeguFF";
|
||||
//修改成自己的
|
||||
String accessKeySecret = "PIUhRNVcApBF3H8TzU8pM1ZI6fjvWQ";
|
||||
String accessKeySecret = "hvqM5zpi72hvX4VXM71wq6AE0XYtDI";
|
||||
//可自助调整超时时间
|
||||
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
||||
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
||||
|
@ -0,0 +1,25 @@
|
||||
package com.xkrs.websocket.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
|
||||
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
|
||||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
|
||||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
|
||||
|
||||
/**
|
||||
* websocket广播配置
|
||||
* @author xkrs
|
||||
*/
|
||||
@Configuration
|
||||
@EnableWebSocketMessageBroker
|
||||
public class WebSocketBrokerConfig implements WebSocketMessageBrokerConfigurer {
|
||||
@Override
|
||||
public void registerStompEndpoints(StompEndpointRegistry registry) {
|
||||
registry.addEndpoint("/endPointClientServer").withSockJS();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||
registry.enableSimpleBroker("/ws");
|
||||
}
|
||||
}
|
16
src/main/java/com/xkrs/websocket/config/WebSocketConfig.java
Normal file
16
src/main/java/com/xkrs/websocket/config/WebSocketConfig.java
Normal file
@ -0,0 +1,16 @@
|
||||
package com.xkrs.websocket.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
/**
|
||||
* @author xkrs
|
||||
*/
|
||||
@Configuration
|
||||
public class WebSocketConfig {
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter(){
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.xkrs.websocket.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author xkrs
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/test")
|
||||
public class TestController {
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String sayHello(){
|
||||
return "asdasd";
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.xkrs.websocket.controller.websocket;
|
||||
|
||||
import com.xkrs.websocket.service.WebSocketServer;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @author xkrs
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/socket")
|
||||
public class WebSocketController {
|
||||
|
||||
@GetMapping("/sendAll")
|
||||
public String sendAll(@RequestParam String msg) {
|
||||
WebSocketServer.broadInfo(msg);
|
||||
return "success";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/sendOne", method = RequestMethod.GET)
|
||||
public String sendOne(@RequestParam String msg, @RequestParam String session) {
|
||||
WebSocketServer.sendMsg(msg, session);
|
||||
return "success";
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.xkrs.websocket.controller.websocketbroker;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
/**
|
||||
* @author xkrs
|
||||
*/
|
||||
@Configuration
|
||||
public class ViewController extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
registry.addViewController("/ws").setViewName("/ws");
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.xkrs.websocket.controller.websocketbroker;
|
||||
|
||||
import com.xkrs.websocket.websocket.ClientServerMessage;
|
||||
import com.xkrs.websocket.websocket.ServerClientMessage;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
/**
|
||||
* @author xkrs
|
||||
*/
|
||||
@Controller
|
||||
public class WebSocketBrokerController {
|
||||
|
||||
@MessageMapping("/hello")
|
||||
@SendTo("/ws/getResponse")
|
||||
public ServerClientMessage say(ClientServerMessage message) throws InterruptedException {
|
||||
Thread.sleep(3000);
|
||||
return new ServerClientMessage("hello," + message.getName() + "!");
|
||||
}
|
||||
}
|
128
src/main/java/com/xkrs/websocket/service/WebSocketServer.java
Normal file
128
src/main/java/com/xkrs/websocket/service/WebSocketServer.java
Normal file
@ -0,0 +1,128 @@
|
||||
package com.xkrs.websocket.service;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.websocket.*;
|
||||
import javax.websocket.server.ServerEndpoint;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @author xkrs
|
||||
*/
|
||||
@Component
|
||||
@ServerEndpoint(value = "/ws/asset")
|
||||
public class WebSocketServer {
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
System.out.println("websocketBroker loading......");
|
||||
}
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(WebSocketServer.class);
|
||||
private static final AtomicInteger ONLINE_COUNT = new AtomicInteger(0);
|
||||
private static CopyOnWriteArraySet<Session> sessionSet = new CopyOnWriteArraySet<>();
|
||||
|
||||
/**
|
||||
* 连接建立
|
||||
*/
|
||||
@OnOpen
|
||||
public void onOpen(Session session) {
|
||||
sessionSet.add(session);
|
||||
//在线加1
|
||||
int cnt = ONLINE_COUNT.incrementAndGet();
|
||||
//log.info("have a new connect:{}", cnt);
|
||||
//sendMsg(session, "connect success");
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接关闭
|
||||
*
|
||||
* @param session
|
||||
*/
|
||||
@OnClose
|
||||
public void onClose(Session session) {
|
||||
sessionSet.remove(session);
|
||||
//在线减1
|
||||
int cnt = ONLINE_COUNT.decrementAndGet();
|
||||
log.info("have a connect close:{}", cnt);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收到客户端消息后调用的方法
|
||||
*
|
||||
* @param session
|
||||
* @param msg
|
||||
*/
|
||||
@OnMessage
|
||||
public void onMessage(String msg, Session session) {
|
||||
log.info("receive the msg:{}", msg);
|
||||
}
|
||||
|
||||
private static void sendMsg(Session session, String msg) {
|
||||
try {
|
||||
session.getBasicRemote().sendText(String.format(msg));
|
||||
} catch (IOException e) {
|
||||
log.error("发送信息出错:{}", e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void sendDate(Session session, Map map) {
|
||||
try {
|
||||
session.getBasicRemote().sendObject(map);
|
||||
} catch (IOException | EncodeException e) {
|
||||
log.error("发送信息出错:{}", e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 指定session发送
|
||||
*/
|
||||
public static void sendMsg(String msg, String sessionId) {
|
||||
Session session = null;
|
||||
for (Session s : sessionSet) {
|
||||
if (s.getId().equals(sessionId)) {
|
||||
session = s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (session != null) {
|
||||
sendMsg(session, msg);
|
||||
} else {
|
||||
log.warn("not find your session");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 群发消息
|
||||
*/
|
||||
public static void broadInfo(String msg) {
|
||||
for (Session s : sessionSet) {
|
||||
synchronized (sessionSet){
|
||||
if (s.isOpen()) {
|
||||
sendMsg(s, msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 群发map消息
|
||||
* @param map
|
||||
*/
|
||||
public static void broadInfoMap(Map map) {
|
||||
for (Session s : sessionSet) {
|
||||
if (s.isOpen()) {
|
||||
sendDate(s, map);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.xkrs.websocket.websocket;
|
||||
|
||||
/**
|
||||
* @author xkrs
|
||||
*/
|
||||
public class ClientServerMessage {
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String name;
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.xkrs.websocket.websocket;
|
||||
|
||||
/**
|
||||
* @author xkrs
|
||||
*/
|
||||
public class ServerClientMessage {
|
||||
|
||||
public String getResponseMsg() {
|
||||
return responseMsg;
|
||||
}
|
||||
|
||||
public void setResponseMsg(String responseMsg) {
|
||||
this.responseMsg = responseMsg;
|
||||
}
|
||||
|
||||
private String responseMsg;
|
||||
|
||||
public ServerClientMessage(String responseMsg) {
|
||||
this.responseMsg = responseMsg;
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
server.port = 6801
|
||||
|
||||
## 数据源配置
|
||||
spring.datasource.url = jdbc:postgresql://8.142.26.238:5432/fire_point_new
|
||||
spring.datasource.url = jdbc:postgresql://118.24.27.47:5432/fire_point
|
||||
spring.datasource.userName = fire_manage
|
||||
spring.datasource.password = fire456
|
||||
spring.datasource.driverClassName = org.postgresql.Driver
|
||||
|
Loading…
Reference in New Issue
Block a user