Sha256: 241ea908282356644a59c7df4a63222c655bacd2d9400d1d0a830e67707e30fa

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

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() {
        console.log('open');
        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

6 entries across 6 versions & 1 rubygems

Version Path
volt-0.3.0 lib/volt/templates/channel.rb
volt-0.2.9 lib/volt/templates/channel.rb
volt-0.2.7 lib/volt/templates/channel.rb
volt-0.2.5 lib/volt/templates/channel.rb
volt-0.2.4 lib/volt/templates/channel.rb
volt-0.2.3 lib/volt/templates/channel.rb