我的套接字代码如下所示:
let socketObject = {};
const connectSocket = (io) => {
//listening to socket server
io.on('connection', (socket) => {
socketObject = socket;
console.log('a user is connected');
socket.on('disconnect', () => {
console.log('a user is disconnected');
})
});
};
//this emits data to a particular socket channel
const emitData = (channel, data) => {
socketObject.emit(channel, data, (info) => {
console.log('info is ' + info);
});
};
module.exports = {
connectSocket,
emitData
}
在这里,几秒钟后a user is disconnected