app.js
var sockect = require('./socket/server')
var users = require('../router/users')
app.use('/users', users); //路由
var server = app.listen(9000, function () {
console.log('server start');
});
sockect(server); //启动socket
server.js
'use strict';
module.exports = function(server) {
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
//如何将这里的socket暴露给其他地方的js方法里使用socket.emit ?
});
return io;
};
上面是我目前的结构,希望在users里面的逻辑里触发emit方法,google了很多,也没有找到有什么办法可以将参数socket传递出去,不然的话所有逻辑只要涉及到socket.emit的都要写在connection的回调里..
In the server.js file:
Then write business logic in users.js
I also encountered this problem, please solve it
I also think that within the scope of external calls
socket
没什么意义。。而且
express
和socket
两者走的协议都不一样,你可以把
user
的身份信息保存在 局部变量socket
use identification information to perform corresponding user operations
Same question, how to expose the socket object? ? ? ?