我正在嘗試在 Spring Boot 項目中使用 Javax JSR 356 API 實現服務器端 websocket 端點。我正在關注本教程。我知道在這個主題上有一些關于 SO 的問題,但我無法找到一個合理的解決方案。當我嘗試使用客戶端 (ARC) 連接到 websocket 時,它只報告“發生未知錯誤”。我檢查了嵌入式 Tomcat 服務器的日志,但沒有任何條目。根據 Tomcat 啟動條目,應用程序的上下文路徑為空 (""),因此我確定我使用正確的 URI 訪問了 websocket。這是我的代碼:@ServerEndpoint(value = "/socket", configurator = SpringConfigurator.class)public class WebSocketController{ @OnOpen public void onOpen(Session session) throws IOException { System.out.println("Socket has been opened: " + session.getId()); } @OnClose public void onClose(Session session) throws IonException { System.out.println("Socket has been closed: " + session.getId()); }}Spring configuration class@Configurationpublic class WebSocketConfig{ @Bean public WebSocketController webSocketController() { return new WebSocketController(); } @Bean public ServletContextAware endpointExporterInitializer(final ApplicationContext applicationContext) { return new ServletContextAware() { @Override public void setServletContext(ServletContext servletContext) { ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter(); serverEndpointExporter.setApplicationContext(applicationContext); try { serverEndpointExporter.afterPropertiesSet(); } catch (Exception e) { throw new RuntimeException(e); } } }; }}如何在 Spring Boot 應用程序中實現服務器端 websocket 端點?
添加回答
舉報
0/150
提交
取消