Class: Channel

Inherits:
Object show all
Includes:
ReactiveTags
Defined in:
lib/volt/page/channel.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReactiveTags

included, #reactive_method_tag

Constructor Details

#initializeChannel

Returns a new instance of Channel.



11
12
13
14
15
16
17
18
19
20
# File 'lib/volt/page/channel.rb', line 11

def initialize
  @socket = nil
  @status = :opening
  @connected = false
  @error = nil
  @retry_count = 0
  @queue = []

  connect!
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



9
10
11
# File 'lib/volt/page/channel.rb', line 9

def error
  @error
end

#reconnect_intervalObject (readonly)

Returns the value of attribute reconnect_interval.



9
10
11
# File 'lib/volt/page/channel.rb', line 9

def reconnect_interval
  @reconnect_interval
end

#statusObject (readonly)

Returns the value of attribute status.



9
10
11
# File 'lib/volt/page/channel.rb', line 9

def status
  @status
end

Instance Method Details

#close!Object



110
111
112
113
114
115
# File 'lib/volt/page/channel.rb', line 110

def close!
  @status = :closed
  %x{
    this.socket.close();
  }
end

#closed(error) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/volt/page/channel.rb', line 61

def closed(error)
  @status = :closed
  @connected = false
  @error = `error.reason`

  trigger!('closed')
  trigger!('changed')

  reconnect!
end

#connect!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/volt/page/channel.rb', line 30

def connect!
  %x{
    this.socket = new SockJS('/channel');

    this.socket.onopen = function() {
      self.$opened();
    };

    this.socket.onmessage = function(message) {
      self['$message_received'](message.data);
    };

    this.socket.onclose = function(error) {
      self.$closed(error);
    };
  }
end

#connected?Boolean

Returns:



22
23
24
# File 'lib/volt/page/channel.rb', line 22

def connected?
  @connected
end

#message_received(message) ⇒ Object



90
91
92
93
# File 'lib/volt/page/channel.rb', line 90

def message_received(message)
  message = JSON.parse(message)
  trigger!('message', nil, *message)
end

#openedObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/volt/page/channel.rb', line 48

def opened
  @status = :open
  @connected = true
  @reconnect_interval = nil
  @retry_count = 0
  @queue.each do |message|
    send_message(message)
  end

  trigger!('open')
  trigger!('changed')
end

#reconnect!Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/volt/page/channel.rb', line 72

def reconnect!
  @status = :reconnecting
  @reconnect_interval ||= 0
  @reconnect_interval += (2000 + rand(5000))
  @retry_count += 1

  # Trigger changed for reconnect interval
  trigger!('changed')

  interval = @reconnect_interval

  %x{
    setTimeout(function() {
      self['$connect!']();
    }, interval);
  }
end

#retry_countObject



26
27
28
# File 'lib/volt/page/channel.rb', line 26

def retry_count
  @retry_count
end

#send_message(message) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/volt/page/channel.rb', line 98

def send_message(message)
  if @status != :open
    @queue << message
  else
    # TODO: Temp: wrap message in an array, so we're sure its valid JSON
    message = JSON.dump([message])
    %x{
      this.socket.send(message);
    }
  end
end