Class: Channel

Inherits:
Object show all
Includes:
Events
Defined in:
lib/volt/templates/channel.rb

Instance Attribute Summary

Attributes included from Events

#scope

Instance Method Summary collapse

Methods included from Events

#add_event_follower, #add_event_to_chains, #add_following, #all_listeners_for, #event_chain, #event_followers, #event_followings, #listeners, #on, #remove_event_follower, #remove_event_from_chains, #remove_following, #remove_listener, #trigger!, #trigger_by_scope!

Constructor Details

#initializeChannel

Returns a new instance of Channel.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/volt/templates/channel.rb', line 9

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

Instance Method Details

#closeObject



42
43
44
45
46
# File 'lib/volt/templates/channel.rb', line 42

def close
  %x{
    this.socket.close();
  }
end

#message_received(message) ⇒ Object



25
26
27
28
29
30
# File 'lib/volt/templates/channel.rb', line 25

def message_received(message)
  message = JSON.parse(message)
  puts "Got #{message.inspect}"
  
  trigger!('message', message)
end

#send(message) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/volt/templates/channel.rb', line 32

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