Class: Rookout::ComWs::WebsocketConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/rookout/com_ws/websocket_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, proxy) ⇒ WebsocketConnection

Returns a new instance of WebsocketConnection.



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rookout/com_ws/websocket_client.rb', line 88

def initialize url, proxy
  @url = url
  @proxy = proxy

  @uri = URI.parse @url
  @secure = %w[https wss].include? @uri.scheme

  @socket = nil

  @proxy_connected = false
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



128
129
130
# File 'lib/rookout/com_ws/websocket_client.rb', line 128

def url
  @url
end

Instance Method Details

#closeObject



138
139
140
141
142
143
# File 'lib/rookout/com_ws/websocket_client.rb', line 138

def close
  return if @socket.nil?

  @socket.close
  @socket = nil
end

#connect(driver) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/rookout/com_ws/websocket_client.rb', line 100

def connect driver
  @socket = tcp_connect

  proxy_connect driver if @proxy

  @socket = ssl_connect @socket if @secure
end

#proxy_connect(driver) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rookout/com_ws/websocket_client.rb', line 108

def proxy_connect driver
  connection_error = nil
  @proxy_connected = false
  proxy = driver.proxy @proxy

  proxy.on :connect do
    @proxy_connected = true
  end

  proxy.on :error do |error|
    connection_error = error
  end

  proxy.start

  proxy.parse read_char until @proxy_connected == true || !connection_error.nil?

  raise Exceptions::RookProxyException, connection_error unless connection_error.nil?
end

#read_charObject



130
131
132
# File 'lib/rookout/com_ws/websocket_client.rb', line 130

def read_char
  @socket.getc
end

#write(buffer) ⇒ Object



134
135
136
# File 'lib/rookout/com_ws/websocket_client.rb', line 134

def write buffer
  @socket << buffer
end