class Slanger::Connection

Attributes

socket[RW]
socket_id[RW]

Public Class Methods

new(socket, socket_id=nil) click to toggle source
# File lib/slanger/connection.rb, line 7
def initialize socket, socket_id=nil
  @socket, @socket_id = socket, socket_id
end

Public Instance Methods

error(e) click to toggle source
# File lib/slanger/connection.rb, line 21
def error e
  begin
    send_payload nil, 'pusher:error', e
  rescue EventMachine::WebSocket::WebSocketError
    # Raised if connecection already closed. Only seen with Thor load testing tool
  end
end
establish() click to toggle source
# File lib/slanger/connection.rb, line 29
def establish
  @socket_id = "%d.%d" % [Process.pid, SecureRandom.random_number(10 ** 6)]

  send_payload nil, 'pusher:connection_established', {
    socket_id: @socket_id,
    activity_timeout: Slanger::Config.activity_timeout
  }
end
send_message(m) click to toggle source
# File lib/slanger/connection.rb, line 11
def send_message m
  msg = Oj.load m
  s = msg.delete 'socket_id'
  socket.send Oj.dump(msg, mode: :compat) unless s == socket_id
end
send_payload(*args) click to toggle source
# File lib/slanger/connection.rb, line 17
def send_payload *args
  socket.send format(*args)
end

Private Instance Methods

format(channel_id, event_name, payload = {}) click to toggle source
# File lib/slanger/connection.rb, line 40
def format(channel_id, event_name, payload = {})
  body = { event: event_name, data: Oj.dump(payload, mode: :compat) }
  body[:channel] = channel_id if channel_id
  Oj.dump(body, mode: :compat)
end