问题描述
我正在写一个连接到帆的应用程序
服务器。
此应用程序与科尔多瓦
, angularJS
书面和离子
。
当我在浏览器中启动我的应用程序(以离子服务
)的插座将无法连接到服务器。下面是我得到的消息:
GET http://localhost:8100/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_s…sails_io_sdk_language=javascript&EIO=3&transport=polling&t=1443472067762-4错误(404)
服务器运行的是本地端口1337.我试图改变上述网址为:
<$p$p><$c$c>http://localhost:1337/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_s…sails_io_sdk_language=javascript&EIO=3&transport=polling&t=1443472067762-4和它的工作。
在我的code我设置后的网址 sails.io.js
已包含:
io.sails.url =的http://本地主机:1337';
io.sails.useCORSRouteToGetCookie = FALSE;
为什么要问本地主机:8100
,而不是本地主机:1337
当我在移动设备启动这个应用程序(该URL设置为 http://192.168.1.10:1337
),它的正常工作。
的版本 sails.io.js
是 0.11.6
和版本的帆
是 0.11.0
我终于找到了解决办法(但我很抱歉,我不记得在那里我发现它:()
问题是, io.sails.url =的http://本地主机:1337';
是第一个JS周期后,从而帆执行,插座已经被加载。 (这是(从我的理解),因为在一些浏览器如果code是在另一个剧本
标记它在另一个周期执行)
要使它工作,我收到我包括帆插座脚本中加入此code:
VAR apiHost ='localhost'的;
(函数(){
VAR IO;
Object.defineProperty(窗口,IO,{
得到:函数(){
返回IO;
},
设置:功能(值){
VAR帆;
IO =价值;
Object.defineProperty(IO,'帆',{
得到:函数(){
返回帆;
},
设置:功能(值){
帆=价值;
sails.url =的http://+ apiHost +':1337';
sails.useCORSRouteToGetCookie = FALSE;
}
});
}
});
})();
I'm writing an application that connects to a sails
server.
This application is written with cordova
, angularJS
and Ionic
.
When I launch my application in my browser (with ionic serve
) the socket fails to connect to the server. Here is the message I get:
GET http://localhost:8100/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_s…sails_io_sdk_language=javascript&EIO=3&transport=polling&t=1443472067762-4 404 (Not Found)
The server is running locally on port 1337. I tried to change the above URL to:
http://localhost:1337/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_s…sails_io_sdk_language=javascript&EIO=3&transport=polling&t=1443472067762-4
and it's working.
In my code I set the URL after sails.io.js
has been included:
io.sails.url = 'http://localhost:1337';
io.sails.useCORSRouteToGetCookie = false;
Why is it asking to localhost:8100
and not localhost:1337
?
When I launch this application in my mobile device (setting the url to http://192.168.1.10:1337
) it's working correctly.
Version of sails.io.js
is 0.11.6
, and version of sails
is 0.11.0
I finally found the solution (but I'm sorry I can't recall where I found it :()
The problem was that the io.sails.url = 'http://localhost:1337';
was executed after the first JS cycle, and thus the sails socket was already loaded. (that's (from what I understood) because in some browsers if the code is in another script
tag it is executed in another cycle)
To make it work I had to add this code before I include the sails socket script:
var apiHost = 'localhost';
(function (){
var io;
Object.defineProperty(window, 'io', {
get: function (){
return io;
},
set: function (value){
var sails;
io = value;
Object.defineProperty(io, 'sails', {
get: function (){
return sails;
},
set: function (value){
sails = value;
sails.url = 'http://'+apiHost+':1337';
sails.useCORSRouteToGetCookie = false;
}
});
}
});
})();
这篇关于套接字连接失败总是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!