Building to device(ionic run android) prevents sails web socket from connecting


#1

I had a working socket connection using sails v0.11.0 and sails.io.js v0.11.5 until approximately one week ago. I’ve tried reconfiguring my setup to look something like the solution provided here, but still no connection.

I just discovered that if I do ‘ionic serve’, to build to my local chrome browser, from the command line instead of ionic run android, to build to my android device, my sails socket connects. I had been successfully connecting from my android device previously.

ionic index.html scripts loading

<head> 
  <script src="lib/ionic/js/ionic.bundle.js"></script>
  <script src="cordova.js"></script>
  <script src="app.js"></script>
  <script src="lib/socket.io-client/socket.io.js"></script>
  <script src="lib/sails.io.js/sails.io.js"></script>
  <script type="text/javascript">io.sails.url = 'http://my_sails_api_ip:1337';</script>
  <script src="lib/angularSails/dist/ngsails.io.js"></script>
</head>

Loading the scripts in this order had been working for me for about 6 months. I’m not sure why it stopped.

The error I get in chrome console is;

Failed to load resource: the server responded with a status of 404 (Not Found) http://my_sails_api_ip:1337/__getcookie

I’ve tried setting ‘useCORSRouteToGetCookie = false’ as has been suggested on other posts. This results in repeated unsuccessful attempts to make the socket connection as opposed to the single error above.

additional info: My sails api is being ran on an Amazon EC2 instance that “my_sails_api_ip:1337” points to


#2

Solved it.This was due to my upgrade to Cordova 5.3.3 and effects Cordova 5.x.x. See this blog post by Nic Raboy https://blog.nraboy.com/2015/05/whitelist-external-resources-for-use-in-ionic-framework/

I had to tweak the meta tag a bit from Nic’s to get my websocket to connect as well. Here is what that looks like;

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">