SpringBoot整合websocket簡單示例
阿新 • • 發佈:2021-07-28
依賴
<!-- 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); } @Overridepublic 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 @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才可以)