當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
使用SpringBoot搭建一个简单的webSocket服务
生活随笔
收集整理的這篇文章主要介紹了
使用SpringBoot搭建一个简单的webSocket服务
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前言
個(gè)人地址:使用SpringBoot搭建一個(gè)簡(jiǎn)單的webSocket服務(wù)
WebSocket是一個(gè)HTML5新增的協(xié)議,它的目的在瀏覽器和服務(wù)器之間建立一個(gè)不受限的雙向?qū)崟r(shí)通信的通道。比如,服務(wù)器可以任意時(shí)刻發(fā)送消息給瀏覽器。它是基于TCP,先通過HTTP/HTTPS協(xié)議發(fā)起一條特殊的HTTP請(qǐng)求進(jìn)行握手后創(chuàng)建一個(gè)用于交換數(shù)據(jù)的TCP連接。
2.有什么優(yōu)勢(shì)?
webSocket只需要一次握手就可以實(shí)時(shí)發(fā)送消息。
搭建環(huán)境
1. 創(chuàng)建基礎(chǔ)工程
我們使用Idea編輯器創(chuàng)建一個(gè)開發(fā)的基本工程,這里我們通過Spring Initializr創(chuàng)建。
通過next下一步,填寫自己的包路徑以及項(xiàng)目名稱。
一直下一步,直到創(chuàng)建完成。
2. 引入jar包
<!-- springboot依賴 --> <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.5</version><relativePath/> <!-- lookup parent from repository --> </parent> <!-- springboot websocket依賴 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId> </dependency>3. 注冊(cè)bean配置
@Bean public ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter(); }4. 編寫服務(wù)類
package com.zy.websocket.server;import org.springframework.stereotype.Component;import javax.websocket.OnClose; import javax.websocket.OnError; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import java.io.IOException; import java.util.concurrent.CopyOnWriteArraySet;@Component @ServerEndpoint(value = "/webSocket") public class SocketServer {/*** 靜態(tài)變量,用來記錄當(dāng)前在線連接數(shù)。應(yīng)該把它設(shè)計(jì)成線程安全的。*/private static int onlineCount = 0;/*** concurrent包的線程安全Set,用來存放每個(gè)客戶端對(duì)應(yīng)的MyWebSocket對(duì)象。*/private static CopyOnWriteArraySet<SocketServer> webSocketSet = new CopyOnWriteArraySet<SocketServer>();/*** 與某個(gè)客戶端的連接會(huì)話,需要通過它來給客戶端發(fā)送數(shù)據(jù)*/private Session session;/*** 連接建立成功調(diào)用的方法*/@OnOpenpublic void onOpen(Session session) {this.session = session;//加入set中webSocketSet.add(this);//在線數(shù)加1addOnlineCount();System.out.println("有新連接加入!當(dāng)前在線人數(shù)為" + getOnlineCount());try {sendMessage("當(dāng)前在線人數(shù)為" + getOnlineCount());} catch (IOException e) {System.out.println("IO異常");}}/*** 連接關(guān)閉調(diào)用的方法*/@OnClosepublic void onClose() {//從set中刪除webSocketSet.remove(this);//在線數(shù)減1subOnlineCount();System.out.println("有一連接關(guān)閉!當(dāng)前在線人數(shù)為" + getOnlineCount());}/*** 收到客戶端消息后調(diào)用的方法** @param message 客戶端發(fā)送過來的消息*/@OnMessagepublic void onMessage(String message, Session session) {System.out.println("來自客戶端的消息:" + message);//群發(fā)消息for (SocketServer item : webSocketSet) {try {item.sendMessage(message);} catch (IOException e) {e.printStackTrace();}}}@OnErrorpublic 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);}/*** 群發(fā)自定義消息*/public static void sendInfo(String message) throws IOException {for (SocketServer item : webSocketSet) {try {item.sendMessage(message);} catch (IOException e) {continue;}}}public static synchronized int getOnlineCount() {return onlineCount;}public static synchronized void addOnlineCount() {SocketServer.onlineCount++;}public static synchronized void subOnlineCount() {SocketServer.onlineCount--;} }5.測(cè)試服務(wù)
總結(jié)
以上是生活随笔為你收集整理的使用SpringBoot搭建一个简单的webSocket服务的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何删除电脑浏览历史如何删除我的电脑
- 下一篇: 不会使用Origin8.5,作图只用ex