在nodejs(express / client)中使用EventEmitter事件?

时间:2016-04-27 19:32:48

标签: angularjs node.js express eventemitter

通过https://github.com/alepez/omxdirector代码,我看到很多片段,其中omxdirector进程正在发出事件

var play = function (videos, options) {
    if (omxProcess) {
      if (!paused) {
        return false;
      }
      sendAction('pause');
      paused = false;
      that.emit('play');
      return true;
    }
    if (!videos) {
      throw new TypeError("No files specified");
    }
    if (typeof videos !== 'string' && !util.isArray(videos)) {
      throw new TypeError("Incorrect value for videos: " + videos);
    }
    open(videos, options);
    that.emit('play');
    return true;
  };

我的问题是谁消耗这些事件?它是在服务器端还是在客户端?

我正在使用socket.io在我的前端angularjs客户端和快速服务器之间进行通信。我的实现如下 -

服务器:响应客户端发出的歌曲:播放'

var omx = require('omxdirector');
omx.setVideoDir('/home/pi/webapp/songs'); 
{...}
 socket.on('song:play', function(data,fn) {
        if(omx.isLoaded()) { 
            if(!omx.isPlaying()) {
                if(data.song === currentSong) {
                    omx.play();
                } else {
                    omx.stop();
                        omx.play(data.song, {audioOutput: 'local'});
                        currentSong = data.song;
                }
            } else {
                omx.stop();
                    omx.play(data.song, {audioOutput: 'local'});
                    currentSong = data.song;
            }
        } else {
                omx.play(data.song, {audioOutput: 'local'});
                currentSong = data.song;
            }
            fn(data);
      });
{...}

客户:发出歌曲:播放'

function playSong(song) {
      socket.emit('song:play', { song: song }, function(data) {
        $scope.currentSong = data.song;
      });     

      // Songs.get({ song: song, action: 'play'}).$promise.then(function(data) {
      //   $scope.currentSong = data.song;
      // });
      $scope.isPlaying = true;
    }

omxdirector Emitter究竟适合哪里? 我不想让这个问题特定于omxdirector,但适用于任何带有EventEmitter实例的nodejs应用程序?谁消耗这些事件?

0 个答案:

没有答案