All Projects → dadiyang → Springws

dadiyang / Springws

Licence: mit
基于SpringMVC实现的WebSocket工程实例

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Springws

Webfluxtemplate
Spring Webflux template application with working Spring Security, Web-sockets, Rest, Web MVC, and Authentication with JWT.
Stars: ✭ 107 (+205.71%)
Mutual labels:  spring, websockets
Brutusin Rpc
Self-describing JSON-RPC web services over HTTP, with automatic API description based on JSON-Schema
Stars: ✭ 36 (+2.86%)
Mutual labels:  spring, websockets
Spring Reactive Sample
Spring 5 Reactive playground
Stars: ✭ 867 (+2377.14%)
Mutual labels:  spring
Sockpuppet
Having fun with WebSockets, Python, Golang and nytimes.com
Stars: ✭ 32 (-8.57%)
Mutual labels:  websockets
Spring Depend
Tool for getting a spring bean dependency graph
Stars: ✭ 28 (-20%)
Mutual labels:  spring
Ssm redis template
An template based on Maven, using Spring + Spring MVC + mybatis + spring-data-redis frames. It can be used to construct a new Java Web Appliaction quickly
Stars: ✭ 13 (-62.86%)
Mutual labels:  spring
Real Time Chat Frontend
Chat Frontend developed with React
Stars: ✭ 30 (-14.29%)
Mutual labels:  websockets
Spring Cloud Examples
Examples of microservice instrastructures
Stars: ✭ 11 (-68.57%)
Mutual labels:  spring
Novnc
VNC client web application
Stars: ✭ 8,269 (+23525.71%)
Mutual labels:  websockets
Websocket Classloader
Load remote java classes via WebSocket.
Stars: ✭ 21 (-40%)
Mutual labels:  websockets
Carrot
🥕 Build multi-device AR applications
Stars: ✭ 32 (-8.57%)
Mutual labels:  websockets
Angular7 Springboot Crud Tutorial
Develop a single page application(SPA) using Angular 7 as a front-end and Spring boot restful API as a backend.
Stars: ✭ 21 (-40%)
Mutual labels:  spring
Spring Mvc Thymeleaf Crud
Spring MVC CRUD Application with Thymeleaf, HTML5, CSS3 and Bootstrap
Stars: ✭ 14 (-60%)
Mutual labels:  spring
Intrinio Realtime Node Sdk
Intrinio NodeJS SDK for Real-Time Stock & Crypto Prices
Stars: ✭ 30 (-14.29%)
Mutual labels:  websockets
Tiny Spring
本项目是一个精简版的spring,通过自己实现一遍spring来理解spring框架的精华。
Stars: ✭ 13 (-62.86%)
Mutual labels:  spring
Javabrainmap
Java 体系涉及到的各方面知识点脑图总结,万物皆脑图。The knowledge of all aspects of the Java system is summarized in the brain map.
Stars: ✭ 33 (-5.71%)
Mutual labels:  spring
Streamsx.inet
This toolkit supports common internet protocols, such as HTTP and WebSockets
Stars: ✭ 11 (-68.57%)
Mutual labels:  websockets
Ruoyi
(RuoYi)官方仓库 基于SpringBoot的权限管理系统 易读易懂、界面简洁美观。 核心技术采用Spring、MyBatis、Shiro没有任何其它重度依赖。直接运行即可用
Stars: ✭ 905 (+2485.71%)
Mutual labels:  spring
Storm Camel Example
Real-time analysis and visualization with Storm-AMQ-Camel-Websockets-Highcharts integration.
Stars: ✭ 28 (-20%)
Mutual labels:  websockets
Spring framework
Examples on Spring(Core, AOP, DAO, Transaction Management)
Stars: ✭ 34 (-2.86%)
Mutual labels:  spring

Spring-ws

基于SpringMVC实现的支持SockJS的WebSocket工程实例

说明

本项目为maven项目,使用SpringMVC实现,用于展示如果使用 SockJS。项目启动后直接打开对应的端口在首页就可以看到展示的功能。

后端在"/echo"上添加了WebSocket处理器,它会在客户端传过来的消息前面加上 "Echo:" 然后传回去。

而页面会在打开的时候连接后端的WebSocket服务,连接之后每两秒发送一个 "Hey guy!" 的消息。

基于 SockJS 的 WebSocket

在学习 《Spring实战》 第18章的时候才知道,原来在不支持WebSocket的情况下,也可以很简单地实现WebSocket的功能的,方法就是使用 SockJS。它会优先选择WebSocket进行连接,但是当服务器或客户端不支持WebSocket时,会自动在 XHR流、XDR流、iFrame事件源、iFrame HTML文件、XHR轮询、XDR轮询、iFrame XHR轮询、JSONP轮询 这几个方案中择优进行连接。

幸运的是,我们不需要知道这些方案都代表什么,只需要简单地设置就可以使用了

服务端

在启动WebSocket的配置中,你需要做的所有事情就是加上 withSockJS()

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
    // withSockJS 声明启用支持 sockJS
    webSocketHandlerRegistry.addHandler(echoHandler(), "/echo").withSockJS();
}

客户端

在客户端需要引入SockJS库,然后把 new WebSocket(url); 替换成 new SockJS(url);

SockJS类和WebSocket类是兼容的,所以可以直接替换

<script type="text/javascript" src="/resources/js/sockjs-1.0.0.min.js"></script>
var sock = new SockJS(url);

需要做的事情就是这么多

效果展示

支持WebSocket

当浏览器和服务器都支持 websocket 的时候,直接使用websocket连接

正常情况

不支持WebSocket

不支持WebSocket的场景有:

  1. 浏览器不支持
  2. Web容器不支持,如tomcat7以前的版本不支持WebSocket
  3. 防火墙不允许
  4. Nginx没有开启WebSocket支持

当遇到不支持WebSocket的情况时,SockJS会尝试使用其他的方案来连接,刚开始打开的时候因为需要尝试各种方案,所以会阻塞一会儿,之后可以看到连接有异常,那就是尝试失败的情况。

为了测试,我使用Nginx做反向代理,把www.test.com指到项目启动的端口上,然后本地配HOST来达到模拟真实场景的效果。因为Nginx默认是不支持WebSocket的,所以这里模拟出了服务器不支持WebSocket的场景。

不支持WebSocket

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].