qhtcsqht
qhtcsqht
2016-09-14 09:08
采纳率: 0%
浏览 4.2k

springmvc 整合websocket 报错 404

第一: pom 配置文件:

javax.websocket
javax.websocket-api
1.0
provided<!--这个是从网上看的是为了解决jar包冲突的 -->



org.springframework

spring-websocket

4.1.6.RELEASE





org.springframework

spring-messaging

4.1.6.RELEASE


第二步: 【注册,启动 websocket server 和 握手拦截器 】
@Configuration

@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

@Override  
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    System.out.println("....注册......");
    registry.addHandler(myhandler(), "/websocket").addInterceptors(myInterceptors()).setAllowedOrigins("*");  
    registry.addHandler(myhandler(), "/sockjs/websocket").addInterceptors(myInterceptors()).withSockJS();  
}  

@Bean  
public WebSocketHandler myhandler() { 
    System.out.println("....服务端......");
    return new WebsocketEndPoint();  
}  

@Bean  
public HandshakeInterceptor myInterceptors() { 
    System.out.println("....握手拦截器......");
    return new HandshakeInterceptor();  
}  

}

public class WebsocketEndPoint extends TextWebSocketHandler {

private Logger logger = LoggerFactory.getLogger(WebsocketEndPoint.class);

@Override

protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {

  super.handleTextMessage(session, message);  
  logger.debug("GOMA === > WebSocketEndPoint.handlerTextMessage...");  

  TextMessage returnMessage = new TextMessage(message.getPayload()+" received at server");  
  session.sendMessage(returnMessage);  

}

}

public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor {

private Logger logger = Logger.getLogger(getClass());

@Override

public boolean beforeHandshake(ServerHttpRequest request,

ServerHttpResponse response, WebSocketHandler wsHandler,

Map attributes) throws Exception {

logger.debug("GOMA ===> Before Handshake");

return super.beforeHandshake(request, response, wsHandler, attributes);

}

@Override

public void afterHandshake(ServerHttpRequest request,

ServerHttpResponse response, WebSocketHandler wsHandler,

Exception ex) {

logger.debug("GOMA ===> After Handshake");

super.afterHandshake(request, response, wsHandler, ex);

}

}

第三步:页面 client
我的tomcat 的端口是 9018
socket = new WebSocket('ws://localhost:9018/testSocket/websocket');

运行报错:
WebSocket connection to 'ws://localhost:9018/testSocket/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400

这是为啥? 都配置了 ,为啥 是404 奇怪~~~ 那个大神给我解决一下

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐