Class: WSocketIO::Channel
- Inherits:
-
Object
- Object
- WSocketIO::Channel
- Defined in:
- lib/wsocket_io.rb
Overview
─── Channel ────────────────────────────────────────────────
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#presence ⇒ Object
readonly
Returns the value of attribute presence.
Instance Method Summary collapse
- #handle_history(result) ⇒ Object
- #handle_message(data, meta) ⇒ Object
- #history(limit: nil, before: nil, after: nil, direction: nil) ⇒ Object
-
#initialize(name, send_fn) ⇒ Channel
constructor
A new instance of Channel.
- #on_history(&block) ⇒ Object
- #publish(data, persist: nil) ⇒ Object
- #subscribe(&callback) ⇒ Object
- #unsubscribe ⇒ Object
Constructor Details
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
116 117 118 |
# File 'lib/wsocket_io.rb', line 116 def name @name end |
#presence ⇒ Object (readonly)
Returns the value of attribute presence.
116 117 118 |
# File 'lib/wsocket_io.rb', line 116 def presence @presence end |
Instance Method Details
#handle_history(result) ⇒ Object
164 165 166 |
# File 'lib/wsocket_io.rb', line 164 def handle_history(result) @history_cbs.each { |cb| cb.call(result) } end |
#handle_message(data, meta) ⇒ Object
160 161 162 |
# File 'lib/wsocket_io.rb', line 160 def (data, ) @message_cbs.each { |cb| cb.call(data, ) } end |
#history(limit: nil, before: nil, after: nil, direction: nil) ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/wsocket_io.rb', line 145 def history(limit: nil, before: nil, after: nil, direction: nil) opts = { action: 'history', channel: @name } opts[:limit] = limit if limit opts[:before] = before if before opts[:after] = after if after opts[:direction] = direction if direction @send_fn.call(opts) self end |
#on_history(&block) ⇒ Object
155 156 157 158 |
# File 'lib/wsocket_io.rb', line 155 def on_history(&block) @history_cbs << block self end |
#publish(data, persist: nil) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/wsocket_io.rb', line 138 def publish(data, persist: nil) msg = { action: 'publish', channel: @name, data: data, id: SecureRandom.uuid } msg[:persist] = persist unless persist.nil? @send_fn.call(msg) self end |
#subscribe(&callback) ⇒ Object
126 127 128 129 130 |
# File 'lib/wsocket_io.rb', line 126 def subscribe(&callback) @message_cbs << callback if callback @send_fn.call({ action: 'subscribe', channel: @name }) self end |
#unsubscribe ⇒ Object
132 133 134 135 136 |
# File 'lib/wsocket_io.rb', line 132 def unsubscribe @send_fn.call({ action: 'unsubscribe', channel: @name }) @message_cbs.clear self end |