詳解spring boot Websocket使用筆記
本文只作為個(gè)人筆記,大部分代碼是引用其他人的文章的。
在springboot項(xiàng)目中使用websocket做推送,雖然挺簡(jiǎn)單的,但初學(xué)也踩過(guò)幾個(gè)坑,特此記錄。
使用websocket有兩種方式:1是使用sockjs,2是使用h5的標(biāo)準(zhǔn)。使用Html5標(biāo)準(zhǔn)自然更方便簡(jiǎn)單,所以記錄的是配合h5的使用方法。
1、pom
核心是@ServerEndpoint這個(gè)注解。這個(gè)注解是Javaee標(biāo)準(zhǔn)里的注解,tomcat7以上已經(jīng)對(duì)其進(jìn)行了實(shí)現(xiàn),如果是用傳統(tǒng)方法使用tomcat發(fā)布項(xiàng)目,只要在pom文件中引入javaee標(biāo)準(zhǔn)即可使用。
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>provided</scope> </dependency>
但使用springboot的內(nèi)置tomcat時(shí),就不需要引入javaee-api了,spring-boot已經(jīng)包含了。使用springboot的websocket功能首先引入springboot組件。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
順便說(shuō)一句,springboot的高級(jí)組件會(huì)自動(dòng)引用基礎(chǔ)的組件,像spring-boot-starter-websocket就引入了spring-boot-starter-web和spring-boot-starter,所以不要重復(fù)引入。
2、使用@ServerEndpoint創(chuàng)立websocket endpoint
首先要注入ServerEndpointExporter,這個(gè)bean會(huì)自動(dòng)注冊(cè)使用了@ServerEndpoint注解聲明的Websocket endpoint。要注意,如果使用獨(dú)立的servlet容器,而不是直接使用springboot的內(nèi)置容器,就不要注入ServerEndpointExporter,因?yàn)樗鼘⒂扇萜髯约禾峁┖凸芾怼?/p>
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
接下來(lái)就是寫(xiě)websocket的具體實(shí)現(xiàn)類(lèi),很簡(jiǎn)單,直接上代碼:
@ServerEndpoint(value = "/websocket")
@Component
public class MyWebSocket {
//靜態(tài)變量,用來(lái)記錄當(dāng)前在線(xiàn)連接數(shù)。應(yīng)該把它設(shè)計(jì)成線(xiàn)程安全的。
private static int onlineCount = 0;
//concurrent包的線(xiàn)程安全Set,用來(lái)存放每個(gè)客戶(hù)端對(duì)應(yīng)的MyWebSocket對(duì)象。
private static CopyOnWriteArraySet<MyWebSocket> webSocketSet = new CopyOnWriteArraySet<MyWebSocket>();
//與某個(gè)客戶(hù)端的連接會(huì)話(huà),需要通過(guò)它來(lái)給客戶(hù)端發(fā)送數(shù)據(jù)
private Session session;
/**
* 連接建立成功調(diào)用的方法*/
@OnOpen
public void onOpen(Session session) {
this.session = session;
webSocketSet.add(this); //加入set中
addOnlineCount(); //在線(xiàn)數(shù)加1
System.out.println("有新連接加入!當(dāng)前在線(xiàn)人數(shù)為" + getOnlineCount());
try {
sendMessage(CommonConstant.CURRENT_WANGING_NUMBER.toString());
} catch (IOException e) {
System.out.println("IO異常");
}
}
/**
* 連接關(guān)閉調(diào)用的方法
*/
@OnClose
public void onClose() {
webSocketSet.remove(this); //從set中刪除
subOnlineCount(); //在線(xiàn)數(shù)減1
System.out.println("有一連接關(guān)閉!當(dāng)前在線(xiàn)人數(shù)為" + getOnlineCount());
}
/**
* 收到客戶(hù)端消息后調(diào)用的方法
*
* @param message 客戶(hù)端發(fā)送過(guò)來(lái)的消息*/
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("來(lái)自客戶(hù)端的消息:" + message);
//群發(fā)消息
for (MyWebSocket item : webSocketSet) {
try {
item.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 發(fā)生錯(cuò)誤時(shí)調(diào)用
@OnError
public void onError(Session session, Throwable error) {
System.out.println("發(fā)生錯(cuò)誤");
error.printStackTrace();
}
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
//this.session.getAsyncRemote().sendText(message);
}
/**
* 群發(fā)自定義消息
* */
public static void sendInfo(String message) throws IOException {
for (MyWebSocket item : webSocketSet) {
try {
item.sendMessage(message);
} catch (IOException e) {
continue;
}
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
MyWebSocket.onlineCount++;
}
public static synchronized void subOnlineCount() {
MyWebSocket.onlineCount--;
}
}
使用springboot的唯一區(qū)別是要@Component聲明下,而使用獨(dú)立容器是由容器自己管理websocket的,但在springboot中連容器都是spring管理的。
雖然@Component默認(rèn)是單例模式的,但springboot還是會(huì)為每個(gè)websocket連接初始化一個(gè)bean,所以可以用一個(gè)靜態(tài)set保存起來(lái)。
3、前端代碼
<!DOCTYPE HTML>
<html>
<head>
<title>My WebSocket</title>
</head>
<body>
Welcome<br/>
<input id="text" type="text" /><button onclick="send()">Send</button> <button onclick="closeWebSocket()">Close</button>
<div id="message">
</div>
</body>
<script type="text/javascript">
var websocket = null;
//判斷當(dāng)前瀏覽器是否支持WebSocket
if('WebSocket' in window){
websocket = new WebSocket("ws://localhost:8084/websocket");
}
else{
alert('Not support websocket')
}
//連接發(fā)生錯(cuò)誤的回調(diào)方法
websocket.onerror = function(){
setMessageInnerHTML("error");
};
//連接成功建立的回調(diào)方法
websocket.onopen = function(event){
setMessageInnerHTML("open");
}
//接收到消息的回調(diào)方法
websocket.onmessage = function(event){
setMessageInnerHTML(event.data);
}
//連接關(guān)閉的回調(diào)方法
websocket.onclose = function(){
setMessageInnerHTML("close");
}
//監(jiān)聽(tīng)窗口關(guān)閉事件,當(dāng)窗口關(guān)閉時(shí),主動(dòng)去關(guān)閉websocket連接,防止連接還沒(méi)斷開(kāi)就關(guān)閉窗口,server端會(huì)拋異常。
window.onbeforeunload = function(){
websocket.close();
}
//將消息顯示在網(wǎng)頁(yè)上
function setMessageInnerHTML(innerHTML){
document.getElementById('message').innerHTML += innerHTML + '<br/>';
}
//關(guān)閉連接
function closeWebSocket(){
websocket.close();
}
//發(fā)送消息
function send(){
var message = document.getElementById('text').value;
websocket.send(message);
}
</script>
</html>
4、總結(jié)
springboot已經(jīng)做了深度的集成和優(yōu)化,要注意是否添加了不需要的依賴(lài)、配置或聲明。由于很多講解組件使用的文章是和spring集成的,會(huì)有一些配置,在使用springboot時(shí),由于springboot已經(jīng)有了自己的配置,再這些配置有可能導(dǎo)致各種各樣的異常。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring WebSocket 404錯(cuò)誤的解決方法
- Spring和Websocket相結(jié)合實(shí)現(xiàn)消息的推送
- 完美解決spring websocket自動(dòng)斷開(kāi)連接再創(chuàng)建引發(fā)的問(wèn)題
- java中實(shí)現(xiàn)兼容ie6 7 8 9的spring4+websocket
- 詳解WebSocket+spring示例demo(已使用sockJs庫(kù))
- Spring整合WebSocket應(yīng)用示例(上)
- 詳解在Spring Boot框架下使用WebSocket實(shí)現(xiàn)消息推送
- SpringBoot webSocket實(shí)現(xiàn)發(fā)送廣播、點(diǎn)對(duì)點(diǎn)消息和Android接收
- Spring整合websocket整合應(yīng)用示例(下)
- Spring集成webSocket頁(yè)面訪(fǎng)問(wèn)404問(wèn)題的解決方法
相關(guān)文章
Java實(shí)現(xiàn)合并兩個(gè)有序序列算法示例
這篇文章主要介紹了Java實(shí)現(xiàn)合并兩個(gè)有序序列算法,簡(jiǎn)單描述了序列合并算法的原理與java合并有序序列的具體操作步驟及相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09
關(guān)于springboot的接口返回值統(tǒng)一標(biāo)準(zhǔn)格式
這篇文章主要介紹了關(guān)于springboot的接口返回值統(tǒng)一標(biāo)準(zhǔn)格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
springMVC框架下JQuery傳遞并解析Json數(shù)據(jù)
json作為一種輕量級(jí)的數(shù)據(jù)交換格式,在前后臺(tái)數(shù)據(jù)交換中占據(jù)著非常重要的地位,這篇文章主要介紹了springMVC框架下JQuery傳遞并解析Json數(shù)據(jù),有興趣的可以了解一下。2017-01-01
因Spring AOP導(dǎo)致@Autowired依賴(lài)注入失敗的解決方法
這篇文章主要給大家介紹了因Spring AOP導(dǎo)致@Autowired依賴(lài)注入失敗的解決方法,文中通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
Java女裝商城系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實(shí)現(xiàn)一個(gè)女裝商城系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11

