springboot整合websocket
1、添加依赖
<dependency> <!-- websocket --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <dependency> <!-- fastjson --> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
2、添加配置文件
package com.cosmo.sandtable.configure; import com.cosmo.sandtable.service.ReceiveDataService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; import javax.annotation.PostConstruct; /** * @author 12077 */ @Configuration public class WebSocketConfig { /** * 注入一个ServerEndpointExporter,该Bean会自动注册使用@ServerEndpoint注解申明的websocket endpoint */ @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
package com.cosmo.sandtable.websocket; import com.cosmo.sandtable.service.ReceiveDataService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; /** * 前后端交互的类实现消息的接收推送(自己发送给所有人(不包括自己)) * * @author 12077 * @ServerEndpoint(value = "/test/oneToMany") 前端通过此URI 和后端交互,建立连接 */ @Slf4j @ServerEndpoint(value = "/sandTable") @Service public class OneToManyWebSocket { /** * 记录当前在线连接数 */ private static AtomicInteger onlineCount = new AtomicInteger(0); /** * 存放所有在线的客户端 */ private static Map<String, Session> clients = new ConcurrentHashMap<>(); private static ReceiveDataService receiveDataService; /** * 解决authwire不能注入问题 ReceiveDataService * @param receiveDataService */ @Autowired public void setApplicationContext(ReceiveDataService receiveDataService){ OneToManyWebSocket.receiveDataService=receiveDataService; } /** * 连接建立成功调用的方法 */ @OnOpen public void onOpen(Session session) { onlineCount.incrementAndGet(); // 在线数加1 clients.put(session.getId(), session); log.info("有新连接加入:{},当前在线人数为:{}", session.getId(), onlineCount.get()); receiveDataService.lightResetLoop(1); } /** * 连接关闭调用的方法 */ @OnClose public void onClose(Session session) { onlineCount.decrementAndGet(); // 在线数减1 clients.remove(session.getId()); log.info("有一连接关闭:{},当前在线人数为:{}", session.getId(), onlineCount.get()); } /** * 收到客户端消息后调用的方法 * * @param message 客户端发送过来的消息 */ @OnMessage public void onMessage(String message, Session session) { log.info("服务端收到客户端[{}]的消息:{}", session.getId(), message); this.sendMessage(message, session); } @OnError public void onError(Session session, Throwable error) { log.error("发生错误"); error.printStackTrace(); } /** * 群发消息 * * @param message 消息内容 */ private void sendMessage(String message, Session fromSession) { for (Map.Entry<String, Session> sessionEntry : clients.entrySet()) { Session toSession = sessionEntry.getValue(); // 排除掉自己 if (!fromSession.getId().equals(toSession.getId())) { log.info("服务端给客户端[{}]发送消息{}", toSession.getId(), message); toSession.getAsyncRemote().sendText(message); } } } public void sendMessage(String message) { for (Map.Entry<String, Session> sessionEntry : clients.entrySet()) { Session toSession = sessionEntry.getValue(); toSession.getAsyncRemote().sendText(message); } } }
websocket不能注入报空指针:https://blog.csdn.net/qq_40136782/article/details/109078750
https://www.cnblogs.com/xuwenjin/p/12664650.html