前言:

  最近工作中有一个需求,就是服务端要主动推送消息给客户端,而我们平常的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服务器的更多相关文章

  1. SpringBoot+Mybatis集成搭建

    本博客介绍一下SpringBoot集成Mybatis,数据库连接池使用alibaba的druid,使用SpringBoot微框架虽然集成Mybatis之后可以不使用xml的方式来写sql,但是用惯了x ...

  2. SpringBoot集成RabbitMQ消息队列搭建与ACK消息确认入门

    1.RabbitMQ介绍 RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,用于在分布式系统中存储转发消息,在易用性.扩展性.高可用性等方面表现不俗.Rabbi ...

  3. 持续集成之二:搭建SVN服务器(subversion)

    安装环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) jdk1.7.0_80 subversion-1.10.3.tar.gz apr-1. ...

  4. idea 搭建 SpringBoot 集成 mybatis

    编译器:IDEA2018.2.3 环境:win10,jdk1.8,maven3.4 数据库:mysql 5.7 备注:截图较大,如果看不清,可以在图片上右键=>在新标签页中打开   查看高清大图 ...

  5. Windows/Linux 环境搭建Git服务器 + vs2012集成git

    1. 下载.安装Git 我的系统是Windows 7,需要安装Git for Windows. 下载地址: http://code.google.com/p/msysgit/downloads/lis ...

  6. SpringBoot集成ssm-druid-通用mapper

    简单介绍 springboot 首先什么是springboot? springboot是spring的另外一款框架,设计目的是用来简化新的spring应用的搭建和开发时所需要的特定的配置,从而使开发过 ...

  7. 自己家里搭建NAS服务器有什么好方案?

    转自:https://www.zhihu.com/question/21359049 作者:陈二发链接:https://www.zhihu.com/question/21359049/answer/6 ...

  8. Windows Server 2003搭建邮件服务器

    Windows Server 2003搭建邮件服务器 由于Windows Server 2003默认是没有安装我们搭建邮件服务器所需要的POP3和SMTP服务的,因此需要我们自己来安装.方法如下: 1 ...

  9. LUA+resty 搭建验证码服务器

    使用Lua和OpenResty搭建验证码服务器 雨客 2016-04-08 16:38:11 浏览2525 评论0 云数据库Redis版 摘要: Lua下有个Lua-GD图形库,通过简单的Lua语句就 ...

随机推荐

  1. UIWindow介绍

    1.作为容器,包含app所要显示的所有视图 3.与UIViewController协同工作,方便完成设备方向旋转的支持 1.addSubview 2.rootViewController 三.Wind ...

  2. 【python】青果教务系统模拟登陆

    使用 python 的 selenium + chrome 来模拟登陆学校教务系统 完整代码传至 github,增加了一个自动识别验证码的功能,不过是用的别人的轮子,识别度也不高 这是需要手动输入验证 ...

  3. 资源工作表中与资源有关的操作(Project)

    <Project2016 企业项目管理实践>张会斌 董方好 编著 这个内容,我需要专门写一篇吗? 不写吧,好像对不起我那股学习的劲:写吧,实在是--一句话就够了:所有与任务有关的新建.修改 ...

  4. mysql使用自定义序列实现row_number功能

    看了一些文章,终于知道该怎么在 mysql 里面实现 row_number() 排序 话不多说,show you the code: 第一步:建表: create table grades( `nam ...

  5. CF1077A Frog Jumping 题解

    Content 在一个数轴上有一个动点,初始时在 \(0\) 这个位置上,接下来有若干次操作,对于第 \(i\) 次操作: 如果 \(i\) 是奇数,那么动点往右移 \(a\) 个单位. 如果 \(i ...

  6. CSS 常用颜色代号

    常用颜色代号一览表:http://www.divcss5.com/html/h636.shtml   #000000   #2F0000   #600030   #460046   #28004D   ...

  7. Flink的窗口处理机制(一)

    一.为什么需要 window ? 在流处理应用中,数据是连续不断的,即数据是没有边界的,因此我们不可能等到所有数据都到了才开始处理.当然我们可以每来一个消息就处理一次,但是有时我们需要做一些聚合类的处 ...

  8. JAVA结合 JSON Web Token(JWT) 工具类

    引入java-jwt-3.3.0.jar .  jjwt-0.9.0.jar .jackson-all-1.7.6.jar 或者maven <!-- https://mvnrepository. ...

  9. 【LeetCode】504. Base 7 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 内建库 BigInteger类 逐位计算 倍数相加 ...

  10. 【LeetCode】773. Sliding Puzzle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/sliding- ...