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(); } } } }
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)