Module: EM::Pusher::Client

Defined in:
lib/em/pusher/client.rb,
lib/em/pusher/client/connection.rb,
lib/em/pusher/client/msg_parser.rb

Defined Under Namespace

Classes: Connection, MsgParser

Constant Summary collapse

DEFAULT_OPTIONS =
{
  app_id: nil,
  app_secret: nil,
  scheme: 'ws',
  port: 80,
  encrypted: 'on',
  protocol: 4,
  version: '4.2',
  client_name: 'em-pusher-client',
}.freeze
REQUIRED_OPTIONS =
%i[key cluster].freeze

Class Method Summary collapse

Class Method Details

.build_auth(secret, key, socket_id, channel_name) ⇒ Object



36
37
38
39
# File 'lib/em/pusher/client.rb', line 36

def self.build_auth(secret, key, socket_id, channel_name)
  sig = sign(secret, socket_id, channel_name)
  "#{key}:#{sig}"
end

.connect(options) ⇒ Object



23
24
25
26
27
28
# File 'lib/em/pusher/client.rb', line 23

def self.connect(options)
  uri = url(options)
  Connection.connect(uri).tap do |conn|
    yield conn if block_given?
  end
end

.sign(secret, socket_id, channel_name) ⇒ Object



30
31
32
33
34
# File 'lib/em/pusher/client.rb', line 30

def self.sign(secret, socket_id, channel_name)
  digest = OpenSSL::Digest::SHA256.new
  string_to_sign = "#{socket_id}:#{channel_name}"
  OpenSSL::HMAC.hexdigest(digest, secret, string_to_sign)
end

.url(options) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/em/pusher/client.rb', line 41

def self.url(options)
  opts = DEFAULT_OPTIONS.merge(options)
  REQUIRED_OPTIONS.each { |opt| fail ArgumentError, "option #{opt} is required" unless opts[opt] }
  "#{opts[:scheme]}://ws-#{opts[:cluster]}.pusher.com:#{opts[:port]}/app/#{opts[:key]}" \
    "?protocol=#{opts[:protocol]}" \
    "&client=#{opts[:client_name]}" \
    "&version=#{opts[:version]}"
end