This article is mainly to introduce the relevant information on the integration of Spring Boot WebSocket and nginx configuration, the paper sample code to tell you in great detail, I believe has a certain reference value of learning for all of us to learn or work, you need friends below with the small series learning to learn together under the bar.
Foreword
This paper to introduce relevant content on Spring Boot WebSocket integration and nginx configuration to share for your reference study, birthdates named below the word did not talk much to say, to take a look at the detailed introduction.
A: Spring Boot WebSocket integration
Create a maven project, add the following dependence
org.springframework.boot spring-boot-dependencies 1.4.0.RELEASE import pom org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-websocket
代码如下:
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 内容如下: