欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于Spring Boot WebSocket整合以及nginx配置详解

程序员文章站 2024-02-28 16:19:04
前言 本文主要给大家介绍了关于spring boot websocket整合及nginx配置的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。...

前言

本文主要给大家介绍了关于spring boot websocket整合及nginx配置的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

一:spring boot websocket整合

创建一个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 内容如下:

server.port=9080 
spring.resources.static-locations=classpath:/webapp/html/ 

src/main/resources/webapp/html/index.html  内容如下:

<!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 通过在客户端和后端服务器之间建立起一条隧道来支持websocket。

为了使nginx可以将来自客户端的upgrade请求发送给后端服务器,upgrade和connection的头信息必须被显式的设置。如下所示:

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

一旦我们完成以上设置,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的错误

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。