I'm trying to use Socket.io with Node.js and emit to a socket within the logic of a route.
I have a fairly standard Express 3 setup with a server.js file that sits in the route, and then I have an index.js which sits in a routes folders that exports all the pages/publically accessible functions of the site. So they look like:
exports.index = function (req, res) {
res.render('index', {
title: "Awesome page"
});
};
with the routing defined in server.js like:
app.get('/',routes.index);
I'm assuming I have to create the socket.io object in the server.js, since it needs the server object, but how can I access that object and emit to it from the index.js export functions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…