1 springBoot中已經集成webSocket的依賴
maven座標
<!--webSocket使用-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
2 建立WebSocketConfig類
package com.guoheng.hazard.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* 類功能描述: WebSocket配置類
* 使用此配置可以關閉servlet容器對websocket端點的掃描,這個暫時沒有深入研究。
* @author fbl
* @date 2019-12-03 08:33
*/
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
3 建立 WebSocketServer類
package com.guoheng.hazard.manager.websocket;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
/**
* 類功能描述: webSocket服務層
* 這裏我們連接webSocket的時候,路徑中傳一個參數值id,用來區分不同頁面推送不同的數據
*
* @author fbl
* @date 2019-02-13 8:02
*/
@ServerEndpoint(value = "/websocket/{id}")
@Component
public class WebSocketServer {
/**
* 靜態變量,用來記錄當前在線連接數。應該把它設計成線程安全的。
*/
private static int onlineCount = 0;
/**
* concurrent包的線程安全Set,用來存放每個客戶端對應的MyWebSocket對象。
*/
public static ConcurrentHashMap<Integer, WebSocketServer> webSocketSet = new ConcurrentHashMap<Integer, WebSocketServer>();
/**
* 與某個客戶端的連接會話,需要通過它來給客戶端發送數據
*/
private Session session;
/**
* 傳過來的id
*/
private Integer id = 0;
/**
* 連接建立成功調用的方法*/
@OnOpen
public void onOpen(@PathParam(value = "id") Integer param, Session session) {
//接收到發送消息的人員編號
id = param;
this.session = session;
/**加入set中*/
webSocketSet.put(param,this);
/**在線數加1*/
addOnlineCount();
System.out.println("有新連接加入!當前在線人數爲" + getOnlineCount());
try {
sendMessage("-連接已建立-");
} catch (IOException e) {
System.out.println("IO異常");
}
}
/**
* 連接關閉調用的方法
*/
@OnClose
public void onClose() {
if (id != null && id != 0) {
/** 從set中刪除 */
webSocketSet.remove(id);
/** 在線數減1 */
subOnlineCount();
System.out.println("有一連接關閉!當前在線人數爲" + getOnlineCount());
}
}
/**
* 收到客戶端消息後調用的方法
*
* @param message 客戶端發送過來的消息*/
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("來自客戶端的消息:" + message);
try {
this.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 發生錯誤時調用
* **/
@OnError
public void onError(Session session, Throwable error) {
System.out.println("發生錯誤");
error.printStackTrace();
}
public void sendMessage(String message) throws IOException {
synchronized (session) {
getSession().getBasicRemote().sendText(message);
}
}
/**
* 給指定的人發送消息
* @param message
*/
public void sendToMessageById(Integer id,String message) {
try {
if (webSocketSet.get(id) != null) {
webSocketSet.get(id).sendMessage(message);
} else {
System.out.println("webSocketSet中沒有此key,不推送消息");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 羣發自定義消息
* */
public static void sendInfo(String message) throws IOException {
for (WebSocketServer item : webSocketSet.values()) {
try {
item.sendMessage(message);
} catch (IOException e) {
continue;
}
}
}
public Session getSession() {
return session;
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
}
4 建立連接webSocket的參數枚舉
記得與前端約定好,那個參數(此處是id)代表那個頁面
package com.guoheng.hazard.common.enums;
import lombok.Data;
/**
* 枚舉描述: 不同頁面區分webSocket
*
* @author fbl
* @date 2019-12-12 09:34
*/
public enum WebSocketEnum {
/**
* 實時報警
*/
SOCKET_ENUM_ALARM(1,"實時報警"),
/**
* 溫度傳感器
*/
SOCKET_ENUM_TEMP(2,"溫度傳感器"),
/**
* 壓力傳感器
*/
SOCKET_ENUM_PRESSURE(3,"壓力傳感器"),
/**
* 液位傳感器
*/
SOCKET_ENUM_LIQUID(4,"液位傳感器"),
/**
* 可燃傳感器
*/
SOCKET_ENUM_COMBUSTIBLE(5,"可燃傳感器"),
/**
* 有毒傳感器
*/
SOCKET_ENUM_POISONOUS(6,"有毒傳感器");
private Integer code;
private String msg;
WebSocketEnum(Integer code,String msg){
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
5 定時任務我們使用spring自帶的task
這是服務層使用webSocket+定時任務的代碼
@Override
@Scheduled(cron = "0/3 * * * * ?")
public Result realTimeAlarm() {
if (CollectionUtil.isNotEmpty(WebSocketServer.webSocketSet)) {
// 查詢待處理報警類
List<MajorHazardAlarmSelectDTO> alarm = majorHazardAlarmMapper.getAlarm(null, null, null
, null, null, 1);
String alarmToString = JSON.toJSONString(alarm);
webSocketServer.sendToMessageById(SOCKET_ENUM_ALARM.getCode(), alarmToString);
}
return Result.success();
}
控制層代碼
@ApiOperation(value = "實時報警")
@GetMapping(value = "data/major/hazard/realTimeAlarm")
public Result realTimeAlarm() {
return safeOneMapService.realTimeAlarm();
}
至此,調用這個接口,連接webSocket,傳遞不同的參數,就可以每三秒推送一次數據庫中的數據,達到實時刷新的目的,同時不同頁面傳不同參數,達到推送不同數據的目的