通过springBoot集成搭建webScoket服务器
前言:
最近工作中有一个需求,就是服务端要主动推送消息给客户端,而我们平常的Http请求只能一请求一响应,为此学习了webScokset通讯技术,以下介绍的是java 通过SpringBoot集成webScoket搭建服务端。
1.引入webScoket依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
2.bean注入
- ServerEndpointExporter 这个Bean会自动注册使用@ServerEndpoint注解声明的websocket endpoint
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
3.创建scoket的Server服务
创建一个java类 WebScoketServer,以下为我自己写的一个小demo,实现的就是服务端和客户端一个对话(客户端没有搭建用的网页测试),效果我会在下面测试演示。
其他具体业务逻辑在连接成功或者收到信息后方法里进行处理
@ServerEndpoint("/webSocket/{username}")
@Component
@Slf4j
public class WebScoketServer {
//concurrent包的线程安全Set,用来存放每个客户端对应的WebSocketServer对象。
private static ConcurrentHashMap<String, Session> sessionPools = new ConcurrentHashMap<>();
private static Map<Session, List<String>> map =new HashMap<>();
//发送消息
public void sendMessage(Session session, String message) throws IOException {
synchronized (session) {
System.out.println("发送数据:" + message);
session.getBasicRemote().sendText(message);
}
}
//建立连接成功调用
@OnOpen
public void onOpen(Session session, @PathParam(value = "username") String userName){
log.info("连接建立 userName是{}",userName);
sessionPools.put(userName,session);
List<String> list =new ArrayList<>();
list.add("服务端--》哈哈哈建立连接后的回复 ");
map.put(session,list);
try {
sendMessage(session,list.toString());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("连接建立");
}
//关闭连接时调用
@OnClose
public void onClose(@PathParam(value = "username") String userName){
log.info("连接关闭 userName是{}",userName);
System.out.println("连接关闭");
}
//收到客户端信息后
@OnMessage
public void onMessage(String message,@PathParam(value = "username") String userName) throws IOException{
log.info("收到消息 message是{}",message);
try {
Session session = sessionPools.get(userName);
List<String> list = map.get(session);
list.add("客户端--》"+message+" ");
list.add("服务端--》我收到消息:"+message+" ");
map.put(session,list);
sendMessage(session,list.toString());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("收到消息");
}
//错误时调用
@OnError
public void onError(Session session, Throwable throwable){
log.info("连接错误");
System.out.println("连接错误");
}
}
4.测试
通过springBoot集成搭建webScoket服务器的更多相关文章
- SpringBoot+Mybatis集成搭建
本博客介绍一下SpringBoot集成Mybatis,数据库连接池使用alibaba的druid,使用SpringBoot微框架虽然集成Mybatis之后可以不使用xml的方式来写sql,但是用惯了x ...
- SpringBoot集成RabbitMQ消息队列搭建与ACK消息确认入门
1.RabbitMQ介绍 RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.Rabbi ...
- 持续集成之二:搭建SVN服务器(subversion)
安装环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) jdk1.7.0_80 subversion-1.10.3.tar.gz apr-1. ...
- idea 搭建 SpringBoot 集成 mybatis
编译器:IDEA2018.2.3 环境:win10,jdk1.8,maven3.4 数据库:mysql 5.7 备注:截图较大,如果看不清,可以在图片上右键=>在新标签页中打开 查看高清大图 ...
- Windows/Linux 环境搭建Git服务器 + vs2012集成git
1. 下载.安装Git 我的系统是Windows 7,需要安装Git for Windows. 下载地址: http://code.google.com/p/msysgit/downloads/lis ...
- SpringBoot集成ssm-druid-通用mapper
简单介绍 springboot 首先什么是springboot? springboot是spring的另外一款框架,设计目的是用来简化新的spring应用的搭建和开发时所需要的特定的配置,从而使开发过 ...
- 自己家里搭建NAS服务器有什么好方案?
转自:https://www.zhihu.com/question/21359049 作者:陈二发链接:https://www.zhihu.com/question/21359049/answer/6 ...
- Windows Server 2003搭建邮件服务器
Windows Server 2003搭建邮件服务器 由于Windows Server 2003默认是没有安装我们搭建邮件服务器所需要的POP3和SMTP服务的,因此需要我们自己来安装.方法如下: 1 ...
- LUA+resty 搭建验证码服务器
使用Lua和OpenResty搭建验证码服务器 雨客 2016-04-08 16:38:11 浏览2525 评论0 云数据库Redis版 摘要: Lua下有个Lua-GD图形库,通过简单的Lua语句就 ...
随机推荐
- iOS开发——密码存储之keychain的使用
iOS的keychain服务提供了一种安全的保存私密信息(密码,序列号,证书等)的方式.每个ios程序都有一个独立的keychain存储.从ios 3.0开始,跨程序分享keychain变得可行. 下 ...
- Sql中的小技巧
1.where 字段名 regexp '正则表达式' 正则符号: ^ $ . [ ] * | . 表示1个任意字符 * 表示前面重复0次,或者任意次 ^ 开始 $ 结尾 [] 范围 | 或 sql示例 ...
- 去除指定….RemoveMatching…(Power Query 之 M 语言)
表去除指定行: =Table.RemoveMatchingRows( 表, 列表, "指定列") 表中指定列中与列表中相同的行会被去除 表只有一列时,第三参数可以缺省 示例1:&q ...
- 关于Marshal 类的整理
在两个不同的实体(两个线程或者进程甚至机器.在Managed和Unmanaged之间)进行方法调用和参数传递的时候,具体的调用方法和参数的内存格式可能需要一定的转换,这个转换的过程叫做Marshal. ...
- MySQL 报错:[Err] 1071 - Specified key was too long; max key length is 767 bytes
[Err] 1071 - Specified key was too long; max key length is 767 bytes 这个会出现在MySQ5.7以下版本 因为没有启用innodb_ ...
- summernote富文本图片上传,增加视频上传功能、批量上传方法
Summernote 是一个简单灵活的所见即所得的 HTML 在线编辑器,基于 jQuery 和 Bootstrap 构建,支持快捷键操作,提供大量可定制的选项. 但是却只有图片上传功能,没有视频上传 ...
- Android 控件使用教程(二)—— RecyclerView 展示图片
简介 在上一篇博文中,介绍了大家已经很熟悉的布局控件ListView,在这篇文章中,我将使用比较新.功能也更强大的RecyclerView. RecyclerView 首先,要用这个控件,你需要在gr ...
- 【LeetCode】758. Bold Words in String 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】1029. Two City Scheduling 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 排序 日期 题目地址:https://lee ...
- leetcode1261在受污染的二叉树中查找元素
题目 一颗二叉树,树根值为0,父节点为x,则左子值为2x+1,右子为2x+2.现在只有树的结构,所有值都变为-1被污染了.求污染前是否存在某个值. 构建一次树,查询会调用多次. 题解 这道题还是比较简 ...