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.



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

def origin
  @origin
end

#protocol_versionObject

Returns the value of attribute protocol_version.



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

def protocol_version
  @protocol_version
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

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

Yields:

  • (conn)


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

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



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

def connected(&cback)
  @connect = cback
end

#connection_completedObject



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

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

#disconnect(&cback) ⇒ Object



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

def disconnect(&cback)
  @disconnect = cback
end

#post_initObject



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

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

#receive_data(data) ⇒ Object



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

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



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

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



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

def stream(&cback)
  @stream = cback
end

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



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/em/pusher/client/connection.rb', line 57

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



91
92
93
94
# File 'lib/em/pusher/client/connection.rb', line 91

def unbind
  super
  @disconnect.call if @disconnect
end