日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

使用SpringBoot搭建一个简单的webSocket服务

發布時間:2023/12/3 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用SpringBoot搭建一个简单的webSocket服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

個人地址:使用SpringBoot搭建一個簡單的webSocket服務

  • 什么是WebSocket?
    WebSocket是一個HTML5新增的協議,它的目的在瀏覽器和服務器之間建立一個不受限的雙向實時通信的通道。比如,服務器可以任意時刻發送消息給瀏覽器。它是基于TCP,先通過HTTP/HTTPS協議發起一條特殊的HTTP請求進行握手后創建一個用于交換數據的TCP連接。
    2.有什么優勢?
    webSocket只需要一次握手就可以實時發送消息。
  • 搭建環境

    1. 創建基礎工程

    我們使用Idea編輯器創建一個開發的基本工程,這里我們通過Spring Initializr創建。

    通過next下一步,填寫自己的包路徑以及項目名稱。

    一直下一步,直到創建完成。

    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. 注冊bean配置

    @Bean public ServerEndpointExporter serverEndpointExporter() {return new ServerEndpointExporter(); }

    4. 編寫服務類

    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 {/*** 靜態變量,用來記錄當前在線連接數。應該把它設計成線程安全的。*/private static int onlineCount = 0;/*** concurrent包的線程安全Set,用來存放每個客戶端對應的MyWebSocket對象。*/private static CopyOnWriteArraySet<SocketServer> webSocketSet = new CopyOnWriteArraySet<SocketServer>();/*** 與某個客戶端的連接會話,需要通過它來給客戶端發送數據*/private Session session;/*** 連接建立成功調用的方法*/@OnOpenpublic void onOpen(Session session) {this.session = session;//加入set中webSocketSet.add(this);//在線數加1addOnlineCount();System.out.println("有新連接加入!當前在線人數為" + getOnlineCount());try {sendMessage("當前在線人數為" + getOnlineCount());} catch (IOException e) {System.out.println("IO異常");}}/*** 連接關閉調用的方法*/@OnClosepublic void onClose() {//從set中刪除webSocketSet.remove(this);//在線數減1subOnlineCount();System.out.println("有一連接關閉!當前在線人數為" + getOnlineCount());}/*** 收到客戶端消息后調用的方法** @param message 客戶端發送過來的消息*/@OnMessagepublic void onMessage(String message, Session session) {System.out.println("來自客戶端的消息:" + message);//群發消息for (SocketServer item : webSocketSet) {try {item.sendMessage(message);} catch (IOException e) {e.printStackTrace();}}}@OnErrorpublic void onError(Session session, Throwable error) {System.out.println("發生錯誤");error.printStackTrace();}public void sendMessage(String message) throws IOException {this.session.getBasicRemote().sendText(message);}/*** 群發自定義消息*/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.測試服務

    總結

    以上是生活随笔為你收集整理的使用SpringBoot搭建一个简单的webSocket服务的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。