如何在整个服务器上监听事件并在每次调用时传入套接字,而不必为每个套接字生成和存储新的状态数据?或者资源方面不是那么昂贵吗?
io.on('connection', function(socket){
//do stuff here and make variables
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('message', function(){
console.log('user sent message', JSON.stringify(arguments,null,2));
//handle the message here.
});
});
相反,我只是想这样做:
io.on('message', function(socket){
console.log('io on message' , arguments);
});