亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關(guān)于Spring Boot WebSocket整合以及nginx配置詳解

 更新時間:2017年09月07日 11:40:40   作者:西夏一品堂  
這篇文章主要給大家介紹了關(guān)于Spring Boot WebSocket整合以及nginx配置的相關(guān)資料,文中通過示例代碼給大家介紹的非常詳細,相信對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)下吧。

前言

本文主要給大家介紹了關(guān)于Spring Boot WebSocket整合及nginx配置的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細的介紹吧。

一:Spring Boot WebSocket整合

創(chuàng)建一個maven項目,加入如下依賴

<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> 

最后,啟動main方法,訪問http://127.0.0.1:9080/index.html即可看到輸出

二:nginx配置

nginx 通過在客戶端和后端服務(wù)器之間建立起一條隧道來支持WebSocket。

為了使nginx可以將來自客戶端的Upgrade請求發(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;   這個配置

否則,會報:WebSocket connection to 'ws://192.168.1.104:9080/web/count' failed: Error during WebSocket handshake: Unexpected response code: 403的錯誤

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • idea中Maven鏡像源詳細配置步驟記錄(對所有項目)

    idea中Maven鏡像源詳細配置步驟記錄(對所有項目)

    Maven是一個能使我們的java程序開發(fā)節(jié)省時間和精力,是開發(fā)變得相對簡單,還能使開發(fā)規(guī)范化的工具,下面這篇文章主要給大家介紹了關(guān)于idea中Maven鏡像源詳細配置(對所有項目)的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • Java基礎(chǔ)教程之接口的繼承與抽象類

    Java基礎(chǔ)教程之接口的繼承與抽象類

    這篇文章主要介紹了Java基礎(chǔ)教程之接口的繼承與抽象類,本文介紹了接口繼承、接口的多重繼承以及抽象類的知識,需要的朋友可以參考下
    2014-09-09
  • Lombok中@EqualsAndHashCode注解的使用及說明

    Lombok中@EqualsAndHashCode注解的使用及說明

    這篇文章主要介紹了Lombok中@EqualsAndHashCode注解的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SpringBoot 二維碼生成base64并上傳OSS的實現(xiàn)示例

    SpringBoot 二維碼生成base64并上傳OSS的實現(xiàn)示例

    本文主要介紹了SpringBoot 二維碼生成base64并上傳OSS的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • spring data jpa使用詳解(推薦)

    spring data jpa使用詳解(推薦)

    這篇文章主要介紹了spring data jpa使用詳解(推薦),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Java對象的內(nèi)存布局全流程

    Java對象的內(nèi)存布局全流程

    這篇文章主要介紹了Java對象的內(nèi)存布局全流程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 關(guān)于SpringBoot mysql數(shù)據(jù)庫時區(qū)問題

    關(guān)于SpringBoot mysql數(shù)據(jù)庫時區(qū)問題

    在后端開發(fā)過程中經(jīng)常會遇到幾個時區(qū)設(shè)置問題,今天分幾種情況給大家介紹SpringBoot mysql數(shù)據(jù)庫時區(qū)問題,感興趣的朋友跟隨小編一起看看吧
    2021-06-06
  • Java Spring快速入門

    Java Spring快速入門

    本文主要介紹了SpringSpring簡介和入門知識。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • 詳解java中if語句和switch的使用

    詳解java中if語句和switch的使用

    這篇文章主要介紹了java中if語句和switch的使用,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • SpringBoot整合SpringBoot-Admin實現(xiàn)監(jiān)控應(yīng)用功能

    SpringBoot整合SpringBoot-Admin實現(xiàn)監(jiān)控應(yīng)用功能

    本文主要介紹如何整合Spring Boot Admin,以此監(jiān)控Springboot應(yīng)用,文中有相關(guān)的示例代碼供大家參考,需要的朋友可以參考下
    2023-05-05

最新評論