什么是websocket
websocket协议是基于TCP的一种新的网络协议,实现浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端。
为什么需要websocket?
因为http协议的通信只能由客户端发起,做不到服务器主动向客户端发送消息。
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
创建weosocket的配置类
import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* WebSocketConfig
* @author songxue
* @date 2022-04-08 15:01
*/
public class WebSocketConfig {
/**
* 开启websocket支持
* @author songxue
* @date 2022/4/8 15:01
* @param
* @return
* @other
**/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
创建websocket的文件夹
@ServerEndpoint("/webSocketServer/{token}")
@Component
@RequiredArgsConstructor
public class WebSocketServer {
private WebSocketService webSocketService;
static Logger log = LoggerFactory.getLogger(WebSocketServer.class);
/**静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。*/
private static int onlineCount = 0;
/**concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。*/
private static ConcurrentHashMap<String,Client> webSocketMap = new ConcurrentHashMap<>();
/**与某个客户端的连接会话,需要通过它来给客户端发送数据*/
private Session session;
/**token*/
private String token="";
/**
* 连接建立成功调用的方法*/
@OnOpen
public void onOpen(Session session,@PathParam("token") String token) {
this.session = session;
this.token=token;
webSocketMap.put(token,new Client(token,session));
log.info("客户端连接" + token + "成功");
System.out.println("客户端连接" + token + "成功");
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
if(webSocketMap.containsKey(token)){
webSocketMap.remove(token);
//从set中删除
subOnlineCount();
}
for(Map.Entry<String, Client> entry: webSocketMap.entrySet()) {
Client client = webSocketMap.get(entry.getKey());//
if(session==client.getSession()){
webSocketMap.remove(entry.getKey());
}
}
log.info("客户端连接关闭");
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息*/
@OnMessage
public void onMessage(String message, Session session) {
log.info("onMessage+thread-----{}",Thread.currentThread().getName());
log.info("客户端:【{}】接收数据",message);
try {
session.getBasicRemote().sendText("pong:");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.info("onError+thread-----{}",Thread.currentThread().getName()+error);
for(Map.Entry<String, Client> entry: webSocketMap.entrySet()) {
Client client = webSocketMap.get(entry.getKey());//
if(session==client.getSession()){
webSocketMap.remove(entry.getKey());
}
}
}
/**
* 实现服务器主动推送
*/
public static void sendMessage(Session session, String message) throws IOException {
session.getBasicRemote().sendText(message);
}
/**
* 发送自定义消息
* */
public static void sendInfo(String message,@PathParam("token") String token) throws IOException {
for (Map.Entry<String, Client> entry: webSocketMap.entrySet()) {
try {
//这里可以设定只推送给这个token的,为null则全部推送
if(token==null) {
Session session=entry.getValue().getSession();
//客户端关闭连接之后,session并没有被删除或者置为null,而只是关闭了session的连接,可以在每次调用send之前判断一下session.isOpen()=true
//后期优化加上移除 session
if (session.isOpen()){
sendMessage(session,message);
}
}else if(entry.getValue().getToken().equals(token)){
Session session=entry.getValue().getSession();
if (session.isOpen()){
sendMessage(session,message);
}
}
} catch (IOException e) {
continue;
}
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
}
数据库业务
@Service
@RequiredArgsConstructor
public class WebSocketService {
private final TrackLibraryAlarmPersonByWebSocketMapper trackLibraryAlarmPersonByWebSocketMapper;
/**
* 获取最大的id
* @author songxue
* @date 2022/4/9 8:47
* @param
* @return
* @other
**/
public int getMaxId(MsgSocketId msgSocketIdDTO) {
int maxIdByPerson = trackMapper.selectMaxIdByPerson(msgSocketIdDTO);
return maxIdByPerson;
}
/**
* 修改最大的id
* @author songxue
* @date 2022/4/9 8:50
* @param
* @return
* @other
**/
public void updMsgSocketId(MsgSocketIdDTO msgSocketIdDTO) throws BaseException {
MsgSocketId msgSocketId = new MsgSocketId();
msgSocketId.setRecordName(msgSocketIdDTO.getRecordName());
if (CheckParamUtils.isNullOrEmpty(msgSocketId.getRecordName())) {
throw new ParamException("缺少表名");
}
int result = trackMapper.updateMsgSocketId(msgSocketId);
if (result <= 0) {
// SQL执行未生效
throw new ResultException(ExceptionConstant.DEFAULT_MAPPER_EXCEPTION_MSG);
}
}
/**
* 查询最新信息
* @author songxue
* @date 2022/4/9 8:55
* @param
* @return
* @other
**/
public List<BasicTrackSocketVO> getAlarmPersonByWebSocketList(BasicTrackSocketDTO alarmSocketDTO){
BasicSocket personSocket = new BasicSocket();
personSocket .setCommunityCode(alarmSocketDTO.getCommunityCode());
personSocket .setCreateUserId(alarmSocketDTO.getCreateUserId());
personSocket .setMaxId(alarmSocketDTO.getMaxId());
List<BasicSocket> alarmPersonList = trackSocketMapper.selectTrackLibraryAlarmPersonBySocket(personSocket );
List<BasicTrackLibraryAlarmPersonByWebSocketVO> resultList = new ArrayList<>();
for (BasicTrackLibraryAlarmPersonByWebSocket pws : alarmPersonList) {
BasicTrackSocketVO eachVO = BasicTrackSocketVO
.builder()
.id(pws.getId())
.libraryName(pws.getLibraryName())
.alarmLevel(pws.getAlarmLevel())
.personName(pws.getPersonName())
.deviceName(pws.getDeviceName())
.alarmTime(DateFormatUtils.formatDate("yyyy-MM-dd HH:mm:ss",pws.getAlarmTime()))
.build();
resultList.add(eachVO);
}
return resultList;
}
推送逻辑
@Slf4j
@Component
@EnableScheduling
@RequiredArgsConstructor
public class WebsocketSendTask {
@Autowired
private WebSocketService webSocketService;
/**
* @Description 定时获取bigId推送最新预警(每十秒一次)
* @Date 9:08 2021/5/29
* @Param []
* @return void
**/
@Scheduled(cron = "0/10 * * * * ?")
public void sendAlarm() throws BaseException {
BasicTrackSocketDTO alarmSocketDTO = new BasicTrackSocketDTO();
MsgSocketId msgSocketId = new MsgSocketId();
msgSocketId.setRecordName("basic_person");
int maxId = webSocketService.getMaxId(msgSocketId);
alarmSocketDTO .setMaxId(maxId);
alarmSocketDTO .setCreateUserId("zs");
List<BasicTrackSocketVO> webSocketList = webSocketService.getAlarmPersonByWebSocketList(alarmSocketDTO );
if (webSocketList.size() > 0){
log.info("推送人员信息"+webSocketList.size()+"条");
//分条发送
webSocketList.forEach(temp -> {
System.out.println(temp);
String str = JSON.toJSONString(temp);
System.out.println(str);
try {
WebSocketServer.sendInfo(str,null);
System.out.println("***************************websocket预警推送信息***************");
} catch (IOException e) {
e.printStackTrace();
}
//逐条将最大主键记录
Integer id = temp.getId();
msgSocketId.setMaxId(id);
});
//更改最大id
msgSocketId.setRecordName("basic_person");
}
}
}