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

java中Websocket的使用方法例子

 更新時間:2023年11月28日 10:28:08   作者:煙雨**魚兒  
這篇文章主要給大家介紹了關(guān)于java中Websocket的使用方法,WebSocket是HTML5開始提供的一種在瀏覽器和服務器間進行全雙工通信的協(xié)議,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

什么試WebSocket

WebSocket是一個連接,這個連接是客戶端(頁面)與服務端之間的連接,處理兩者間通訊;

好處:HTML5定義了WebSocket協(xié)議,能更好的節(jié)省服務器資源和帶寬,并且能夠更實時地進行通訊,不需要每次發(fā)送請求接口獲取數(shù)據(jù),
使用場景:比如客戶端登錄后,出現(xiàn)的消息推送,每天定時廣播推送給客戶端消息等場景;

SpringBoot maven項目中實現(xiàn)

導入依賴:

		<dependency>
		   <groupId>org.springframework.boot</groupId>
		   <artifactId>spring-boot-starter-websocket</artifactId>
		</dependency>

客戶端和服務端怎么鏈接呢?前端實現(xiàn)也是固定的寫法,只需要請求/websocket/{userId} 這個地址即可實現(xiàn)鏈接

前端js代碼:

//判斷當前瀏覽器是否支持WebSocket  
if ('WebSocket' in window) {  
    websocket = new WebSocket("ws://" + document.location.host + "/WebChat/websocket/" + username + "/"+ _img);  
} else {  
    alert('當前瀏覽器 Not support websocket')  
} 

java 實現(xiàn)websocket連接的代碼:

package org.jeecg.modules.message.websocket;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.WebsocketConst;
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.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @Author wsf
 * @Date 2023/04/29 9:41
 * @Description: 此注解相當于設(shè)置訪問URL
 */
@Component
@Slf4j
@ServerEndpoint("/websocket/{userId}") //此注解相當于設(shè)置訪問URL
public class WebSocket {

    private Session session;

    private static final CopyOnWriteArraySet<WebSocket> webSockets = new CopyOnWriteArraySet<>();
    /*
    websocket是客戶端和服務端之間建立了一個連接,建立完連接以后,會生成一個websocket對象,我們可以用這個對象來執(zhí)行發(fā)送,接收等操作。但是這只是一個存在于客戶端與服務器之間的鏈接,換句話說,系統(tǒng)只能識別到這個websocket連接是對應于哪個頁面(瀏覽器),而這個頁面在系統(tǒng)中是對應哪個用戶(數(shù)據(jù)庫中的用戶,或者根本就沒有對應任何用戶,即未登錄,只是一個游客),我們是無法從這個websocket對象中獲取的。所以我們需要創(chuàng)建一個Map對象,用于將websocket對象和實際的user對象進行關(guān)聯(lián),這樣為我們后續(xù)向特定的用戶推送消息做鋪墊
    */
    private static final Map<String, Session> sessionPool = new HashMap<>();

    @OnOpen
    public void onOpen(Session session, @PathParam(value = "userId") String userId) {
        try {
            this.session = session;
            webSockets.add(this);
            sessionPool.put(userId, session);
            log.info("【websocket消息】有新的連接,總數(shù)為: {}", webSockets.size());
        } catch (Exception e) {
        }
    }

    @OnClose
    public void onClose(@PathParam(value = "userId") String userId) {
        try {
            webSockets.remove(this);
            sessionPool.remove(userId);
            log.info("【websocket消息】連接斷開,總數(shù)為: {}", webSockets.size());
        } catch (Exception e) {
        }
    }

    @OnMessage
    public void onMessage(String message) {
        log.debug("【websocket消息】收到客戶端消息: {}", message);
        JSONObject obj = new JSONObject();
        obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_CHECK);//業(yè)務類型
        obj.put(WebsocketConst.MSG_TXT, "心跳響應");//消息內(nèi)容
        session.getAsyncRemote().sendText(obj.toJSONString());
    }


    @OnError
    public void OnError(Session session, @PathParam(value = "userId") String userId, Throwable t) {
        try {
            if (session.isOpen()) {
                session.close();
            }
            webSockets.remove(this);
            sessionPool.remove(userId);
            log.info("【websocket消息】連接[錯誤]斷開,總數(shù)為: {}, 錯誤:{}", webSockets.size(), t.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 此為廣播消息
    public void sendAllMessage(String message) {
        log.info("【websocket消息】廣播消息:" + message);
        for (WebSocket webSocket : webSockets) {
            try {
                if (webSocket.session.isOpen()) {
                    webSocket.session.getAsyncRemote().sendText(message);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 此為單點消息
    public void sendOneMessage(String userId, String message) {
        Session session = sessionPool.get(userId);
        if (session != null && session.isOpen()) {
            try {
                log.info("【websocket消息】 單點消息:" + message);
                session.getAsyncRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 此為單點消息(多人)
    public void sendMoreMessage(String[] userIds, String message) {
        for (String userId : userIds) {
            Session session = sessionPool.get(userId);
            if (session != null && session.isOpen()) {
                try {
                    log.info("【websocket消息】 單點消息:" + message);
                    session.getAsyncRemote().sendText(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

在RunApplication中加入:

package org.jeecg.config;

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

@Configuration
public class WebSocketConfig {
      /**
     * 會自動注冊使用了@ServerEndpoint注解聲明的Websocket endpoint
     * 要注意,如果使用獨立的servlet容器,
     * 而不是直接使用springboot的內(nèi)置容器,
     * 就不要注入ServerEndpointExporter,因為它將由容器自己提供和管理。
     *  注入ServerEndpointExporter,
     * 	這個bean會自動注冊使用了@ServerEndpoint注解聲明的Websocket endpoint
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
      return new ServerEndpointExporter();
    }
}

可以編寫一個test 測試連接

public class WebSocketTest {

    public static void main(String[] args) {
        try {

            // 創(chuàng)建WebSocket客戶端
            MyWebSocketClient myClient = new MyWebSocketClient(new URI("ws://127.0.0.1:9091/web/websocket/123333"));
            // 與服務端建立連接
            myClient.connect();
            while (!myClient.getReadyState().equals(ReadyState.OPEN)) {
                System.out.println("連接中。。。");
                Thread.sleep(1000);
            }
            // 往websocket服務端發(fā)送數(shù)據(jù)
            myClient.send("發(fā)送來自websocketClient 123333的消息");
            Thread.sleep(1000);
            // 關(guān)閉與服務端的連接
            // myClient.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        // write your code here

    }
}

實際開發(fā)使用:

package org.jeecg.modules.food.job;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.constant.WebsocketConst;
import org.jeecg.modules.food.entity.DailyMenu;
import org.jeecg.modules.food.mapper.DailyMenuMapper;
import org.jeecg.modules.message.websocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Auther: wsf
 * @Date: 2023/5/30 15:52
 * @Description:定時每天10、14點定時推送訂餐消息
 */
@Component
public class DailyMenuTask {

    @Resource
    private WebSocket webSocket;

    @Autowired
    private DailyMenuMapper dailyMenuMapper;

    @Scheduled(cron = "0 0 10,14 * * ?")
    public void pushDailyMenu() {
        DailyMenu dailyMenu = new DailyMenu();
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE, 1);
        Date time = cal.getTime();
        dailyMenu.setFoodDate(time);
        List<DailyMenu> dailyMenus = dailyMenuMapper.selectByDate(dailyMenu);
        if (CollectionUtils.isNotEmpty(dailyMenus)) {
            //創(chuàng)建業(yè)務消息信息
            JSONObject obj = new JSONObject();
            // 業(yè)務類型
            obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_TOPIC);
            obj.put(WebsocketConst.MSG_ID, dailyMenus.get(0).getId());
            obj.put(WebsocketConst.MSG_TXT, "訂餐發(fā)布");
            //全體發(fā)送
            webSocket.sendAllMessage(obj.toJSONString());
        }
    }
}

java 實現(xiàn)websocket的方式有很多種

  • java 實現(xiàn)websocket一般兩種方式,一種使用tomcat的websocket實現(xiàn),比如上述使用這種方式無需別的任何配置,只需服務端一個處理類;
  • 使用spring的websocket,spring與websocket整合需要spring 4.x,并且使用了socketjs,對不支持websocket的瀏覽器可以模擬websocket使用。實現(xiàn)WebSocketConfigurer接口的registerWebSocketHandlers方法加入自己WebSocketHandler接口的實現(xiàn)類。
  • 使用spring stomp封裝的方法,實現(xiàn)更簡單,配置注入后通過@sendto就可向客戶端發(fā)消息。
  • 等等

總結(jié) 

到此這篇關(guān)于java中Websocket的使用方法的文章就介紹到這了,更多相關(guān)java Websocket使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Json 自定義使用函數(shù)的簡單實例

    Json 自定義使用函數(shù)的簡單實例

    下面小編就為大家?guī)硪黄狫son 自定義使用函數(shù)的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • Springboot 整合 Dubbo/ZooKeeper 實現(xiàn) SOA 案例解析

    Springboot 整合 Dubbo/ZooKeeper 實現(xiàn) SOA 案例解析

    這篇文章主要介紹了Springboot 整合 Dubbo/ZooKeeper 詳解 SOA 案例,需要的朋友可以參考下
    2017-11-11
  • java繪制五子棋棋盤

    java繪制五子棋棋盤

    這篇文章主要為大家詳細介紹了java繪制五子棋棋盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置問題及解決方法

    SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置問題及解決方法

    這篇文章主要介紹了SpringBoot多數(shù)據(jù)源讀寫分離的自定義配置,我們可以通過自定義配置數(shù)據(jù)庫配置類來解決這個問題,方式有很多,不同的業(yè)務采用的方式也不同,下面我簡單的介紹我們項目的使用的方法
    2022-06-06
  • 基于hibernate框架在eclipse下的配置方法(必看篇)

    基于hibernate框架在eclipse下的配置方法(必看篇)

    下面小編就為大家?guī)硪黄趆ibernate框架在eclipse下的配置方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Java中實現(xiàn)String字符串用逗號隔開

    Java中實現(xiàn)String字符串用逗號隔開

    這篇文章主要介紹了Java中實現(xiàn)String字符串用逗號隔開,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java?鏈表實戰(zhàn)真題訓練

    Java?鏈表實戰(zhàn)真題訓練

    跟著思路走,之后從簡單題入手,反復去看,做過之后可能會忘記,之后再做一次,記不住就反復做,反復尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化
    2022-04-04
  • 逆波蘭計算器(Java實現(xiàn))

    逆波蘭計算器(Java實現(xiàn))

    這篇文章主要為大家詳細介紹了Java實現(xiàn)逆波蘭計算器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • itext生成PDF設(shè)置頁眉頁腳的實例詳解

    itext生成PDF設(shè)置頁眉頁腳的實例詳解

    這篇文章主要介紹了itext生成PDF設(shè)置頁眉頁腳的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • MyBatis XPathParser解析器使用范例詳解

    MyBatis XPathParser解析器使用范例詳解

    這篇文章主要介紹了關(guān)于MyBatis中解析器XPathParser的實際使用實踐,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2022-07-07

最新評論