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

SpringBoot集成WebSocket的兩種方式(JDK內(nèi)置版和Spring封裝版)

 更新時(shí)間:2023年06月14日 15:36:31   作者:haiyangyiba  
這篇文章主要介紹了SpringBoot集成WebSocket的兩種方式,這兩種方式為JDK內(nèi)置版和Spring封裝版,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

本站在2014年4月時(shí)曾全面的學(xué)習(xí)HTML5的技術(shù),特寫過HTML5的WebSocket示例,當(dāng)時(shí)使用的Servlet3.0規(guī)范中的API,需要Tomcat7的支持(貌似在Tomcat6的后期維護(hù)版本也增加了WebSocket的支持),早在當(dāng)初該示例還是本站的一個(gè)特色功能,好多來找我要源碼的呢。時(shí)隔多年再來使用SpringBoot架構(gòu)來體驗(yàn)一下集成WebSocket的實(shí)現(xiàn),經(jīng)過一番資料的百科大概有找到使用兩種方式的實(shí)現(xiàn),我分別對(duì)它們進(jìn)行了實(shí)踐,所以我稱這兩種方式為JDK內(nèi)置版和Spring封裝版。

1.JDK內(nèi)置版

主要是使用javax.websocket包下的注解進(jìn)行集成,主要有:ServerEndpoint、OnOpen、OnMessage、OnClose、OnError相關(guān)的類和注解來集成,整體上比較簡(jiǎn)單,都是基于注解定義的方法來聲明的,參考如下代碼所示:

JdkWebSocket

package cn.chendd.websocket.jdk;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
/**
 * websocket實(shí)現(xiàn)
 *
 * @date 2023/6/2 21:02
 */
@Component
@ServerEndpoint(value = "/websocket/jdk")
public class JdkWebSocket {
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("WebSocketConfig.onOpen");
    }
    @OnMessage
    public void onMessage(Session session , String message) {
        System.out.println("WebSocketConfig.onMessage-->" + session + "--->" + message);
    }
    @OnClose
    public void onClose() {
        System.out.println("WebSocketConfig.onClose");
    }
    @OnError
    public void onError(Session sesison , Throwable throwable) {
        System.out.println("WebSocketConfig.onError-->" + sesison + "--->" + throwable);
    }
}

JdkWebSocketConfig

package cn.chendd.websocket.jdk;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
 * @author chendd
 * @date 2023/6/2 21:15
 */
@Configuration
public class JdkWebSocketConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter(){
        return new ServerEndpointExporter();
    }
}

運(yùn)行結(jié)果

2.Spring封裝版

Spring封裝版本有對(duì)于消息類型進(jìn)行封裝,提供了更加全面的WebSocket方面的API,值得深入分析。

SpringWebSocketHandler

package cn.chendd.websocket.spring;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.*;
import org.springframework.web.socket.handler.TextWebSocketHandler;
/**
 * SpringWebSocketHandler
 *
 * @author chendd
 * @date 2023/6/2 22:08
 */
@Component
public class SpringWebSocketHandler extends TextWebSocketHandler {
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        super.afterConnectionEstablished(session);
        System.out.println("SpringWebSocketHandler.afterConnectionEstablished");
    }
    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        super.handleMessage(session, message);
        System.out.println("SpringWebSocketHandler.handleMessage");
    }
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        super.handleTextMessage(session, message);
        System.out.println("SpringWebSocketHandler.handleTextMessage");
    }
    @Override
    protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception {
        super.handlePongMessage(session, message);
        System.out.println("SpringWebSocketHandler.handlePongMessage");
    }
    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        super.handleTransportError(session, exception);
        System.out.println("SpringWebSocketHandler.handleTransportError");
    }
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        super.afterConnectionClosed(session, status);
        System.out.println("SpringWebSocketHandler.afterConnectionClosed");
    }
    @Override
    public boolean supportsPartialMessages() {
        System.out.println("SpringWebSocketHandler.supportsPartialMessages");
        return super.supportsPartialMessages();
    }
}

SpringWebSocketConfig

package cn.chendd.websocket.spring;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import javax.annotation.Resource;
/**
 * SpringWebSocketConfig
 *
 * @author chendd
 * @date 2023/6/2 22:11
 */
@Configuration
@EnableWebSocket
public class SpringWebSocketConfig implements WebSocketConfigurer {
    @Resource
    private SpringWebSocketHandler springWebSocketHandler;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(springWebSocketHandler , "/websocket/spring").setAllowedOrigins("*");
    }
}

運(yùn)行結(jié)果

3.其它說明

(1)本次示例重點(diǎn)在于服務(wù)端Java代碼的示例,未編寫前端示例,可在下方的項(xiàng)目源碼中參見前端的HTML集成,或者是使用本例中使用的在線WebSocket測(cè)試的頁面地址進(jìn)行在線驗(yàn)證,右鍵查看其源代碼也是原生的HTML5規(guī)范的相關(guān)代碼;

(2)提供了JDK的內(nèi)置版本和Spring的封裝版本,個(gè)人推薦使用Spring的封裝版,畢竟JDK的注解不夠清晰的描述出被注解的方法的方法具體參數(shù),而Spring封裝版本有對(duì)于消息類型進(jìn)行封裝;

(3)代碼比較簡(jiǎn)單,結(jié)合前文的參考鏈接中的全量代碼也包含在內(nèi),代碼包結(jié)構(gòu)如下:

參考文章鏈接:一個(gè)猜你所選的小程序隨寫

到此這篇關(guān)于SpringBoot集成WebSocket的兩種方式的文章就介紹到這了,更多相關(guān)SpringBoot集成WebSocket內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論