在sails js socket.io中使用io.socket.get
我正在使用sailsjs和socketio。sails版本是0.10.5。我有以下用于测试的套接字客户端:在sails js socket.io中使用io.socket.get,socket.io,sails.js,Socket.io,Sails.js,我正在使用sailsjs和socketio。sails版本是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
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'
module.exports={
join: function(req, res) {
console.log('Inside Join');
sails.sockets.join(req.socket,'myroom');
res.json({message:'youve subscribed to a room'});
}
};
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.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.on('connect', function(){
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);
});
在我的api/controller/LayoutController中,我有以下内容:
'get /join':'LayoutController.join'
module.exports={
join: function(req, res) {
console.log('Inside Join');
sails.sockets.join(req.socket,'myroom');
res.json({message:'youve subscribed to a room'});
}
};
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.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.on('connect', function(){
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);
});
我遇到的问题是,控制器中的join函数永远不会通过套接字调用io.socket.get('/join',…)触发。console.log-in控制器从不打印,也不会向客户端返回响应。如果我通过http执行相同的测试,它将触发join函数。我第一次使用socket。任何人都可以建议我在这里做错了什么吗?您可以尝试在套接字与服务器建立连接后运行get请求,因为它将作为通过套接字向sails服务器发出的虚拟请求。您可以将代码更改为以下内容:
'get /join':'LayoutController.join'
module.exports={
join: function(req, res) {
console.log('Inside Join');
sails.sockets.join(req.socket,'myroom');
res.json({message:'youve subscribed to a room'});
}
};
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.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.on('connect', function(){
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);
});
这将仅在套接字连接后调用路由。希望这有帮助 您将套接字客户端的代码放在哪里?您是从单独的SAIL服务器还是节点服务器运行它?它看起来不像是要在浏览器中运行的代码,除非您使用的是
browserify
…谢谢您的回复。我使用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”),然后发布答案,以便其他有相同问题的用户可以在此处找到帮助!