Sha256: c269e4fd503242d5938205e390d6cb8ff1c29ed3e49f37413416a90f866419f5
Contents?: true
Size: 1.58 KB
Versions: 4
Compression:
Stored size: 1.58 KB
Contents
require 'eventmachine' require 'faye/websocket' module Slack class RealTime attr_reader :session def initialize(session) @session = session @events = Registry.new end def broadcast(channel, message, options={}) attributes = { channel: channel, text: message }.merge(options) connection.send Message.generate(attributes).to_json end def run(queue=nil, options={}) EM.run do connect(options) connection.on(:open) { |event| EM.defer { @events.notify(:open, event) }} connection.on(:message) { |event| send_message(event) } connection.on(:close) do |event| EM.defer { @events.notify(:close, event) } shutdown end connection.on(:error) { |event| EM.defer { @events.notify(:error, event) }} queue << connection if queue end end def on(event=nil, handler=nil, func=:update, &blk) @events.register event, handler, func, &blk self end def off(event=nil, handler=nil, func=:update, &blk) @events.unregister event, handler, func, &blk end def shutdown connection.close if connection EM.stop if EM.reactor_running? @events.clear end private attr_reader :connection def connect(options={}) @connection ||= Faye::WebSocket::Client.new(session.url, nil, default_options.merge(options)) end def default_options { ping: 10 } end def send_message(event) message = Message.parse event.data EM.defer { @events.notify(message.type, message) } end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
laziness-0.1.9 | lib/laziness/real_time.rb |
laziness-0.1.8 | lib/laziness/real_time.rb |
laziness-0.1.7 | lib/laziness/real_time.rb |
laziness-0.1.6 | lib/laziness/real_time.rb |