问题
I am stuck in a problem.
I am making a Socket.IO connection in the bin file which is working, but can anyone tell me how I can export this connection to different controller. This is my bin file code.
var app = require('../app');
var debug = require('debug')('userservice:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3015');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
var io = require('socket.io')(server);
io.on('connection', (socket) => {
console.log('Connection made #######################################################.', socket.id);
socket.on('disconnect', () => {
console.log('Connection disconnected @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.', socket.id);
});
});
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
回答1:
There is plenty of techniques can be used to re-use the socket instance, an easy and simple one is to create a singular class, to be able to:
- Initiate socket instance
- Export the instance to other modules
socket.js:
let io;
module.exports = {
init: (server) => {
io = require('socket.io').listen(server); io.origins('*:*');
return io;
},
get: () => {
if (!io) {
throw new Error("socket is not initialized");
}
return io;
}
};
server.js:
const app = require('../app');
const http = require('http');
/**
* Get port from environment and store in Express.
*/
const port = '3015';
app.set('port', port);
/**
* Create HTTP server.
*/
const server = http.createServer(app);
const io = require('./socket.js').init(server);
io.on('connection', (socket) => {
console.log('Connection success', socket.id);
socket.on('disconnect', () => {
console.log('Connection disconnected', socket.id);
});
}
Now you can use it in other modules.
const io = require('./socket.js').getio();
回答2:
You could just create a socket.io controller module that exports a function that you call for every new connection.
So, in your current server file, you add this:
const {socketConnected} = require('socketController.');
And, you modify this portion to call it for each new socket:
var server = http.createServer(app);
var io = require('socket.io')(server);
io.on('connection', (socket) => {
console.log('Connection made #######################################################.', socket.id);
// tell controller about new socket (and pass the io instance)
socketConnected(socket, io);
socket.on('disconnect', () => {
console.log('Connection disconnected @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@.', socket.id);
});
});
Then, your socket controller can be like this:
module.exports.socketConnected = function(socket, io) {
// new socket.io socket connected
console.log(`controller: got socket.io connection ${socket.id}`);
// register appropriate event handlers on the socket here
}
来源:https://stackoverflow.com/questions/59551831/how-can-i-export-socket-io-connection-to-another-controller