WebScoket中不能直接@resource来通过spring管理对象
不能直接在websocket类中通过@resource来创建对象需要通过创建静态参数 ,再通过Configuration类中注入第一步创建websocket静态参数@Component@ServerEndpoint("/live-server/liveChat/websocket/{roomId}/{userId}")public class WebSocket {public static Li
·
不能直接在websocket类中通过@resource来创建对象
需要通过创建静态参数 ,再通过@resource注入
第一步创建websocket静态参数
@Component
@ServerEndpoint("/live-server/liveChat/websocket/{roomId}/{userId}")
public class WebSocket {
public static LiveService liveService;
public static ObjectMapper objectMapper;
public static StringRedisTemplate stringRedisTemplate;
// 使用map来收集session,key为roomId,value为同一个房间的用户集合
private static Map<Long, Set<Session>> rooms = new ConcurrentHashMap();
private static ConcurrentHashMap<Long, Session> sessionMap = new ConcurrentHashMap<>();
第二步 websocket配置类
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter(){
return new ServerEndpointExporter();
}
@Resource
public void setMessageService(LiveService liveService){
WebSocket.liveService = liveService;
}
@Resource
public void setRedis(StringRedisTemplate stringRedisTemplate){
WebSocket.stringRedisTemplate = stringRedisTemplate;
}
@Resource
public void setObjectMapper(ObjectMapper objectMapper){
WebSocket.objectMapper = objectMapper;
}

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)