欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java后端+前端使用WebSocket實現(xiàn)消息推送的詳細流程

 更新時間:2022年10月27日 12:04:23   作者:poker_zero  
后端向前端推送消息就需要長連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關于java后端+前端使用WebSocket實現(xiàn)消息推送的詳細流程,需要的朋友可以參考下

 

前言

在項目的開發(fā)時,遇到實現(xiàn)服務器主動發(fā)送數(shù)據(jù)到前端頁面的功能的需求。實現(xiàn)該功能不外乎使用輪詢和websocket技術,但在考慮到實時性和資源損耗后,最后決定使用websocket?,F(xiàn)在就記錄一下用Java實現(xiàn)Websocket技術吧~

Java實現(xiàn)Websocket通常有兩種方式:1、創(chuàng)建WebSocketServer類,里面包含open、close、message、error等方法;2、利用Springboot提供的webSocketHandler類,創(chuàng)建其子類并重寫方法。我們項目雖然使用Springboot框架,不過仍采用了第一種方法實現(xiàn)。

創(chuàng)建WebSocket的簡單實例操作流程

1.引入Websocket依賴

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-websocket -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
            <version>2.7.0</version>
        </dependency>

2.創(chuàng)建配置類WebSocketConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * 開啟WebSocket支持
 */
@Configuration
public class WebSocketConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

3.創(chuàng)建WebSocketServer

在websocket協(xié)議下,后端服務器相當于ws里面的客戶端,需要用@ServerEndpoint指定訪問路徑,并使用@Component注入容器

@ServerEndpoint:當ServerEndpointExporter類通過Spring配置進行聲明并被使用,它將會去掃描帶有@ServerEndpoint注解的類。被注解的類將被注冊成為一個WebSocket端點。所有的配置項都在這個注解的屬性中
( 如:@ServerEndpoint(“/ws”) )

下面的栗子中@ServerEndpoint指定訪問路徑中包含sid,這個是用于區(qū)分每個頁面

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.net.Socket;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ServerEndpoint 注解是一個類層次的注解,它的功能主要是將目前的類定義成一個websocket服務器端,
 * 注解的值將被用于監(jiān)聽用戶連接的終端訪問URL地址,客戶端可以通過這個URL來連接到WebSocket服務器端
 */
@ServerEndpoint("/notice/{userId}")
@Component
@Slf4j
public class NoticeWebsocket {

    //記錄連接的客戶端
    public static Map<String, Session> clients = new ConcurrentHashMap<>();

    /**
     * userId關聯(lián)sid(解決同一用戶id,在多個web端連接的問題)
     */
    public static Map<String, Set<String>> conns = new ConcurrentHashMap<>();

    private String sid = null;

    private String userId;


    /**
     * 連接成功后調(diào)用的方法
     * @param session
     * @param userId
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("userId") String userId) {
        this.sid = UUID.randomUUID().toString();
        this.userId = userId;
        clients.put(this.sid, session);

        Set<String> clientSet = conns.get(userId);
        if (clientSet==null){
            clientSet = new HashSet<>();
            conns.put(userId,clientSet);
        }
        clientSet.add(this.sid);
        log.info(this.sid + "連接開啟!");
    }

    /**
     * 連接關閉調(diào)用的方法
     */
    @OnClose
    public void onClose() {
        log.info(this.sid + "連接斷開!");
        clients.remove(this.sid);
    }

    /**
     * 判斷是否連接的方法
     * @return
     */
    public static boolean isServerClose() {
        if (NoticeWebsocket.clients.values().size() == 0) {
            log.info("已斷開");
            return true;
        }else {
            log.info("已連接");
            return false;
        }
    }

    /**
     * 發(fā)送給所有用戶
     * @param noticeType
     */
    public static void sendMessage(String noticeType){
        NoticeWebsocketResp noticeWebsocketResp = new NoticeWebsocketResp();
        noticeWebsocketResp.setNoticeType(noticeType);
        sendMessage(noticeWebsocketResp);
    }


    /**
     * 發(fā)送給所有用戶
     * @param noticeWebsocketResp
     */
    public static void sendMessage(NoticeWebsocketResp noticeWebsocketResp){
        String message = JSONObject.toJSONString(noticeWebsocketResp);
        for (Session session1 : NoticeWebsocket.clients.values()) {
            try {
                session1.getBasicRemote().sendText(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根據(jù)用戶id發(fā)送給某一個用戶
     * **/
    public static void sendMessageByUserId(String userId, NoticeWebsocketResp noticeWebsocketResp) {
        if (!StringUtils.isEmpty(userId)) {
            String message = JSONObject.toJSONString(noticeWebsocketResp);
            Set<String> clientSet = conns.get(userId);
            if (clientSet != null) {
                Iterator<String> iterator = clientSet.iterator();
                while (iterator.hasNext()) {
                    String sid = iterator.next();
                    Session session = clients.get(sid);
                    if (session != null) {
                        try {
                            session.getBasicRemote().sendText(message);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    /**
     * 收到客戶端消息后調(diào)用的方法
     * @param message
     * @param session
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("收到來自窗口"+this.userId+"的信息:"+message);
    }

    /**
     * 發(fā)生錯誤時的回調(diào)函數(shù)
     * @param error
     */
    @OnError
    public void onError(Throwable error) {
        log.info("錯誤");
        error.printStackTrace();
    }
}

封裝了一個發(fā)送消息的對象可以直接使用

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel("ws通知返回對象")
public class NoticeWebsocketResp<T> {

    @ApiModelProperty(value = "通知類型")
    private String noticeType;

    @ApiModelProperty(value = "通知內(nèi)容")
    private T noticeInfo;

}

4.websocket調(diào)用

一個用戶調(diào)用接口,主動將信息發(fā)給后端,后端接收后再主動推送給指定/全部用戶

@RestController
@RequestMapping("/order")
public class OrderController {
	@GetMapping("/test")
    public R test() {
    	NoticeWebsocket.sendMessage("你好,WebSocket");
        return R.ok();
    }
}

前端WebSocket連接

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SseEmitter</title>
</head>
<body>
<div id="message"></div>
</body>
<script>
var limitConnect = 0;
init();
function init() {
var ws = new WebSocket('ws://192.168.2.88:9060/notice/1');
// 獲取連接狀態(tài)
console.log('ws連接狀態(tài):' + ws.readyState);
//監(jiān)聽是否連接成功
ws.onopen = function () {
    console.log('ws連接狀態(tài):' + ws.readyState);
    limitConnect = 0;
    //連接成功則發(fā)送一個數(shù)據(jù)
    ws.send('我們建立連接啦');
}
// 接聽服務器發(fā)回的信息并處理展示
ws.onmessage = function (data) {
    console.log('接收到來自服務器的消息:');
    console.log(data);
    //完成通信后關閉WebSocket連接
    // ws.close();
}
// 監(jiān)聽連接關閉事件
ws.onclose = function () {
    // 監(jiān)聽整個過程中websocket的狀態(tài)
    console.log('ws連接狀態(tài):' + ws.readyState);
reconnect();

}
// 監(jiān)聽并處理error事件
ws.onerror = function (error) {
    console.log(error);
}
}
function reconnect() {
    limitConnect ++;
    console.log("重連第" + limitConnect + "次");
    setTimeout(function(){
        init();
    },2000);
   
}
</script>
</html>

項目啟動,打開頁面后控制臺打印連接信息

調(diào)用order/test方法后前端打印推送消息內(nèi)容

這樣,就可以接口或者ws調(diào)用網(wǎng)址的方式進行websocket的通信啦~

如果沒有前端頁面也可以使用在線WebSocket測試

總結

到此這篇關于java后端+前端使用WebSocket實現(xiàn)消息推送的文章就介紹到這了,更多相關javaWebSocket實現(xiàn)消息推送內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 詳解Java類庫的概念以及import的使用方法

    詳解Java類庫的概念以及import的使用方法

    這篇文章主要介紹了詳解Java類庫的概念以及import的使用方法,是Java入門學習中的基礎知識,需要的朋友可以參考下
    2015-09-09
  • SpringBoot項目里集成Hibernate的示例

    SpringBoot項目里集成Hibernate的示例

    在Spring Boot項目中,集成Hibernate可以幫助我們更輕松地進行數(shù)據(jù)庫操作,本文將介紹如何在Spring Boot項目中集成Hibernate,并提供相應的示例,感興趣的朋友跟隨小編一起看看吧
    2023-04-04
  • Java正則驗證電話,手機,郵箱,日期,金額的方法示例

    Java正則驗證電話,手機,郵箱,日期,金額的方法示例

    這篇文章主要介紹了Java正則驗證電話,手機,郵箱,日期,金額的方法,結合具體實例形式分析了Java針對電話,手機,郵箱,日期,金額的正則判定操作技巧,需要的朋友可以參考下
    2017-03-03
  • Java編寫Mapreduce程序過程淺析

    Java編寫Mapreduce程序過程淺析

    MapReduce是一種用于處理大規(guī)模數(shù)據(jù)集的并行編程模型,其特點高效性和可擴展性,在本文中,我們將深入了解MapReduce,并使用Java編寫一個簡單的MapReduce程序,需要的朋友可以參考下
    2023-05-05
  • 深入解析Java的設計模式編程中建造者模式的運用

    深入解析Java的設計模式編程中建造者模式的運用

    這篇文章主要介紹了深入解析Java的設計模式編程中建造者模式的運用,同時文中也介紹了建造者模式與工廠模式的區(qū)別,需要的朋友可以參考下
    2016-02-02
  • Spring MVC傳遞接收參數(shù)方式小結

    Spring MVC傳遞接收參數(shù)方式小結

    大家在開發(fā)中經(jīng)常會用到Spring MVC Controller來接收請求參數(shù),主要常用的接收方式就是通過實體對象以及形參等方式、有些用于GET請求,有些用于POST請求,有些用于兩者,下面介紹幾種常見的Spring MVC傳遞接收參數(shù)的方式
    2021-11-11
  • RabbitMQ基礎概念之信道channel詳解

    RabbitMQ基礎概念之信道channel詳解

    這篇文章主要介紹了RabbitMQ基礎概念之信道channel詳解,信道是生產(chǎn)消費者與rabbit通信的渠道,生產(chǎn)者publish或者消費者消費一個隊列都是需要通過信道來通信的,需要的朋友可以參考下
    2023-08-08
  • 淺析Java中的內(nèi)存泄漏

    淺析Java中的內(nèi)存泄漏

    這篇文章主要介紹了Java中的內(nèi)存泄漏,包括其基本概念和基本的預防措施,需要的朋友可以參考下
    2015-07-07
  • Spring Data JPA進行數(shù)據(jù)分頁與排序的方法

    Spring Data JPA進行數(shù)據(jù)分頁與排序的方法

    這篇文章主要介紹了Spring Data JPA進行數(shù)據(jù)分頁與排序的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-11-11
  • Java如何通過枚舉實現(xiàn)有限狀態(tài)機

    Java如何通過枚舉實現(xiàn)有限狀態(tài)機

    這篇文章主要介紹了Java如何通過枚舉實現(xiàn)有限狀態(tài)機,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07

最新評論