如果我想使用 sails.io
,哪里是放置我的逻辑的好地方? config/bootstrap.js
是放置它的好地方吗?或者我可以在其他地方创建一些其他文件吗?
以下代码有效:
// config/bootstrap.js
module.exports.bootstrap = function (cb) {
sails.io.sockets.on('connection', function(socket) {
console.log("Got a connected client");
});
cb();
};
最佳答案
直到 0.9.4
才支持它。
第 1 步。获取最新版本的 sails.js
第 2 步。使用 cli 生成 sails
第三步,参见config/sockets.js
,自定义onConnect
函数,如下:
module.exports.sockets = {
// This custom onConnect function will be run each time AFTER a new socket connects
// (To control whether a socket is allowed to connect, check out `authorization` config.)
// Keep in mind that Sails' RESTful simulation for sockets
// mixes in socket.io events for your routes and blueprints automatically.
onConnect: function(session, socket) {
// By default: do nothing
// This is a good place to subscribe a new socket to a room, inform other users that
// someone new has come online, or any other custom socket.io logic
console.log("Got a connected client");
},
...
关于javascript - 在 sails.js 中放置 socket.io 逻辑的好地方在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18886010/