Class: Rookout::ComWs::WebsocketClient

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

Instance Method Summary collapse

Constructor Details

#initialize(url, proxy, token) ⇒ WebsocketClient

Returns a new instance of WebsocketClient.



11
12
13
14
15
16
# File 'lib/rookout/com_ws/websocket_client.rb', line 11

def initialize url, proxy, token
  @token = token
  @connection = WebsocketConnection.new url, proxy
  @proxy = proxy
  @driver = nil
end

Instance Method Details

#closeObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rookout/com_ws/websocket_client.rb', line 62

def close
  return if @driver.nil?

  begin
    @driver.close
  rescue RuntimeError, Errno::EPIPE, OpenSSL::OpenSSLError
    # Protocol close may fail if the connection is already closed
    nil
  end
  @connection.close
end

#connectObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rookout/com_ws/websocket_client.rb', line 18

def connect
  connection_error = nil
  @driver = WebSocket::Driver.client @connection

  headers.each do |key, value|
    @driver.set_header key, value
  end

  @driver.on :error do |error|
    connection_error = error
  end

  # Connect to the remote server
  # TODO: ADD CONNECT TIMEOUT
  @connection.connect @driver
  @driver.start

  while @driver.state == :connecting
    recv_data = @connection.read_char
    @driver.parse recv_data
  end

  raise Exceptions::RookWebsocketException, connection_error if @driver.state != :open
end

#connection_pump(message_handler) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/rookout/com_ws/websocket_client.rb', line 43

def connection_pump message_handler
  @driver.on :message do |e|
    message_handler.call e.data
  end

  until @driver.state == :closed
    recv_data = @connection.read_char
    @driver.parse recv_data
  end
end

#ping(message, &callback) ⇒ Object



58
59
60
# File 'lib/rookout/com_ws/websocket_client.rb', line 58

def ping message, &callback
  @driver.ping message, &callback
end

#send_frame(msg) ⇒ Object



54
55
56
# File 'lib/rookout/com_ws/websocket_client.rb', line 54

def send_frame msg
  @driver.binary msg
end