Sha256: c120a614b1131a2c35f3f2fa27b196639fceb25751bc7495030feb04e9c5d930

Contents?: true

Size: 811 Bytes

Versions: 8

Compression:

Stored size: 811 Bytes

Contents

require 'faye/websocket'
require 'eventmachine'

module Slack
  module RealTime
    class Client
      def initialize(url)
        @url = url
        @callbacks ||= {}
      end

      def on(type, &block)
        @callbacks[type] ||= []
        @callbacks[type] << block
      end

      def start
        EM.run do
          ws = Faye::WebSocket::Client.new(@url)

          ws.on :open do |event|
          end

          ws.on :message do |event|
            data = JSON.parse(event.data)
            if !data["type"].nil? && !@callbacks[data["type"].to_sym].nil?
              @callbacks[data["type"].to_sym].each do |c|
                c.call data
              end
            end
          end

          ws.on :close do |event|
            EM.stop
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
slack-api-1.3.0 lib/slack/realtime/client.rb
slack-api-1.2.4 lib/slack/realtime/client.rb
slack-api-1.2.3 lib/slack/realtime/client.rb
slack-api-1.2.2 lib/slack/realtime/client.rb
slack-api-1.2.1 lib/slack/realtime/client.rb
slack-api-1.2.0 lib/slack/realtime/client.rb
slack-api-1.1.6 lib/slack/realtime/client.rb
slack-api-1.1.4 lib/slack/realtime/client.rb