最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
springboot websocket简单入门示例
时间:2018-08-22 编辑:猪哥 来源:一聚教程网
之前做的需求都是客户端请求服务器响应,新需求是服务器主动推送信息到客户端.百度之后有流、长轮询、websoket等方式进行.但是目前更加推崇且合理的显然是websocket.
从springboot官网翻译了一些资料,再加上百度简单实现了springboot使用websocekt与客户端的双工通信.
1.首先搭建一个简单的springboot环境
org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE org.springframework.boot spring-boot-starter-web
2.引入springboot整合websocket依赖
org.springframework.boot spring-boot-starter-websocket 2.0.4.RELEASE
3.创建启动springboot的核心类
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class GlobalConfig { public static void main(String[] args) { SpringApplication.run(GlobalConfig.class, args); } }
4.创建websocket服务器
正如springboot 官网推荐的websocket案例,需要实现WebSocketHandler或者继承TextWebSocketHandler/BinaryWebSocketHandler当中的任意一个.
package com.xiaoer.handler; import com.alibaba.fastjson.JSONObject; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; import java.util.HashMap; import java.util.Map; /** * 相当于controller的处理器 */ public class MyHandler extends TextWebSocketHandler { @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { String payload = message.getPayload(); Mapmap = JSONObject.parseObject(payload, HashMap.class); System.out.println("=====接受到的数据"+map); session.sendMessage(new TextMessage("服务器返回收到的信息," + payload)); } }
5.注册处理器
package com.xiaoer.config; import com.xiaoer.handler.MyHandler; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), "myHandler/{ID}"); } public WebSocketHandler myHandler() { return new MyHandler(); } }
6.运行访问
出现如上图是因为不能直接通过http协议访问,需要通过html5的ws://协议进行访问.
7.创建Html5 客户端
8.运行
利用客户端运行之后仍然会出现上图中的一连接就中断了websocket连接.
这是因为spring默认不接受跨域访问:
As of Spring Framework 4.1.5, the default behavior for WebSocket and SockJS is to accept only same origin requests.
需要在WebSocketConfig中设置setAllowedOrigins.
package com.xiaoer.config; import com.xiaoer.handler.MyHandler; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), "myHandler/{ID}") .setAllowedOrigins("*"); } public WebSocketHandler myHandler() { return new MyHandler(); } }
如下图,并未输出中断,说明连接成功.
9.服务器和客户端的相互通信
服务器端收到消息
客户端收到服务器主动推送消息
之前做的需求都是客户端请求服务器响应,新需求是服务器主动推送信息到客户端.百度之后有流、长轮询、websoket等方式进行.但是目前更加推崇且合理的显然是websocket.
从springboot官网翻译了一些资料,再加上百度简单实现了springboot使用websocekt与客户端的双工通信.
1.首先搭建一个简单的springboot环境
org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE org.springframework.boot spring-boot-starter-web
2.引入springboot整合websocket依赖
org.springframework.boot spring-boot-starter-websocket 2.0.4.RELEASE
3.创建启动springboot的核心类
package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class GlobalConfig { public static void main(String[] args) { SpringApplication.run(GlobalConfig.class, args); } }
4.创建websocket服务器
正如springboot 官网推荐的websocket案例,需要实现WebSocketHandler或者继承TextWebSocketHandler/BinaryWebSocketHandler当中的任意一个.
package com.xiaoer.handler; import com.alibaba.fastjson.JSONObject; import org.springframework.web.socket.TextMessage; import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.handler.TextWebSocketHandler; import java.util.HashMap; import java.util.Map; /** * 相当于controller的处理器 */ public class MyHandler extends TextWebSocketHandler { @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { String payload = message.getPayload(); Mapmap = JSONObject.parseObject(payload, HashMap.class); System.out.println("=====接受到的数据"+map); session.sendMessage(new TextMessage("服务器返回收到的信息," + payload)); } }
5.注册处理器
package com.xiaoer.config; import com.xiaoer.handler.MyHandler; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), "myHandler/{ID}"); } public WebSocketHandler myHandler() { return new MyHandler(); } }
6.运行访问
出现如上图是因为不能直接通过http协议访问,需要通过html5的ws://协议进行访问.
7.创建Html5 客户端
8.运行
利用客户端运行之后仍然会出现上图中的一连接就中断了websocket连接.
这是因为spring默认不接受跨域访问:
As of Spring Framework 4.1.5, the default behavior for WebSocket and SockJS is to accept only same origin requests.
需要在WebSocketConfig中设置setAllowedOrigins.
package com.xiaoer.config; import com.xiaoer.handler.MyHandler; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.WebSocketHandler; import org.springframework.web.socket.config.annotation.EnableWebSocket; import org.springframework.web.socket.config.annotation.WebSocketConfigurer; import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(myHandler(), "myHandler/{ID}") .setAllowedOrigins("*"); } public WebSocketHandler myHandler() { return new MyHandler(); } }
如下图,并未输出中断,说明连接成功.
9.服务器和客户端的相互通信
服务器端收到消息
客户端收到服务器主动推送消息
相关文章
- SpringBoot自定义bean绑定解析 10-24
- Javaweb工程运行报错HTTP Status 404解决教程 10-20
- JAVA获取jvm和操作系统相关信息方法 10-20
- BeanFactory和FactoryBean的区别讲解 10-20
- 微信小程序的宿主环境实现教程 10-10
- dispatchEvent解决重叠元素响应事件教程 10-10