spring+struts2+websocket
@Component
@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new MyWebSocketHandler(), "/webSocketServer")// 添加一个处理器还有定义处理器的处理路径
.addInterceptors(new MyHandShakeInterceptor());
registry.addHandler(new MyWebSocketHandler(), "/sockjs/webSocketServer")// 添加一个处理器还有定义处理器的处理路径
.addInterceptors(new MyHandShakeInterceptor()).withSockJS();
}
}
页面socket请求:
var socket;
if ('WebSocket' in window) {
socket = new WebSocket("ws://"+window.location.host+"/websocket/webSocketServer");
} else if ('MozWebSocket' in window) {
socket = new MozWebSocket("ws://"+window.location.host+"/websocket/webSocketServer");
} else {
socket = new SockJS("http://"+window.location.host+"/websocket/sockjs/webSocketServer");
}
在网上找的例子,例子是springmvc请求,socket可以正常建立;
自己项目是SSH架构,页面请求为
var socket;
if ('WebSocket' in window) {
socket = new WebSocket("ws://"+window.location.host+"/data/webSocketServer");
} else if ('MozWebSocket' in window) {
socket = new MozWebSocket("ws://"+window.location.host+"/data/webSocketServer");
} else {
socket = new SockJS("http://"+window.location.host+"/data/sockjs/webSocketServer");
}
其中data为项目名称,总是报404错误,跪求大牛帮助!!!