Class: WSocketIO::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/wsocket_io.rb

Overview

─── Channel ────────────────────────────────────────────────

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, send_fn) ⇒ Channel

Returns a new instance of Channel.



118
119
120
121
122
123
124
# File 'lib/wsocket_io.rb', line 118

def initialize(name, send_fn)
  @name = name
  @send_fn = send_fn
  @message_cbs = []
  @history_cbs = []
  @presence = Presence.new(name, send_fn)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



116
117
118
# File 'lib/wsocket_io.rb', line 116

def name
  @name
end

#presenceObject (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 handle_message(data, meta)
  @message_cbs.each { |cb| cb.call(data, meta) }
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

#unsubscribeObject



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