Class: Celluloid::WebSocket::Client::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
IO
Defined in:
lib/celluloid/websocket/client/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, handler) ⇒ Connection

Returns a new instance of Connection.


10
11
12
13
14
15
16
17
18
19
# File 'lib/celluloid/websocket/client/connection.rb', line 10

def initialize(url, handler)
  @url = url
  uri = URI.parse(url)
  port = uri.port || (uri.scheme == "ws" ? 80 : 443)
  @socket = Celluloid::IO::TCPSocket.new(uri.host, port)
  @client = ::WebSocket::Driver.client(self)
  @handler = handler

  async.run
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url


20
21
22
# File 'lib/celluloid/websocket/client/connection.rb', line 20

def url
  @url
end

Instance Method Details

#runObject


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/celluloid/websocket/client/connection.rb', line 22

def run
  @client.on('open') do |event|
    @handler.async.on_open if @handler.respond_to?(:on_open)
  end
  @client.on('message') do |event|
    @handler.async.on_message(event.data) if @handler.respond_to?(:on_message)
  end
  @client.on('close') do |event|
    @handler.async.on_close(event.code, event.reason) if @handler.respond_to?(:on_close)
  end

  @client.start

  loop do
    begin
      @client.parse(@socket.readpartial(1024))
    rescue EOFError
      break
    end
  end
end

#write(buffer) ⇒ Object


46
47
48
# File 'lib/celluloid/websocket/client/connection.rb', line 46

def write(buffer)
  @socket.write buffer
end