2016-02-04 27 views
1

我已經解決了這個問題,這段代碼幫我自動檢測服務器是https還是http。然後,使用Javascript創建正確的鏈接SockJS以將其添加到服務器Web-socket。如下Spring-websocket-run-errors-on-cloudfoundry -Pivotal-Web-Services

代碼:

var protocol = location.protocol; 
var hostname = location.hostname; 
var port = location.port; 
var url_prefix = protocol + "//" + hostname + (port === '' ? '' : ':' + port) + contextPath; 
var sock = new SockJS(url_prefix + '/connectsocket'); 

春天的WebSocket本地主機上運行OK:

var socket = new SockJS('http: //localhost:9091/connect/sockjs'); 

春天的WebSocket上運行bics.cfapps.io錯誤:

var socket = new SockJS('http: //bics.cfapps.io/connect/sockjs'); 

錯誤消息CONSOLE.LOG

WebSocket connection to 'ws://bics.cfapps.io/connect/sockjs/376/7o5_41y7/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400 

sockjs-0.3.4.js:807 POST http: //bics.cfapps.io/connect/sockjs/376/colrrwci/xhr_streaming 500 (Internal Server Error) 

Whoops! Lost connection to undefined 

添加端口4443,但它bics.cfapps.io劇照錯誤:

var socket = new SockJS('http: //bics.cfapps.io:4443/connect/sockjs'); 

錯誤消息CONSOLE.LOG

XMLHttpRequest cannot load http: //bics.cfapps.io:4443/connect/sockjs/info. No 'Access-Control-Allow-Origin' header is present on the requested resource. 

Origin 'http: //bics.cfapps.io' is therefore not allowed access. The response had HTTP status code 408. 

Whoops! Lost connection to undefined 

添加端口4443個& WS但它bics.cfapps.io劇照錯誤:

var socket = new WebSocket('ws://bics.cfapps.io:4443/connect/sockjs'); 

錯誤消息CONSOLE.LOG

WebSocket connection to 'ws://bics.cfapps.io:4443/connect/sockjs' failed: Error during WebSocket handshake: Unexpected response code: 408 

Whoops! Lost connection to ws://bics.cfapps.io:4443/connect/sockjs 

我的代碼配置:

@Configuration 
@EnableWebSocket 
@EnableWebSocketMessageBroker 
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { 
    @Override 
    public void configureMessageBroker(MessageBrokerRegistry config) { 
     config.enableSimpleBroker("/topic"); 
     config.setApplicationDestinationPrefixes("/bics"); 
    } 
    @Override 
    public void registerStompEndpoints(StompEndpointRegistry registry) { 
     registry.addEndpoint("/connect/sockjs").withSockJS(); 
    } 
} 
+0

請發表您的WebSocket配置和日誌。連接是否來自同一個來源? –

+0

@ThanhNguyenVan請看我的更新問題。我的項目在我的本地運行正常,但它在cloudfoundry上運行錯誤...我上面描述的錯誤 –

+0

同樣的問題與我同行,你有沒有發現任何東西 –

回答

0

只是指出在評論由gregturn因爲答案是正確的:

PWS通過ws:支持正常的WebSockets而是通過wss:唯一可靠的WebSockets。此外,他們的非標準端口4443

上看到工作PWS release notes June 2016