【发布时间】:2015-10-12 06:20:01
【问题描述】:
我正在使用带有套接字 io 的sailsjs。帆版是 0.10.5。我有以下用于测试的套接字客户端:
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');
// Instantiate the socket client (`io`)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);
// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';
// ...
//Send a GET request to `http://localhost:1337/hello`:
io.socket.get('/join', function serverResponded (body, JWR) {
// body === JWR.body
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
// When you are finished with `io.socket`, or any other sockets you connect manually,
// you should make sure and disconnect them, e.g.:
//io.socket.disconnect();
// (note that there is no callback argument to the `.disconnect` method)
});
io.socket.on('myroom', function(response){
console.log(response);
});
在我的 config/routes.js 中,我有以下内容:
'get /join':'LayoutController.join'
在我的 api/controller/LayoutController 中,我有以下内容:
module.exports={
join: function(req, res) {
console.log('Inside Join');
sails.sockets.join(req.socket,'myroom');
res.json({message:'youve subscribed to a room'});
}
};
我遇到的问题是控制器内部的连接函数永远不会通过套接字调用 io.socket.get('/join',...) 触发。控制器中的 console.log 永远不会打印,也不会响应客户端。如果我通过http做同样的测试,它会触发join函数。我第一次使用套接字。谁能建议我在这里做错了什么?
【问题讨论】:
-
你把你的套接字客户端的代码放在哪里了?您是从单独的 Sails 或 Node 服务器运行它吗?它看起来不像是要在浏览器中运行的代码,除非您使用的是
browserify... -
感谢 sgress 的回复。我正在使用 node 单独运行 socketclient 代码。最终,会有一个客户。但是,这是为了我的测试。看来问题出在我的本地机器上。我在另一台服务器上收到了 io.socket.get(...) 的响应。另一个不相关的问题是,用于sails 的socket.io 是否适用于IIS 7.5?
-
Socket.io 的维护者与 Sails 不同,但谷歌搜索“socket.io IIS”应该会给您一些见解。如果您能找出本地计算机的问题,我建议您更改标题以反映您的系统(例如“Socket request not working with Windows Server 2012”),然后发布答案,以便其他有同样问题的人可能会在这里找到帮助!