SpringBoot整合websocket简单示例

 

 

依赖

  <!-- 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(); } } } }
复制代码

 

posted @   yvioo  阅读(182)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示