最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 整合單機(jī)websocket的步驟 附github源碼

 更新時間:2021年10月08日 11:14:49   作者:jeremylai  
websocket 是一個通信協(xié)議,通過單個 TCP 連接提供全雙工通信,這篇文章主要介紹了Spring Boot 整合單機(jī)websocket的步驟(附github源碼),需要的朋友可以參考下

websocket 概念

websocket 是一個通信協(xié)議,通過單個 TCP 連接提供全雙工通信。websocket 連接成功后,服務(wù)端和客戶可以進(jìn)行雙向通信。不同于 http 通信協(xié)議需要每次由客戶端發(fā)起,服務(wù)響應(yīng)到客戶端。
websocket 相對輪詢也能節(jié)約帶寬,并且能實時的進(jìn)行通信。

整合步驟

1. 添加 maven 依賴

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-websocket</artifactId>
	<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
        <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-freemarker</artifactId>
	<version>2.1.3.RELEASE</version>
</dependency>

添加web、websocket和freemarker依賴。

2. 使用 ServerEndpointExporter 創(chuàng)建 bean

這個 bean 會自動注冊聲明 @ServerEndpoint 注解聲明的 websocket endpoint,使用springboot自帶tomcat啟動需要該配置,使用獨立 tomcat 則不需要該配置。

@Configuration
public class WebSocketConfig {
    //tomcat啟動無需該配置
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}

3. 創(chuàng)建服務(wù)端端點 (ServerEndpoint)

@Component
@ServerEndpoint(value = "/message")
@Slf4j
public class WebSocket {

	private static Map<String, WebSocket> webSocketSet = new ConcurrentHashMap<>();

	private Session session;

	@OnOpen
	public void onOpen(Session session) throws SocketException {
		this.session = session;
		webSocketSet.put(this.session.getId(),this);
		log.info("【websocket】有新的連接,總數(shù):{}",webSocketSet.size());
	}

	@OnClose
	public void onClose(){
		String id = this.session.getId();
		if (id != null){
			webSocketSet.remove(id);
			log.info("【websocket】連接斷開:總數(shù):{}",webSocketSet.size());
		}
	}

	@OnMessage
	public void onMessage(String message){
		if (!message.equals("ping")){
			log.info("【wesocket】收到客戶端發(fā)送的消息,message={}",message);
			sendMessage(message);
		}
	}

	/**
	 * 發(fā)送消息
	 * @param message
	 * @return 全部都發(fā)送一遍
	 */
	public void sendMessage(String message){
		for (WebSocket webSocket : webSocketSet.values()) {
			webSocket.session.getAsyncRemote().sendText(message);
		}
		log.info("【wesocket】廣播消息,message={}", message);

	}

}

4. 添加 controller 和 客戶端

添加 controller

@GetMapping({"","index.html"})
public ModelAndView index() {
	ModelAndView view = new ModelAndView("index");
	return view;
}

添加ftl頁面

<!DOCTYPE html>
<html>
<head>
    <title>websocket</title>
</head>
<body>
<div>
    <input type="text" name="message" id="message">
    <button id="sendBtn">發(fā)送</button>
</div>
<div style="width:100px;height: 500px;" id="content">
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script type="text/javascript">
    var ws = new WebSocket("ws://127.0.0.1:8080/message");
    ws.onopen = function(evt) {
        console.log("Connection open ...");
    };

    ws.onmessage = function(evt) {
        console.log( "Received Message: " + evt.data);
        var p = $("<p>"+evt.data+"</p>")
        $("#content").prepend(p);
        $("#message").val("");
    };

    ws.onclose = function(evt) {
        console.log("Connection closed.");
    };

    $("#sendBtn").click(function(){
        var aa = $("#message").val();
        ws.send(aa);
    })

</script>
</body>
</html>

5. 展示效果

附錄

github源碼

到此這篇關(guān)于Spring Boot 整合單機(jī)websocket 附github源碼的文章就介紹到這了,更多相關(guān)Spring Boot 整合websocket內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

安顺市| 南开区| 叶城县| 阿克苏市| 时尚| 渑池县| 绥阳县| 增城市| 深圳市| 陇西县| 云安县| 桐梓县| 尼木县| 眉山市| 郓城县| 曲麻莱县| 清水县| 乌兰察布市| 综艺| 永昌县| 平乡县| 当雄县| 安平县| 革吉县| 西藏| 襄城县| 泾源县| 和平县| 论坛| 株洲市| 麻城市| 东光县| 永福县| 顺昌县| 凤庆县| 潼关县| 安龙县| 台东县| 保亭| 池州市| 南投县|