express.js,socket.io和EventEmitter侦听器之间的交互 [英] Interaction between expressjs, socket.io and EventEmitter listeners

查看:143
本文介绍了express.js,socket.io和EventEmitter侦听器之间的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用expressjs的服务器设置,该服务器使用socket.io与前端客户端进行通信.该服务器在树莓派上运行,并使用mplayer nodejs模块提供后端来控制我的音乐库的mplayer播放器.

I have a server setup using expressjs which communicates with the front end clients using socket.io. The server runs on the raspberry pi and provides a backend to control mplayer player for my music library using the mplayer nodejs module.

大多数更新前端客户端的操作都是基于mplayer EventEmitter模块发出的事件执行的.例如:

Most of the actions to update the clients on the front end are taken based on events emitted by the mplayer EventEmitter module. For example :

当发出停止播放器的命令为player.stop()时,播放器的EventEmitter实例将发出'stop'事件.

When a command to stop the player is issued as player.stop() the EventEmitter instance of the player emits 'stop' event.

然后,服务器侦听此事件,如下所示: player.on('stop', socket.emit('stop'));

The server then listens for this event as follows : player.on('stop', socket.emit('stop'));

我注意到的问题是,随着越来越多的客户端通过套接字io连接到服务器,每个客户端都为mplayer EventEmitter 'stop'事件创建了一个侦听器!因此,例如,如果有3个客户端,我倾向于从播放器EventEmitter接收3个'stop'事件,并因此向我的前端客户端发送3个'stop'事件.

The issue that I have noticed is as more clients connect to the server over socket io each client creates a listener for the mplayer EventEmitter 'stop' event !! So for example if there were 3 clients, I would tend to receive 3 'stop' events from the player EventEmitter and consequently send 3 'stop' events to my front end clients.

我的问题是如何确保仅为播放器事件创建一个侦听器?为什么每个客户端都将自己绑定为EventEmitter事件的侦听器?

My question is how can I ensure that only one listener is created for the player event? Why does every client bind itself as a listener to the EventEmitter event ?

我的服务器端套接字设置如下:

My server side socketio setup looks as follows :

io.on('connection', function(socket) {
    console.log('a user connected @ ' + socket.id);

    ///// Listen for player to successfully stop playing or when EOF is reached
    player.on('stop', function() {
        console.log("Received Stop Event From Player");
        socket.emit('song:next'); //Play next song
    }

当连接3个客户端时收到'stop'事件时控制台输出:

Console output when 3 clients are connected, on receiving the 'stop' event :

Received Stop Event From Player
Received Stop Event From Player
Received Stop Event From Player

这导致我发送3个'next'事件发送给我的所有客户.

This leads me to send 3 'next' events to be send to all my clients.

MPlayer nodejs看起来像这样:

MPlayer nodejs looks like this :

this.player.on('playstop', function() {
    if(options.verbose) {
        console.log('player.stop');
    }
    this.emit('stop')
}.bind(this));

用于nodejs的mplayer

在通过stackoverflow进行进一步调查时,似乎类似于以下问题: 正在监听全局eventEmitter的本地eventEmitter

On further investigation through stackoverflow it seems similar to this question : Local eventEmitter listening to global eventEmitter

但是由于使用addListener和removeListener,注释中提供的解决方案对我来说没有意义?有人可以帮我分解一下,并解释一下我将如何实现在那里提到的内容吗?

But the solution provided in the comments doesn't make sense to me because of the use of addListener and removeListener ?? Can someone break it down for me and explain how I would achieve what is mentioned there ?

推荐答案

问题似乎是这段代码

io.on('connection', function(socket) {
    console.log('a user connected @ ' + socket.id);

    ///// Listen for player to successfully stop playing or when EOF is reached
    player.on('stop', function() {
        console.log("Received Stop Event From Player");
        socket.emit('song:next'); //Play next song
    }

在连接块内放置播放器会导致每个客户端都绑定到该播放器作为侦听器.将所有玩家事件处理转移到全局范围后,问题似乎已经消失了.

Having the player inside the connection block caused every client to bind to the player as a listener. On moving all player event handling in to the global scope the problem seems to have gone.

这篇关于express.js,socket.io和EventEmitter侦听器之间的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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