Class: EM::Pusher::Client::Connection

Inherits:
Connection
  • Object
show all
Includes:
Deferrable
Defined in:
lib/em/pusher/client/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#originObject

Returns the value of attribute origin.



17
18
19
# File 'lib/em/pusher/client/connection.rb', line 17

def origin
  @origin
end

#protocol_versionObject

Returns the value of attribute protocol_version.



16
17
18
# File 'lib/em/pusher/client/connection.rb', line 16

def protocol_version
  @protocol_version
end

#urlObject

Returns the value of attribute url.



15
16
17
# File 'lib/em/pusher/client/connection.rb', line 15

def url
  @url
end

Class Method Details

.connect(uri, opts = {}) {|conn| ... } ⇒ Object

Yields:

  • (conn)


19
20
21
22
23
24
25
26
27
28
# File 'lib/em/pusher/client/connection.rb', line 19

def self.connect(uri, opts = {})
  p_uri = URI.parse(uri)
  conn = EM.connect(p_uri.host, p_uri.port || 80, self) do |c|
    c.url = uri
    c.protocol_version = opts[:version]
    c.origin = opts[:origin]
  end
  yield conn if block_given?
  conn
end

Instance Method Details

#connected(&cback) ⇒ Object



49
50
51
# File 'lib/em/pusher/client/connection.rb', line 49

def connected(&cback)
  @connect = cback
end

#connection_completedObject



35
36
37
38
39
40
41
42
43
# File 'lib/em/pusher/client/connection.rb', line 35

def connection_completed
  @connect.yield if @connect
  @hs = ::WebSocket::Handshake::Client.new(
    url:     @url,
    origin:  @origin,
    version: @protocol_version,
  )
  send_data(@hs.to_s)
end

#connection_established(&cback) ⇒ Object



57
58
59
# File 'lib/em/pusher/client/connection.rb', line 57

def connection_established(&cback)
  @connection_established = cback
end

#disconnect(&cback) ⇒ Object



53
54
55
# File 'lib/em/pusher/client/connection.rb', line 53

def disconnect(&cback)
  @disconnect = cback
end

#ping(&cback) ⇒ Object



61
62
63
# File 'lib/em/pusher/client/connection.rb', line 61

def ping(&cback)
  @ping = cback
end

#post_initObject



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

def post_init
  @handshaked = false
  @frame = ::WebSocket::Frame::Incoming::Client.new
end

#receive_data(data) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/em/pusher/client/connection.rb', line 78

def receive_data(data)
  return handle_received_data(data) if @handshaked
  @hs << data
  if @hs.finished?
    @handshaked = true
    succeed
  end

  receive_data(@hs.leftovers) if @hs.leftovers
end

#send_msg(data, args = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/em/pusher/client/connection.rb', line 89

def send_msg(data, args = {})
  type = args[:type] || :text
  data = data.to_json if data.is_a?(Hash)
  frame = ::WebSocket::Frame::Outgoing::Client.new(
    data: data,
    type: type,
    version: @hs.version,
  )
  send_data(frame.to_s)
end

#stream(&cback) ⇒ Object



45
46
47
# File 'lib/em/pusher/client/connection.rb', line 45

def stream(&cback)
  @stream = cback
end

#subscribe(channel, auth = nil, channel_data = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/em/pusher/client/connection.rb', line 66

def subscribe(channel, auth = nil, channel_data = nil)
  msg = {
    event: 'pusher:subscribe',
    data: {
      channel: channel,
      auth: auth,
      channel_data: channel_data,
    },
  }
  conn.send_msg(msg)
end

#unbindObject



100
101
102
103
# File 'lib/em/pusher/client/connection.rb', line 100

def unbind
  super
  @disconnect.call if @disconnect
end