سؤال

I am using SockJS-client. SockJS constructor takes a relative URL as

var ws= new SockJS('/spring-websocket-test/sockjs/echo', undefined,{protocols_whitelist: [transport]});

Where do we indicate that WSS:// be used instead of WS://. If I try absolute URL, it gives error :

XMLHttpRequest cannot load ws://localhost:8080/appname/app. Cross origin requests are only supported for HTTP.
_ws_onclose. wasClean: false code: 1002 reason: Can't connect to server

Not sure why getting this error. Any similar configuration needed on Spring Server Implementation?

هل كانت مفيدة؟

المحلول

Went through the sock JS client code :I went through the client code -sock JS takes care of it -if its HTTPS it uses WSS else it does WS

var that = this;
var url = trans_url + '/websocket';
if (url.slice(0, 5) === 'https') {
    url = 'wss' + url.slice(5);
} else {
    url = 'ws' + url.slice(4);
}

No need to pass WS or WSS (In fact not even HTTP/HTTPS) with the SockJS constructor -just relative URL is sufficient. SockJs client library takes care of it. One more surprising fact I encountered, it appends "/websocket" at end of the URL -this gave me clue why I was not able to connect with java client using jetty websocket-client apis

نصائح أخرى

Try this code.

var ws= new SockJS('http://localhost:8080/sockjs/echo', undefined,{protocols_whitelist: [transport]});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top