通过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语句就 ...
随机推荐
- 严重危害警告!Log4j 执行漏洞被公开!
12 月 10 日凌晨,Apache 开源项目 Log4j2 的远程代码执行漏洞细节被公开,漏洞威胁等级为:严重. Log4j2 是一个基于 Java 的日志记录工具.它重写了 Log4j 框架,引入 ...
- 简单的理解 Object.defineProperty()
Object.defineProperty()的作用就是直接在一个对象上定义一个新属性,或者修改一个已经存在的属性. Object.defineProperty(obj,prop,descriptor ...
- Jaeger的客户端采样配置(Java版)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 基于MCRA-OMLSA的语音降噪(一):原理
前面的几篇文章讲了webRTC中的语音降噪.最近又用到了基于MCRA-OMLSA的语音降噪,就学习了原理并且软件实现了它.MCRA主要用于噪声估计,OMLSA是基于估计出来的噪声去做降噪.类比于web ...
- CF981B Businessmen Problems 题解
Content 有一个长度为 \(n\) 的序列和长度为 \(m\) 的序列,两个序列中的元素都有一个编号 \(num\) 和一个值 \(val\),且同一个序列的元素之间的编号互不相同.现在从这两个 ...
- 第二周Python笔记之 变量的三元运算
如果变量a小于b,则d的值取a变量的值,否则取c变量的值
- MAMP的使用
MAMP下载并安装 下载地址:https://pan.baidu.com/s/1TgoKBG3F59NGO8lEj9mf4Q 密码:2m3d 安装:按照提示,一直下一步直到完成 MAMP操作
- 【LeetCode】42. Trapping Rain Water 接雨水 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 保存左右最大值 单调栈 日期 题目地址:ht ...
- 【LeetCode】663. Equal Tree Partition 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】475. Heaters 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...