Sha256: 6d4795d44eff76060b7910a279be2f15dc996c64bb90a42fa62de530b21a9a94

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

# The SocketManager provides communication between a Websocket / Pubsub
# system and routes messages through the application to instances
# of the 
class Luca.SocketManager extends Backbone.Model
  defaults:
    autoStart: true  
    providerAvailable: false
    ready: false
    provider: "faye.js"

  initialize: (@attributes={})->
    @loadProviderSource() unless @providerAvailable() is true

    Luca.Model::initialize?.apply(@, arguments)

    model = @

    connectOnReady = ()=> @connect() if @isReady()

    model.on "change", ()->
      connectOnReady()
      model.unbind("change", @)

    @on "ready", _.once ()=> @set('ready', true)

    @trigger "change"

  isReady: ()->
    @get("ready") is true and @get("providerAvailable") is true

  providerAvailable: ()->
    providerTest = switch @get('provider')
      when "socket.io"
        "io"
      when "faye.js"
        "Faye.Client"

    !!(Luca.util.resolve(providerTest)?)

  connect: ()->
    switch @get('provider')
      when "socket.io"
        @client = io.connect( @get('host') )
      when "faye.js"
        @client = new Faye.Client( @get('host')  + ( @get('namespace')  || "") )

  providerSourceLoaded: ()-> 
    @set "providerAvailable", true

  providerSourceUrl: ()->
    switch @get('provider')
      when "socket.io" then "#{  @get('host')  }/socket.io/socket.io.js"
      when "faye.js" then "#{  @get('host')  }/faye.js"

  loadProviderSource: ()->
    script = document.createElement 'script'
    script.setAttribute "type", "text/javascript"
    script.setAttribute "src", @providerSourceUrl()
    script.onload = _.bind(@providerSourceLoaded,@)

    if Luca.util.isIE()
      script.onreadystatechange = ()=>
        if script.readyState is "loaded"
          @providerSourceLoaded()

    document.getElementsByTagName('head')[0].appendChild script

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
luca-0.9.8 app/assets/javascripts/luca/managers/socket_manager.coffee