Class: Relay

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/relay.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag, xff = nil) ⇒ Relay

Returns a new instance of Relay.



5
6
7
8
9
10
11
12
13
14
# File 'lib/relay.rb', line 5

def initialize(tag, xff = nil)
  super
  pause
  @tag = tag
  @xff = xff
  @remote_port, @remote_ip = unpack_sockaddr_in(get_peername)
  @websocket = nil
  @ws_remote_ip = nil
  @ws_remote_port = nil
end

Instance Method Details

#receive_data(data) ⇒ Object



28
29
30
31
# File 'lib/relay.rb', line 28

def receive_data(data)
  log('sent', data.bytesize)
  @websocket.send_binary(data)
end

#start(websocket) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/relay.rb', line 16

def start(websocket)
  @websocket = websocket
  @websocket.onbinary do |msg|
    log('recv', msg.bytesize)
    send_data(msg)
  end
  @websocket.onerror { |err| log('ws/error', err) }
  @websocket.onclose { close_connection_after_writing }
  @ws_remote_port, @ws_remote_ip = unpack_sockaddr_in(websocket.get_peername)
  resume
end

#unbindObject



33
34
35
36
37
# File 'lib/relay.rb', line 33

def unbind
  log('close')
  # Status code 1000 indicates indicates a normal closure.
  @websocket&.close(1000) if @websocket&.state == :connected
end