Class: EventMachine::WebSocketClient

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/em-websocket-client.rb', line 9

def url
  @url
end

Class Method Details

.connect(uri) ⇒ Object



11
12
13
14
15
16
# File 'lib/em-websocket-client.rb', line 11

def self.connect uri
  p_uri = URI.parse(uri)
  conn = EM.connect(p_uri.host, p_uri.port || 80, self) do |c|
    c.url = uri
  end
end

Instance Method Details

#connected(&cb) ⇒ Object



30
# File 'lib/em-websocket-client.rb', line 30

def connected &cb; @connect = cb; end

#connection_completedObject



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

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

#disconnect(&cb) ⇒ Object



31
# File 'lib/em-websocket-client.rb', line 31

def disconnect &cb; @disconnect = cb; end

#post_initObject



18
19
20
21
# File 'lib/em-websocket-client.rb', line 18

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

#receive_data(data) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/em-websocket-client.rb', line 33

def receive_data data
  if !@handshaked
    @hs << data
    if @hs.finished?
      @handshaked = true
      succeed
    end
  else
    @frame << data
    while msg = @frame.next
      @stream.call(msg) if @stream
    end
  end
end

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



48
49
50
51
52
# File 'lib/em-websocket-client.rb', line 48

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

#stream(&cb) ⇒ Object



29
# File 'lib/em-websocket-client.rb', line 29

def stream &cb; @stream = cb; end

#unbindObject



54
55
56
57
# File 'lib/em-websocket-client.rb', line 54

def unbind
  super
  @disconnect.call if @disconnect
end