WebSocket connection to 'ws://localhost:8888/websocket' failed: Error during WebSocket handshake: Unexpected response code: 404
//得到WebSocket對象 var websocket = new WebSocket("ws://localhost:8888/websocket");
@ServerEndpoint(value = "/websocket") @Service public class MsgWebSocket { /** * 收到消息 * * @param message 客戶端發送過來的消息 * @param session */ @OnMessage public void onMessage(String message, Session session) { ... } }
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>
@Component public class WebSocketConfig { /** * 使用spring boot時,使用的是spring-boot的內置容器, * 若是要使用WebSocket,須要注入ServerEndpointExporter * * @return */ @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
完整代碼:前端
git@github.com:valsong/blog-demo.git 中的websocket-javax項目