Last Updated: September 27, 2021
·
2.083K
· tjbladez

Simple Music Broadcasting Server+Client

server - express.js + faye.js

express = require('express')
faye    = require('faye')
server  = express.createServer()
port    = process.env.PORT || 3232
bayeux  = new faye.NodeAdapter mount: '/faye', timeout: 45
client  = bayeux.getClient()

server.configure ->
  server.set 'views', __dirname + '/views'
  server.use express.methodOverride()
  server.use express.bodyParser()
  server.use express.static(__dirname + '/public')
  server.use server.router

server.get '/', (req, res) ->
  res.render 'index.jade', locals: { title: 'Music'}

server.post '/play', (req, res) ->
  res.send("bad params", 400) unless req.body.sound?
  client.publish '/play', {action: 'play', sound: req.body.sound}
  res.send(200)

bayeux.attach server
server.listen port

client - impact.js + faye.js

ig.module
 'game.main'
.requires
    'impact.game',
    'impact.font',
    'impact.sound',
    'plugins.impfaye'
.defines ->
  Music = ig.Game.extend
    init: ->
      opts = {onHandshake: @subscribe}
      @faye = new ig.ImpFaye(@, opts)
    subscribe: ->
      @subscription = @faye.subscribe('play', @musicHandler, @)
    update: -> 
      @parent()
    draw: -> 
      @parent()
    musicHandler: (msg) ->
      if msg.action is 'play' and not ig.soundManager.isPlaying
        @sound = new ig.Sound('media/music/'+msg.sound+'.*')
        @sound.play()
  ig.main('#canvas', Music, 60, 320, 240, 1)