sails js sockets - 超出最大调用堆栈大小错误
sails js sockets - Maximum call stack size exceeded error
我是 sailsjs 的新手,在尝试实现这个示例时卡住了:
http://sailsjs.org/documentation/concepts/realtime
api/controllers/SayController.js
module.exports = {
hello: function(req, res) {
// Make sure this is a socket request (not traditional HTTP)
if (!req.isSocket) {return res.badRequest();}
// Have the socket which made the request join the "funSockets" room
sails.sockets.join(req, 'funSockets');
// Broadcast a "hello" message to all the fun sockets.
// This message will be sent to all sockets in the "funSockets" room,
// but will be ignored by any client sockets that are not listening-- i.e. that didn't call `io.socket.on('hello', ...)`
sails.sockets.broadcast('funSockets', 'hello', req);
// Respond to the request with an a-ok message
return res.ok();
}
}
assets/js/myapp.js
io.socket.on('hello', function gotHelloMessage (data) {
console.log('Socket `' + data.id + '` joined the party!');
});
io.socket.get('/say/hello', function gotResponse(body, response) {
console.log('Server responded with status code ' + response.statusCode + ' and data: ', body);
})
现在,当我 运行 应用程序时,出现错误
ending 500 ("Server Error") response:
RangeError: Maximum call stack size exceeded
at Server.hasOwnProperty (native)
at _hasBinary (/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/has-binary/index.js:49:45)
at _hasBinary (/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/has-binary/index.js:49:63)
请帮忙...
我认为 sails.sockets.broadcast()
的参数需要稍微调整一下。也许文档示例已过时或不完整。试试这个:
SayController.js:
module.exports = {
hello: function(req, res) {
// Make sure this is a socket request (not traditional HTTP)
if (!req.isSocket) {return res.badRequest();}
// Have the socket which made the request join the "funSockets" room
sails.sockets.join(req, 'funSockets');
// Broadcast a "hello" message to all the fun sockets.
// This message will be sent to all sockets in the "funSockets" room,
// but will be ignored by any client sockets that are not listening-- i.e. that didn't call `io.socket.on('hello', ...)`
// ▼ this is "data" in io.socket.on('hello', function gotHelloMessage (data)
sails.sockets.broadcast('funSockets', 'hello', {id: 'my id'}, req);
// Respond to the request with an a-ok message
// ▼ The object returned here is "body" in io.socket.get('/say/hello', function gotResponse(body, response)
return res.ok({
message: "OK"
});
}
}
我是 sailsjs 的新手,在尝试实现这个示例时卡住了:
http://sailsjs.org/documentation/concepts/realtime
api/controllers/SayController.js
module.exports = {
hello: function(req, res) {
// Make sure this is a socket request (not traditional HTTP)
if (!req.isSocket) {return res.badRequest();}
// Have the socket which made the request join the "funSockets" room
sails.sockets.join(req, 'funSockets');
// Broadcast a "hello" message to all the fun sockets.
// This message will be sent to all sockets in the "funSockets" room,
// but will be ignored by any client sockets that are not listening-- i.e. that didn't call `io.socket.on('hello', ...)`
sails.sockets.broadcast('funSockets', 'hello', req);
// Respond to the request with an a-ok message
return res.ok();
}
}
assets/js/myapp.js
io.socket.on('hello', function gotHelloMessage (data) {
console.log('Socket `' + data.id + '` joined the party!');
});
io.socket.get('/say/hello', function gotResponse(body, response) {
console.log('Server responded with status code ' + response.statusCode + ' and data: ', body);
})
现在,当我 运行 应用程序时,出现错误
ending 500 ("Server Error") response:
RangeError: Maximum call stack size exceeded
at Server.hasOwnProperty (native)
at _hasBinary (/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/has-binary/index.js:49:45)
at _hasBinary (/home/ubuntu/.nvm/versions/node/v4.4.5/lib/node_modules/sails/node_modules/sails-hook-sockets/node_modules/socket.io/node_modules/has-binary/index.js:49:63)
请帮忙...
我认为 sails.sockets.broadcast()
的参数需要稍微调整一下。也许文档示例已过时或不完整。试试这个:
SayController.js:
module.exports = {
hello: function(req, res) {
// Make sure this is a socket request (not traditional HTTP)
if (!req.isSocket) {return res.badRequest();}
// Have the socket which made the request join the "funSockets" room
sails.sockets.join(req, 'funSockets');
// Broadcast a "hello" message to all the fun sockets.
// This message will be sent to all sockets in the "funSockets" room,
// but will be ignored by any client sockets that are not listening-- i.e. that didn't call `io.socket.on('hello', ...)`
// ▼ this is "data" in io.socket.on('hello', function gotHelloMessage (data)
sails.sockets.broadcast('funSockets', 'hello', {id: 'my id'}, req);
// Respond to the request with an a-ok message
// ▼ The object returned here is "body" in io.socket.get('/say/hello', function gotResponse(body, response)
return res.ok({
message: "OK"
});
}
}