Class: Caldecott::Client::WebSocketTunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/caldecott/client/websocket_tunnel.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger, url, dst_host, dst_port, auth_token) ⇒ WebSocketTunnel

Returns a new instance of WebSocketTunnel.



8
9
10
# File 'lib/caldecott/client/websocket_tunnel.rb', line 8

def initialize(logger, url, dst_host, dst_port, auth_token)
  @ws = EM::HttpRequest.new("#{url}/websocket/#{dst_host}/#{dst_port}").get :timeout => 0
end

Instance Method Details

#closeObject



31
32
33
# File 'lib/caldecott/client/websocket_tunnel.rb', line 31

def close
  @ws.close_connection_after_writing
end

#onclose(&blk) ⇒ Object



16
17
18
19
# File 'lib/caldecott/client/websocket_tunnel.rb', line 16

def onclose(&blk)
  @ws.errback { blk.call }
  @ws.disconnect { blk.call }
end

#onopen(&blk) ⇒ Object



12
13
14
# File 'lib/caldecott/client/websocket_tunnel.rb', line 12

def onopen(&blk)
  @ws.callback { blk.call }
end

#onreceive(&blk) ⇒ Object



21
22
23
# File 'lib/caldecott/client/websocket_tunnel.rb', line 21

def onreceive(&blk)
  @ws.stream { |data| blk.call(Base64.decode64(data)) }
end

#send_data(data) ⇒ Object



25
26
27
28
29
# File 'lib/caldecott/client/websocket_tunnel.rb', line 25

def send_data(data)
  # Um.. as soon as the em websocket object adds a better named
  # method for this, start using it.
  @ws.send(Base64.encode64(data))
end