Sha256: c90de2a0d26fda6b0f004c0398416329ba19608f602398979109ab31385cfda0

Contents?: true

Size: 1014 Bytes

Versions: 7

Compression:

Stored size: 1014 Bytes

Contents

# The channel is the connection between the front end and the backend.

require 'volt/reactive/events'
require 'json'

class Channel
  include Events
  
  def initialize
    @socket = nil
    %x{
      this.socket = new SockJS('http://localhost:3000/channel');//, {reconnect: true});

      this.socket.onopen = function() {
        self['$trigger!']("open");
      };

      this.socket.onmessage = function(message) {
        console.log('received: ', message);
        self['$message_received'](message.data);
      };
    }
  end
  
  def message_received(message)
    message = JSON.parse(message)
    puts "Got #{message.inspect}"
    
    trigger!('message', message)
  end
  
  def send(message)
    # TODO: Temp: wrap message in an array, so we're sure its valid JSON
    message = JSON.dump([message])
    %x{
      //message = window.JSON.parse(message);
      console.log('send: ', message);
      this.socket.send(message);
    }
  end
  
  def close
    %x{
      this.socket.close();
    }
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
volt-0.3.7 lib/volt/templates/channel.rb
volt-0.3.6 lib/volt/templates/channel.rb
volt-0.3.5 lib/volt/templates/channel.rb
volt-0.3.4 lib/volt/templates/channel.rb
volt-0.3.3 lib/volt/templates/channel.rb
volt-0.3.2 lib/volt/templates/channel.rb
volt-0.3.1 lib/volt/templates/channel.rb