依赖

  <!-- springboot整合websocket -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

也使用了 如果引入不需要引入了

 <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>

EndpointConfigure.java

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import javax.websocket.server.ServerEndpointConfig; /**
*/
public class EndpointConfigure extends ServerEndpointConfig.Configurator implements ApplicationContextAware {
private static volatile BeanFactory context; @Override
public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException
{
return context.getBean(clazz);
} @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
EndpointConfigure.context = applicationContext;
}
}

WebSocketConfigure.java

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Configuration
@Slf4j
public class WebSocketConfigure {
@Bean
public EndpointConfigure newConfigure()
{
return new EndpointConfigure();
} @Bean
public ServerEndpointExporter serverEndpointExporter() {
log.info("===========开启WebSocket支持===========");
return new ServerEndpointExporter();
}
}

测试demo

IndexWebSocket.java

import com.example.websocket.config.EndpointConfigure;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; /**
*
* @author yvioo
*/
@ServerEndpoint(value = "/index", configurator = EndpointConfigure.class)
@Component
@Scope("prototype") //多例
@Slf4j
public class IndexWebSocket { private static Map<String, IndexWebSocket> prototypeMap = new ConcurrentHashMap<>(); /**
* 与某个客户端的连接会话,需要通过它来给客户端发送数据
*/
private Session session; /**
* 可以直接注入,这里没有这个类 只是示例
*/
@Autowired
private UserMapper userMapper; /**
* 连接建立成功调用的方法
*/
@OnOpen
public void onOpen(Session session) {
this.session=session;
log.info("连接" ); } /**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() { log.info("退出" );
} /**
* 收到客户端消息
* @param message 客户端发送过来的消息
*/
@OnMessage
public void onMessage(String message, Session session) {
log.info("设备连接" );
} /**
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
log.error("错误: {}", error.getMessage());
} /**
*服务器主动推送消息
*/
public void sendMessage(String message) {
synchronized (this.session) {
try {
this.session.getBasicRemote().sendText(message);
} catch (IOException e) {
e.printStackTrace();
}
}
} }

SpringBoot整合websocket简单示例的更多相关文章

  1. RabbitMQ基础组件和SpringBoot整合RabbitMQ简单示例

    交换器(Exchange) 交换器就像路由器,我们先是把消息发到交换器,然后交换器再根据绑定键(binding key)和生产者发送消息时的路由键routingKey, 按照交换类型Exchange ...

  2. springboot整合redis简单示例

    一.项目架构 二.项目内容 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  3. SpringBoot 整合 WebSocket

    SpringBoot 整合 WebSocket(topic广播) 1.什么是WebSocket WebSocket为游览器和服务器提供了双工异步通信的功能,即游览器可以向服务器发送消息,服务器也可以向 ...

  4. springboot整合websocket原生版

    目录 HTTP缺点 HTTP websocket区别 websocket原理 使用场景 springboot整合websocket 环境准备 客户端连接 加入战队 微信公众号 主题 HTTP请求用于我 ...

  5. Springboot整合Websocket遇到的坑

    Springboot整合Websocket遇到的坑 一.使用Springboot内嵌的tomcat启动websocket 1.添加ServerEndpointExporter配置bean @Confi ...

  6. WebSocket的简单认识&SpringBoot整合websocket

    1. 什么是WebSocket?菜鸟对websocket的解释如下 WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. WebSocket 使得客户端和服务 ...

  7. springboot整合websocket高级版

    目录 sockjs介绍 产生的原因 环境搭建 springboot整合sockjs 使用场景 聊天室开发 点对点通信 群聊 效果 总结 加入战队 微信公众号 上一章节我们说了websocket的优缺点 ...

  8. SpringBoot整合Swagger2(Demo示例)

    写在前面 由于公司项目采用前后端分离,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理 ...

  9. SpringBoot整合SpringSecurity简单实现登入登出从零搭建

    技术栈 : SpringBoot + SpringSecurity + jpa + freemark ,完整项目地址 : https://github.com/EalenXie/spring-secu ...

随机推荐

  1. 记一次使用 SelectMany 的经历

    最近在改造一个功能时为了减少循环的层数,于是想着将List列表映射为一个能直接使用颗粒大小的List列表,这样一层循环就可以解决问题.     public class ConflictWordIte ...

  2. Discontinuous Galerkin method for steady transport problem

    下面讨论如何使用 Discontinuous Galerkin 求解恒定对流问题. 1.简介 恒定状态对流方程 \[\begin{equation} a\cdot \nabla \mathbf{u} ...

  3. CUDA计算矩阵相乘

    1.最简单的 kernel 函数 __global__ void MatrixMulKernel( float* Md, float* Nd, float* Pd, int Width) { int ...

  4. Docker Error response from daemon,Docker 换镜像

    Docker换镜像,Docker  pull.Docker search 失败出现以下错误 Error response from daemon: Get https://index.docker.i ...

  5. 在R语言中使用Stringr进行字符串操作

    今天来学习下R中字符串处理操作,主要是stringr包中的字符串处理函数的用法. 先导入stringr包,library(stringr),require(stringr),或者stringr::函数 ...

  6. ubuntu常见错误--Could not get lock /var/lib/dpkg/lock

    ubuntu常见错误--Could not get lock /var/lib/dpkg/lock   通过终端安装程序sudo apt-get install xxx时出错:   E: Could ...

  7. Hive-insert into table 与 insert overwrite table 区别

    区分insert into 和 insert overowrite: 0. 命令格式 INSERT OVERWRITE|INTO TABLE tablename [PARTITION (partcol ...

  8. 自动化测试系列(二)|API测试

    在上次的自动化测试系列(一)中为大家大体介绍了自动化测试的概念,本文主要针对API测试的概念及API测试在猪齿鱼Choerodon中的实践展开. API(应用程序编程接口)测试是一种软件测试,可以直接 ...

  9. c#GridView

    分页: 1.先把属性AllowPaging设置为true, 2.pagesize为每一页的行数,PageSize="15". 3.OnPageIndexChanging=" ...

  10. 4 — springboot中的jsr303检验

    1.导入依赖 <!--JSR303校验的依赖 --> <dependency> <groupId>org.springframework.boot</grou ...