關(guān)于Spring Boot WebSocket整合以及nginx配置詳解
前言
本文主要給大家介紹了關(guān)于Spring Boot WebSocket整合及nginx配置的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
一:Spring Boot WebSocket整合
創(chuàng)建一個(gè)maven項(xiàng)目,加入如下依賴
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.4.0.RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <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> </dependency> </dependencies>
代碼如下:
package com.wh.web;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
public class CountWebSocketHandler extends TextWebSocketHandler {
private static long count = 0;
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
session.sendMessage(new TextMessage("你是第" + (++count) + "位訪客"));
}
}
package com.wh.web;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
@Configuration
public class WebsocketConfiguration implements WebSocketConfigurer {
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new CountWebSocketHandler(), "/web/count");
}
}
package com.wh.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
@EnableWebSocket
@SpringBootApplication
public class ServerApp {
public static void main(String[] args) {
SpringApplication.run(ServerApp.class, args);
}
}
application.properties 內(nèi)容如下:
server.port=9080 spring.resources.static-locations=classpath:/webapp/html/
src/main/resources/webapp/html/index.html 內(nèi)容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>web socket</title>
</head>
<body>
<h1>web socket</h1>
<script type="text/javascript">
var url = 'ws://'+window.location.hostname+':9080/web/count';
var ws = new WebSocket(url);
ws.onopen = function(event)
{
ws.send('hello');
};
ws.onmessage = function(event) {
alert(event.data);
};
ws.onerror = function(event) {
alert(event);
}
</script>
</body>
</html>
最后,啟動(dòng)main方法,訪問http://127.0.0.1:9080/index.html即可看到輸出
二:nginx配置
nginx 通過(guò)在客戶端和后端服務(wù)器之間建立起一條隧道來(lái)支持WebSocket。
為了使nginx可以將來(lái)自客戶端的Upgrade請(qǐng)求發(fā)送給后端服務(wù)器,Upgrade和Connection的頭信息必須被顯式的設(shè)置。如下所示:
location /web/count {
proxy_pass http://tomcat-server;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
一旦我們完成以上設(shè)置,nginx就可以處理WebSocket連接了。
注意:必須要有 proxy_set_header Host $host:$server_port; 這個(gè)配置
否則,會(huì)報(bào):WebSocket connection to 'ws://192.168.1.104:9080/web/count' failed: Error during WebSocket handshake: Unexpected response code: 403的錯(cuò)誤
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java排序算法三之歸并排序的遞歸與非遞歸的實(shí)現(xiàn)示例解析
這篇文章主要介紹了Java排序算法三之歸并排序的遞歸與非遞歸的實(shí)現(xiàn)示例解析,文章通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
SpringBoot實(shí)現(xiàn)列表數(shù)據(jù)導(dǎo)出為Excel文件
這篇文章主要為大家詳細(xì)介紹了在Spring?Boot框架中如何將列表數(shù)據(jù)導(dǎo)出為Excel文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-02-02
java實(shí)現(xiàn)將數(shù)字轉(zhuǎn)換成人民幣大寫
前面給大家介紹過(guò)使用javascript,php,c#,python等語(yǔ)言實(shí)現(xiàn)人民幣大寫格式化,這篇文章主要介紹了java實(shí)現(xiàn)將數(shù)字轉(zhuǎn)換成人民幣大寫的代碼,非常的簡(jiǎn)單實(shí)用,分享給大家,需要的朋友可以參考下2015-04-04
SpringBoot程序的打包與運(yùn)行的實(shí)現(xiàn)
本文主要介紹了SpringBoot程序的打包與運(yùn)行的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Java/Android 獲取網(wǎng)絡(luò)重定向文件的真實(shí)URL的示例代碼
本篇文章主要介紹了Java/Android 獲取網(wǎng)絡(luò)重定向文件的真實(shí)URL的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Hystrix?Turbine聚合監(jiān)控的實(shí)現(xiàn)詳解
微服務(wù)架構(gòu)下,?個(gè)微服務(wù)往往部署多個(gè)實(shí)例,如果每次只能查看單個(gè)實(shí)例的監(jiān)控,就需要經(jīng)常切換很不?便,在這樣的場(chǎng)景下,我們可以使??Hystrix?Turbine?進(jìn)?聚合監(jiān)控,它可以把相關(guān)微服務(wù)的監(jiān)控?cái)?shù)據(jù)聚合在?起,便于查看2022-09-09

