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.



36
37
38
# File 'lib/em-websocket-client.rb', line 36

def url
  @url
end

Class Method Details

.connect(uri) ⇒ Object



38
39
40
41
42
43
# File 'lib/em-websocket-client.rb', line 38

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

#connection_completedObject



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

def connection_completed
  @hs = LibWebSocket::OpeningHandshake::Client.new(:url => @url)
  send_data @hs.to_s
end

#disconnect(&cb) ⇒ Object



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

def disconnect &cb; @disconnect = cb; end

#post_initObject



45
46
47
48
# File 'lib/em-websocket-client.rb', line 45

def post_init
  @handshaked = false
  @frame  = LibWebSocket::Frame.new
end

#receive_data(data) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/em-websocket-client.rb', line 58

def receive_data data
  if !@handshaked
    result = @hs.parse data
    fail @hs.error unless result
    
    if @hs.done?
      @handshaked = true
      succeed
    end
  else
    @frame.append(data)
    while msg = @frame.next
      @stream.call(msg) if @stream
    end
  end
end

#send_msg(s) ⇒ Object



75
76
77
78
# File 'lib/em-websocket-client.rb', line 75

def send_msg s
  frame = LibWebSocket::Frame.new(s)
  send_data frame.to_s
end

#stream(&cb) ⇒ Object



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

def stream &cb; @stream = cb; end

#unbindObject



80
81
82
83
# File 'lib/em-websocket-client.rb', line 80

def unbind
  super
  @disconnect.call if @disconnect
end