问题描述
Sails 0.10.0-rc7
Sails 0.10.0-rc7
Sails Socket IO:客户端未收到服务器的响应。
Sails Socket IO : Client not receiving response from server.
我正在尝试使用内置的蓝图来使用此功能从服务器获取信息。 (我希望使用默认行为)
Using sails built in blueprints I am attempting to get information from my server using this functionality. (Im looking to use the default behaviour)
//Client on different server (localhost:8000)
//Sails server
var socket = io.connect('http://localhost:1337');
socket.get('/event',function serverSays(err,events){
if (err)
console.log(err)
console.log(JSON.stringify(events));
});
服务器
事件模型
Server
Event Model
module.exports = {
schema : true,
attributes: {
name : {
type : 'STRING',
maxLength: 50,
required: true
}
}
};
在服务器终端(日志)中:
In the server terminal (logs) :
verbose:客户端授权
verbose:握手授权4TGNw-ywabWYG9j-AHaC
verbose:设置请求GET /socket.io/1/websocket/4TGNw-ywabWYG9j-AHaC?__sails_io_sdk_version=0.10.0&__s = browser& __ sails_io_sdk_language = javascript
详细信息:设置客户端4TGNw-ywabWYG9j-AHaC的心跳间隔
详细信息:已授权
的客户端详细信息:websocket编写1:
详细信息:一个套接字.io客户端(4TGNw-ywabWYG9j-AHaC)已成功连接!
verbose: client authorizedverbose: handshake authorized 4TGNw-ywabWYG9j-AHaCverbose: setting request GET /socket.io/1/websocket/4TGNw-ywabWYG9j-AHaC?__sails_io_sdk_version=0.10.0&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascriptverbose: set heartbeat interval for client 4TGNw-ywabWYG9j-AHaCverbose: client authorized forverbose: websocket writing 1::verbose: A socket.io client (4TGNw-ywabWYG9j-AHaC) connected successfully!
但是我的客户端上的回调从未被调用???
BUT the callback on my client is never being called????
似乎客户端与服务器连接。
It seems as if the client connects with the server..
有任何建议吗?
我必须强调客户端和Sails服务器在不同的服务器上运行。执行io.connect(localhost:1337)时的握手会根据服务器日志正确地与服务器对话。
I must stress that the client and the sails server are running on different servers. The handshake when performing io.connect(localhost:1337) talks with the server correctly based on the server logs.
其后续操作socket.get( / Event ),不会产生任何结果。根据服务器日志,我会说它永远不会到达服务器。...
Its the subsequent action socket.get("/Event") which does not result in anything. Based on the server logs, I would say that its not ever reaching the server....
推荐答案
我以为我会留下一个注释,因为我现在正在执行我的实现。
I thought I would just leave a note as I have my implementation working now.
事实证明,我犯了一个相当尴尬的错误/假设。
So as it turns out, I made a fairly embarrassing mistake/assumption.
使用Sails js的浏览器SDK,我使用以下方式连接到远程服务器:
using Sails js's browser SDK I was connecting to a remote server using:
io.connect("serverurl")
然后很高兴地进行了我的业务,尝试执行各种套接字功能,例如socket.get。
and then happily went about my business attempting to perform various socket functions such as socket.get..
我没有做的是
io.connect("url")
我仍然必须通过侦听确保我的应用确实已连接到服务器在套接字上进行connect事件:
I still had to ensure that my app had indeed connected to the server by listening on the socket for the connect event:
socket.on("connect",function())...
一旦我解决了这个小难题,一切就过去了,而且正在畅游!
Once I had this little piece of the puzzle resolved all went and is going swimmingly!
我还必须声明,我相信遇到此问题的原因是因为我试图在帆运行中执行初始连接和后续请求( init)函数。因此,我的后续动作很可能在应用程序和服务器成功建立连接之前执行。
I must also state that I believe the reason I was running into to this issue was because I was attempting to perform the initial connection and subsequent requests in the sails run (init) function. So my subsequent actions were more than likely executing before the app and the server had successfully established a connection.
我相信具有初始连接(io.connect)和后续连接操作是在单独的用户流中执行的,所有操作都将按照已确定已经建立的连接进行。
I believe had the initial connect (io.connect) and the subsequent actions been executed in separate user flows, all would have been as the connection would have surely been established already.
这篇关于Sails V0.10-rc7通过Socket.IO使用REST蓝图从数据库获取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!