Class: Forward::Socket

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/forward/socket.rb

Constant Summary collapse

HEART_BEAT_INTERVAL =
10.freeze
WATCH_INTERVAL =
60.freeze
ACTIVITY_TIMEOUT =
60.freeze

Constants included from Common

Common::EMAIL_REGEX

Instance Method Summary collapse

Methods included from Common

#config, #exit_with_error, #exit_with_message, #logged_in?, #logger, #os, #stop_reactor_and_exit, #windows?

Constructor Details

#initialize(tunnel) ⇒ Socket

Returns a new instance of Socket.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/forward/socket.rb', line 8

def initialize(tunnel)
  @tunnel = tunnel
  @socket = Faye::WebSocket::Client.new(@tunnel.tunneler)
  @socket.on :open do |event|
    logger.debug '[socket] open'
    send(type: 'tunnel:identify', tunnelId: @tunnel.id)
  end

  @socket.on :message do |event|
    receive(event.data)
  end

  @socket.on :close do |event|
    logger.debug "[socket] close - code: #{event.code} reason: #{event.reason}"
    if @tunnel.open?
      exit_with_message "Your tunnel has been disconnected"
    else
      @tunnel.destroy { exit_with_error "Unable to open tunnel, please contact us at #{SUPPORT_EMAIL}" }
    end
  end

  @socket.on :error do |event|
    logger.debug "[socket] error #{event.inspect}"
  end
end

Instance Method Details

#send(message, data = nil) ⇒ Object



34
35
36
# File 'lib/forward/socket.rb', line 34

def send(message, data = nil)
  @socket.send(pack(message, data))
end