如何在中间件中获取socket.io的事件详细信息 [英] How to get event details in middleware for socket.io

查看:67
本文介绍了如何在中间件中获取socket.io的事件详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Node服务器上的每个事件记录事件名称参数.为此,我使用了

I am trying to log the event name and parameter for each event on my Node server. For this purpose I used

io.use(function(socket, next){
  // how to get event name out of socket.
});

现在,我在尝试获取事件名称和参数时陷入困境.在我看来,这似乎是API开发人员的普遍需求,因此,我很确定必须有某种方式可以使该库获得该资源,我已经尝试阅读文档和源代码,但我无法获得这些东西.>

Now, I got stuck while trying to get event name and arguments. To me, it looks like common demand from API dev, so I am pretty sure there must be some way to the library to get that, I have tried to read the docs and source but I am not able to get the stuff.

推荐答案

套接字事件需要正确处理,在任何情况下,如果未处理事件,将不会有响应.

The socket events needs to be handled properly,in any case if an event is not handled there will be no response.

var io = require('socket.io')(server);
var sessionMiddleWare=(session({secret: 'secret key', resave: true, saveUninitialized: true,cookie: { path: '/', httpOnly: true, maxAge: 300000 },rolling: true}));

app.use(sessionMiddleWare)

io.use(function(socket, next) {
  sessionMiddleWare(socket.request, socket.request.res, next);
});

io.on('connection', function(socket) {  // On Socket connection.
   // inside this you can use different events

   //event name and parameters can be found in socket variable.

   console.log(socket.id) // prints the id sent from the client.
   console.log(socket.data) // prints the data sent from the client.

   // example event

   socket.on('subscribe', function(room) {  // Event sample.
        console.log('joining room', room);
        socket.room=room;
        socket.join(room);
    });
})

希望这会有所帮助.

这篇关于如何在中间件中获取socket.io的事件详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆