Class: Charai::WebSocket::DriverImpl

Inherits:
Object
  • Object
show all
Defined in:
lib/charai/web_socket.rb

Overview

providing #url, #write(string)

Defined Under Namespace

Classes: SecureSocketFactory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ DriverImpl

Returns a new instance of DriverImpl.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/charai/web_socket.rb', line 22

def initialize(url)
  @url = url

  endpoint = URI.parse(url)
  @socket =
    if endpoint.scheme == 'wss'
      SecureSocketFactory.new(endpoint.host, endpoint.port).create
    else
      TCPSocket.new(endpoint.host, endpoint.port)
    end
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



34
35
36
# File 'lib/charai/web_socket.rb', line 34

def url
  @url
end

Instance Method Details

#disposeObject



50
51
52
# File 'lib/charai/web_socket.rb', line 50

def dispose
  @socket.close
end

#readpartial(maxlen = 1024) ⇒ Object



44
45
46
47
48
# File 'lib/charai/web_socket.rb', line 44

def readpartial(maxlen = 1024)
  @socket.readpartial(maxlen)
rescue Errno::ECONNRESET
  raise EOFError.new('closed by remote')
end

#write(data) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/charai/web_socket.rb', line 36

def write(data)
  @socket.write(data)
rescue Errno::EPIPE
  raise EOFError.new('already closed')
rescue Errno::ECONNRESET
  raise EOFError.new('closed by remote')
end