关于SpringBootWebSocket整合以及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) + "位访客"));
}
springframework和springboot}
package com.wh.web;
import t.annotation.Configuration;
import org.springframework.fig.annotation.WebSocketConfigurer;
import org.springframework.fig.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.fig.annotation.EnableWebSocket;
@EnableWebSocket
@SpringBootApplication
public class ServerApp {
public static void main(String[] args) {
SpringApplication.run(ServerApp.class, args);
}
}
application.properties 内容如下:
server.port=9080
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.send('hello');
};
alert(event.data);
};
alert(event);
}
</script>
</body>
</html>
⼆:nginx配置
nginx 通过在客户端和后端服务器之间建⽴起⼀条隧道来⽀持WebSocket。
为了使nginx可以将来⾃客户端的Upgrade请求发送给后端服务器,Upgrade和Connection的头信息必须被显式的设置。如下所⽰:
location /web/count {
proxy_pass 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的错误
总结
以上就是这篇⽂章的全部内容了,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,如果有疑问⼤家可以留⾔交流,谢谢⼤家对的⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论