Class: Channel
Instance Attribute Summary collapse
Instance Method Summary
collapse
included, #reactive_method_tag
Constructor Details
Returns a new instance of Channel.
11
12
13
14
15
16
17
18
|
# File 'lib/volt/page/channel.rb', line 11
def initialize
@socket = nil
@state = :opening
@error = nil
@queue = []
connect!
end
|
Instance Attribute Details
Returns the value of attribute error.
9
10
11
|
# File 'lib/volt/page/channel.rb', line 9
def error
@error
end
|
#reconnect_interval ⇒ Object
Returns the value of attribute reconnect_interval.
9
10
11
|
# File 'lib/volt/page/channel.rb', line 9
def reconnect_interval
@reconnect_interval
end
|
Returns the value of attribute state.
9
10
11
|
# File 'lib/volt/page/channel.rb', line 9
def state
@state
end
|
Instance Method Details
96
97
98
99
100
101
|
# File 'lib/volt/page/channel.rb', line 96
def close!
@state = :closed
%x{
this.socket.close();
}
end
|
#closed(error) ⇒ Object
49
50
51
52
53
54
55
56
57
|
# File 'lib/volt/page/channel.rb', line 49
def closed(error)
@state = :closed
@error = `error.reason`
trigger!('closed')
trigger!('changed')
reconnect!
end
|
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/volt/page/channel.rb', line 20
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
|
#message_received(message) ⇒ Object
75
76
77
78
79
|
# File 'lib/volt/page/channel.rb', line 75
def message_received(message)
message = JSON.parse(message)
trigger!('message', nil, *message)
end
|
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/volt/page/channel.rb', line 38
def opened
@state = :open
@reconnect_interval = nil
@queue.each do |message|
send_message(message)
end
trigger!('open')
trigger!('changed')
end
|
#reconnect! ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/volt/page/channel.rb', line 59
def reconnect!
@reconnect_interval ||= 0
@reconnect_interval += (2000 + rand(5000))
trigger!('changed')
interval = @reconnect_interval
%x{
setTimeout(function() {
self['$connect!']();
}, interval);
}
end
|
#send_message(message) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/volt/page/channel.rb', line 84
def send_message(message)
if @state != :open
@queue << message
else
message = JSON.dump([message])
%x{
this.socket.send(message);
}
end
end
|