Sails socket io connecting to a different url than

2019-03-02 01:13发布

问题:

In production when I try to connect to my socket client in sails at 'https://something.com/api/' from the client url 'https://something.com/' I get a 404

https://something.com/socket.io/?__sails_io_sdk_version=0.13…owser&__sails_io_sdk_language=javascript&EIO=3&transport=polling&t=LFGJG5g 404 (Not Found)

When I establish a socket connection in sails on my local machine it works.

My Local Configuration.

CLIENT HOST

http://localhost:8080/

API HOST

http://localhost:1337/

HTML

<script src="sails.io.js" autoConnect="false"></script>

JS

connect() {
    this.socket = io.sails.connect('http://localhost:1337');// Works Fine
}

My Production Configuration

CLIENT HOST

https://something.com/ 

API HOST

https://something.com/api/

JS

connect() {
    this.socket = io.sails.connect('https://something.com/api/');// Does not work
}

I made sure that autoConnect was defiantly set to false and that the only connection was happening via the connect() function. It seems that no matter what string I pass into the connect() function ( only in production ), it assumes the client url (https://something.com/) and drops the suffix /api/ from the production url (https://something.com/api/).

回答1:

<script src="/js/dependencies/sails.io.js"></script>
<script type="text/javascript">
  io.sails.url = 'https://myapp.com'; // or where you want
</script>

The docs can be found here: http://sailsjs.com/documentation/reference/web-sockets/socket-client#?programmatic-configuration-using-iosails