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

这篇文章主要给大家介绍了关于Spring Boot WebSocket整合以及nginx配置的相关资料,文中通过示例代码给大家介绍的非常详细,相信对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习下吧。
前言

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

一:Spring Boot WebSocket整合

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

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

web socket

web socket

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值