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 = nil) ⇒ 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=nil)
  @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

  start
end

Instance Attribute Details

#handlerObject

Returns the value of attribute handler.



22
23
24
# File 'lib/celluloid/websocket/client/connection.rb', line 22

def handler
  @handler
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/celluloid/websocket/client/connection.rb', line 28

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
    @client.parse(@socket.readpartial(1024))
  end
end

#startObject



24
25
26
# File 'lib/celluloid/websocket/client/connection.rb', line 24

def start
  async.run if handler
end

#write(buffer) ⇒ Object



48
49
50
# File 'lib/celluloid/websocket/client/connection.rb', line 48

def write(buffer)
  @socket.write buffer
end