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

2025-05-29 0 84

前言

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

一:Spring Boot WebSocket整合

创建一个maven项目,加入如下依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

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

代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13
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) + "位访客"));

}

}

?

1

2

3

4

5

6

7

8

9

10

11

12
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");

}

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13
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 内容如下:

?

1

2
server.port=9080

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26
<!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的头信息必须被显式的设置。如下所示:

?

1

2

3

4

5

6

7

8

9

10
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的错误

总结

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

原文链接:http://blog.csdn.net/mn960mn/article/details/52184937

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 关于Spring Boot WebSocket整合以及nginx配置详解 https://www.kuaiidc.com/114913.html

相关文章

发表评论
暂无评论