我尝试使用弹簧4.3.17连接SockJS(v1.1.5)并且踩踏但是只有IE8无法连接,我才会超时。使用更实际的浏览器(Chrome,IE11,FireFox),它通过websocket连接没有问题,如果对我使用jquery 1.7的答案有用
这是浏览器控制台显示的内容:
REGISTRO: sockjs> -client:main using url +0mshttp://localhost:9081/CHAT/rest/add
REGISTRO: sockjs> -client:utils:url same +0mshttp://localhost:9081/CHAT/rest/addhttp://localhost:9081/CHAT/index.htmtrue
REGISTRO: sockjs> -client:info> -receiver http://localhost:9081/CHAT/rest/add +0ms
REGISTRO: Opening Web Socket...
REGISTRO: sockjs> -client:info> -receiver doXhr +16mshttp://localhost:9081/CHAT/rest/add/info
REGISTRO: sockjs> -client:browser:xhr GET +0mshttp://localhost:9081/CHAT/rest/add/info
REGISTRO: sockjs> -client:browser:xhr readyState +15ms1
REGISTRO: sockjs> -client:browser:xhr readyState +32ms2
REGISTRO: sockjs> -client:browser:xhr readyState +0ms3
REGISTRO: sockjs> -client:browser:xhr status +0ms200
REGISTRO: sockjs> -client:browser:xhr chunk +0ms
REGISTRO: sockjs> -client:browser:xhr readyState +0ms4
REGISTRO: sockjs> -client:browser:xhr status +0ms200
REGISTRO: sockjs> -client:browser:xhr finish +0ms200{"entropy":> -140896812,"origins":["*:*"],"cookie_needed":false,"websocket":true}
REGISTRO: sockjs> -client:info> -receiver finish +0ms[object Object]47
REGISTRO: sockjs> -client:info> -receiver _cleanup +0ms
REGISTRO: sockjs> -client:main _receiveInfo +0ms47
REGISTRO: sockjs> -client:main info +0ms[object Object]
REGISTRO: sockjs> -client:websocket enabled +0ms
REGISTRO: sockjs> -client:utils:transport disabled +0mswebsocket
REGISTRO: sockjs> -client:utils:transport disabled +0msxhr> -streaming
REGISTRO: sockjs> -client:utils:transport enabled +0msxdr> -streaming
REGISTRO: sockjs> -client:utils:transport disabled +0mseventsource
REGISTRO: sockjs> -client:utils:transport disabled +0msiframe> -eventsource
REGISTRO: sockjs> -client:utils:transport enabled +0mshtmlfile
REGISTRO: sockjs> -client:utils:transport enabled +0msiframe> -htmlfile
REGISTRO: sockjs> -client:utils:transport enabled +0msxhr> -polling
REGISTRO: sockjs> -client:utils:transport enabled +0msxdr> -polling
REGISTRO: sockjs> -client:utils:transport enabled +0msiframe> -xhr> -polling
REGISTRO: sockjs> -client:utils:transport enabled +0msjsonp> -polling
REGISTRO: sockjs> -client:main 7 enabled transports +0ms
REGISTRO: sockjs> -client:main attempt +0msxdr> -streaming
REGISTRO: sockjs> -client:main using timeout +0ms694
REGISTRO: sockjs> -client:main transport url +0mshttp://localhost:9081/CHAT/rest/add/914/i4rksf1x
REGISTRO: sockjs> -client:sender> -receiver http://localhost:9081/CHAT/rest/add/914/i4rksf1x/xhr_streaming +0ms
REGISTRO: sockjs> -client:buffered> -sender http://localhost:9081/CHAT/rest/add/914/i4rksf1x +0ms
REGISTRO: sockjs> -client:polling http://localhost:9081/CHAT/rest/add/914/i4rksf1x/xhr_streaming +0ms
REGISTRO: sockjs> -client:polling _scheduleReceiver +0ms
REGISTRO: sockjs> -client:receiver:xhr http://localhost:9081/CHAT/rest/add/914/i4rksf1x/xhr_streaming +0ms
REGISTRO: sockjs> -client:sender:xdr CHATT +0mshttp://localhost:9081/CHAT/rest/add/914/i4rksf1x/xhr_streaming
REGISTRO: sockjs> -client:browser:xhr cleanup +0ms
REGISTRO: sockjs> -client:sender:xdr _start +15ms
REGISTRO: sockjs> -client:main _transportTimeout +688ms
REGISTRO: sockjs> -client:buffered> -sender close +0ms
REGISTRO: sockjs> -client:buffered> -sender _cleanup +0ms
REGISTRO: sockjs> -client:sender> -receiver close +0ms
REGISTRO: sockjs> -client:polling abort +0ms
REGISTRO: sockjs> -client:receiver:xhr abort +0ms
REGISTRO: sockjs> -client:sender:xdr close +0ms
REGISTRO: sockjs> -client:sender:xdr cleanup +0mstrue
REGISTRO: sockjs> -client:receiver:xhr close +0ms
REGISTRO: sockjs> -client:polling close +0msnullusertrue
REGISTRO: sockjs> -client:receiver:xhr _cleanup +0ms
REGISTRO: sockjs> -client:main _transportClose +0msundefined2007Transport timed out
REGISTRO: sockjs> -client:main attempt +0mshtmlfile
REGISTRO: sockjs> -client:main using timeout +0ms694
REGISTRO: sockjs> -client:main transport url +0mshttp://localhost:9081/CHAT/rest/add/914/xchdoeru
REGISTRO: sockjs> -client:sender> -receiver http://localhost:9081/CHAT/rest/add/914/xchdoeru/htmlfile +0ms
REGISTRO: sockjs> -client:buffered> -sender http://localhost:9081/CHAT/rest/add/914/xchdoeru +0ms
REGISTRO: sockjs> -client:polling http://localhost:9081/CHAT/rest/add/914/xchdoeru/htmlfile +0ms
REGISTRO: sockjs> -client:polling _scheduleReceiver +0ms
REGISTRO: sockjs> -client:receiver:htmlfile http://localhost:9081/CHAT/rest/add/914/xchdoeru/htmlfile +0ms
REGISTRO: sockjs> -client:receiver:htmlfile using htmlfile +0mstrue
REGISTRO: sockjs> -client:main _transportTimeout +703ms
REGISTRO: sockjs> -client:buffered> -sender close +0ms
REGISTRO: sockjs> -client:buffered> -sender _cleanup +0ms
REGISTRO: sockjs> -client:sender> -receiver close +0ms
REGISTRO: sockjs> -client:polling abort +0ms
REGISTRO: sockjs> -client:receiver:htmlfile abort +0ms
REGISTRO: sockjs> -client:receiver:htmlfile _cleanup +0ms
REGISTRO: sockjs> -client:receiver:htmlfile _close +0msuser
REGISTRO: sockjs> -client:polling close +0msnullusertrue
REGISTRO: sockjs> -client:main _transportClose +0msnull2007Transport timed out
REGISTRO: sockjs> -client:main attempt +0msiframe> -htmlfile
REGISTRO: sockjs> -client:main using timeout +0ms1041
REGISTRO: sockjs> -client:main transport url +0mshttp://localhost:9081/CHAT/rest/add/914/js4asvar
REGISTRO: sockjs> -client:transport:iframe htmlfile +0mshttp://localhost:9081/CHAT/rest/add/914/js4asvarhttp://localhost:9081/CHAT/rest/add/iframe.html#t0oykufg
REGISTRO: sockjs> -client:main _transportTimeout +1s
REGISTRO: sockjs> -client:transport:iframe close +0ms
REGISTRO: sockjs> -client:transport:iframe postMessage +0mscundefined
REGISTRO: sockjs> -client:utils:iframe post +0ms{"windowId":"t0oykufg","type":"c","data":""}http://localhost:9081
REGISTRO: sockjs> -client:utils:iframe cleanup +0ms
REGISTRO: sockjs> -client:utils:iframe unattach +0ms
REGISTRO: sockjs> -client:main _transportClose +0msnull2007Transport timed out
REGISTRO: sockjs> -client:main attempt +0msxhr> -polling
REGISTRO: sockjs> -client:main using timeout +0ms694
REGISTRO: sockjs> -client:main transport url +0mshttp://localhost:9081/CHAT/rest/add/914/nc0qcv4d
REGISTRO: sockjs> -client:sender> -receiver http://localhost:9081/CHAT/rest/add/914/nc0qcv4d/xhr +0ms
REGISTRO: sockjs> -client:buffered> -sender http://localhost:9081/CHAT/rest/add/914/nc0qcv4d +0ms
REGISTRO: sockjs> -client:polling http://localhost:9081/CHAT/rest/add/914/nc0qcv4d/xhr +0ms
REGISTRO: sockjs> -client:polling _scheduleReceiver +0ms
REGISTRO: sockjs> -client:receiver:xhr http://localhost:9081/CHAT/rest/add/914/nc0qcv4d/xhr +0ms
REGISTRO: sockjs> -client:browser:xhr CHATT +0mshttp://localhost:9081/CHAT/rest/add/914/nc0qcv4d/xhr
REGISTRO: sockjs> -client:browser:xhr readyState +15ms1
REGISTRO: sockjs> -client:main _transportTimeout +688ms
REGISTRO: sockjs> -client:buffered> -sender close +0ms
REGISTRO: sockjs> -client:buffered> -sender _cleanup +0ms
REGISTRO: sockjs> -client:sender> -receiver close +0ms
REGISTRO: sockjs> -client:polling abort +0ms
REGISTRO: sockjs> -client:receiver:xhr abort +0ms
REGISTRO: sockjs> -client:browser:xhr close +0ms
REGISTRO: sockjs> -client:browser:xhr cleanup +0ms
REGISTRO: sockjs> -client:receiver:xhr close +0ms
REGISTRO: sockjs> -client:polling close +0msnullusertrue
REGISTRO: sockjs> -client:receiver:xhr _cleanup +0ms
REGISTRO: sockjs> -client:main _transportClose +0msnull2007Transport timed out
REGISTRO: sockjs> -client:main attempt +0msxdr> -polling
REGISTRO: sockjs> -client:main using timeout +0ms694
REGISTRO: sockjs> -client:main transport url +0mshttp://localhost:9081/CHAT/rest/add/914/vop35l43
REGISTRO: sockjs> -client:sender> -receiver http://localhost:9081/CHAT/rest/add/914/vop35l43/xhr +0ms
REGISTRO: sockjs> -client:buffered> -sender http://localhost:9081/CHAT/rest/add/914/vop35l43 +0ms
REGISTRO: sockjs> -client:polling http://localhost:9081/CHAT/rest/add/914/vop35l43/xhr +0ms
REGISTRO: sockjs> -client:polling _scheduleReceiver +0ms
REGISTRO: sockjs> -client:receiver:xhr http://localhost:9081/CHAT/rest/add/914/vop35l43/xhr +0ms
REGISTRO: sockjs> -client:sender:xdr CHATT +0mshttp://localhost:9081/CHAT/rest/add/914/vop35l43/xhr
REGISTRO: sockjs> -client:sender:xdr _start +15ms
REGISTRO: sockjs> -client:main _transportTimeout +688ms
REGISTRO: sockjs> -client:buffered> -sender close +0ms
REGISTRO: sockjs> -client:buffered> -sender _cleanup +0ms
REGISTRO: sockjs> -client:sender> -receiver close +0ms
REGISTRO: sockjs> -client:polling abort +0ms
REGISTRO: sockjs> -client:receiver:xhr abort +0ms
REGISTRO: sockjs> -client:sender:xdr close +0ms
REGISTRO: sockjs> -client:sender:xdr cleanup +0mstrue
REGISTRO: sockjs> -client:receiver:xhr close +0ms
REGISTRO: sockjs> -client:polling close +0msnullusertrue
REGISTRO: sockjs> -client:receiver:xhr _cleanup +0ms
REGISTRO: sockjs> -client:main _transportClose +0msnull2007Transport timed out
REGISTRO: sockjs> -client:main attempt +0msiframe> -xhr> -polling
REGISTRO: sockjs> -client:main using timeout +0ms1041
REGISTRO: sockjs> -client:main transport url +0mshttp://localhost:9081/CHAT/rest/add/914/5albkdox
REGISTRO: sockjs> -client:transport:iframe xhr> -polling +0mshttp://localhost:9081/CHAT/rest/add/914/5albkdoxhttp://localhost:9081/CHAT/rest/add/iframe.html#xmw0zh4e
REGISTRO: sockjs> -client:main _transportTimeout +1s
REGISTRO: sockjs> -client:transport:iframe close +0ms
REGISTRO: sockjs> -client:transport:iframe postMessage +0mscundefined
REGISTRO: sockjs> -client:utils:iframe post +0ms{"windowId":"xmw0zh4e","type":"c","data":""}http://localhost:9081
REGISTRO: sockjs> -client:utils:iframe cleanup +0ms
REGISTRO: sockjs> -client:utils:iframe unattach +0ms
REGISTRO: sockjs> -client:main _transportClose +0msnull2007Transport timed out
REGISTRO: sockjs> -client:main attempt +0msjsonp> -polling
REGISTRO: sockjs> -client:main using timeout +0ms347
REGISTRO: sockjs> -client:main transport url +0mshttp://localhost:9081/CHAT/rest/add/914/qyiufn3r
REGISTRO: sockjs> -client:sender> -receiver http://localhost:9081/CHAT/rest/add/914/qyiufn3r/jsonp +0ms
REGISTRO: sockjs> -client:buffered> -sender http://localhost:9081/CHAT/rest/add/914/qyiufn3r +0ms
REGISTRO: sockjs> -client:polling http://localhost:9081/CHAT/rest/add/914/qyiufn3r/jsonp +0ms
REGISTRO: sockjs> -client:polling _scheduleReceiver +0ms
REGISTRO: sockjs> -client:receiver:jsonp http://localhost:9081/CHAT/rest/add/914/qyiufn3r/jsonp +0ms
REGISTRO: sockjs> -client:receiver:jsonp _createScript +0mshttp://localhost:9081/CHAT/rest/add/914/qyiufn3r/jsonp?c=_jp.aqrr5u4
REGISTRO: sockjs> -client:receiver:jsonp onreadystatechange +0msloading
REGISTRO: sockjs> -client:main _transportTimeout +359ms
REGISTRO: sockjs> -client:buffered> -sender close +0ms
REGISTRO: sockjs> -client:buffered> -sender _cleanup +0ms
REGISTRO: sockjs> -client:sender> -receiver close +0ms
REGISTRO: sockjs> -client:polling abort +0ms
REGISTRO: sockjs> -client:receiver:jsonp abort +0ms
REGISTRO: sockjs> -client:receiver:jsonp _abort +0msError: JSONP user aborted read
REGISTRO: sockjs> -client:receiver:jsonp _cleanup +0ms
REGISTRO: sockjs> -client:polling close +0ms1000JSONP user aborted readtrue
REGISTRO: sockjs> -client:main _transportClose +0msnull2007Transport timed out
REGISTRO: sockjs> -client:main _close +0msnull2000All transports failedfalse0
REGISTRO: Whoops! Lost connection to http://localhost:9081/CHAT/rest/add
REGISTRO: sockjs> -client:main disconnected +16ms
AbstractWebSocketMessageBrokerConfigurer:
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
@Configuration
@EnableWebSocketMessageBroker
public class AppWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/calcApp");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/add").setAllowedOrigins("*").withSockJS().setClientLibraryUrl("http://localhost:9081/CHAT/js/socket/sockjs.js").setSessionCookieNeeded(false);
}
}
我的webSocketController:
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WebSocketController {
@MessageMapping("/add" )
@SendTo("/topic/showResult")
public Result addNum(CalcInput input) throws Exception {
Thread.sleep(2000);
Result result = new Result(input.getNum1()+"+"+input.getNum2()+"="+(input.getNum1()+input.getNum2()));
return result;
}
@RequestMapping("/start")
public String start() {
return "start";
}
}
客户端javascript:
jQuery.support.cors = true;
function connect() {
var socket = new SockJS('/CHAT/rest/add');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
stompClient.subscribe('/topic/showResult', function(calResult){
showResult(JSON.parse(calResult.body).result);
});
});
}
function disconnect() {
stompClient.disconnect();
setConnected(false);
}
function sendNum() {
var num1 = document.getElementById('num1').value;
var num2 = document.getElementById('num2').value;
stompClient.send("/calcApp/add", {}, JSON.stringify({ 'num1': num1, 'num2': num2 }));
}
function showResult(message) {
var response = document.getElementById('calResponse');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.appendChild(document.createTextNode(message));
response.appendChild(p);
}
在spring-security.xml中,我禁用了frame-options,这是我在web.xml中的servlet映射:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
提前致谢