Springboot整合Websocket遇到的坑


Springboot整合Websocket遇到的坑

一、使用Springboot內嵌的tomcat啟動websocket

1.添加ServerEndpointExporter配置bean

@Configuration
public class WebSocketConfig {

    /**
     * 服務器節點
     *
     * 如果使用獨立的servlet容器,而不是直接使用springboot的內置容器,就不要注入ServerEndpointExporter,因為它將由容器自己提供和管理
     * @return
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}

2.在接收連接的類加上@ServerEndpoint和@Component

@ServerEndpoint("/connect")
@Component

二、使用外部tomcat容器啟動websocket

1.刪除ServerEndpointExporter配置bean

2.接收連接的類刪除@Component

三、websocket關閉連接異常

如果客戶端關閉了websocket,但服務端沒有監聽到關閉事件,即onClose方法沒有調用,這是會發生的情況

此時如果服務端向客戶端推送消息,會出現異常告訴開發者:關閉了一個連接,並重新調用onClose方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM