Sha256: 8d46ef266f4fa35916f8e420cd916a0bbc6bb2077e7d1fa2731916cf0025a0e3

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

###
WebSocket Interface for the WebSocketRails client.
###
class WebSocketRails.WebSocketConnection

  constructor: (@url,@dispatcher) ->
    if @url.match(/^wss?:\/\//)
        console.log "WARNING: Using connection urls with protocol specified is depricated"
    else if window.location.protocol == 'http:'
        @url             = "ws://#{@url}"
    else
        @url             = "wss://#{@url}"
    
    @message_queue   = []
    @_conn           = new WebSocket(@url)
    @_conn.onmessage = @on_message
    @_conn.onclose   = @on_close
    @_conn.onerror   = @on_error

  trigger: (event) =>
    if @dispatcher.state != 'connected'
      @message_queue.push event
    else
      @_conn.send event.serialize()

  on_message: (event) =>
    data = JSON.parse event.data
    @dispatcher.new_message data

  on_close: (event) =>
    close_event = new WebSocketRails.Event(['connection_closed', event])
    @dispatcher.state = 'disconnected'
    @dispatcher.dispatch close_event

  on_error: (event) =>
    error_event = new WebSocketRails.Event(['connection_error', event])
    @dispatcher.state = 'disconnected'
    @dispatcher.dispatch error_event

  flush_queue: =>
    for event in @message_queue
      @_conn.send event.serialize()
    @message_queue = []

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
websocket-rails-0.6.2 lib/assets/javascripts/websocket_rails/websocket_connection.js.coffee
websocket-rails-0.6.1 lib/assets/javascripts/websocket_rails/websocket_connection.js.coffee
websocket-rails-0.6.0 lib/assets/javascripts/websocket_rails/websocket_connection.js.coffee
websocket-rails-0.5.0 lib/assets/javascripts/websocket_rails/websocket_connection.js.coffee