Class: EventMachine::Wssh::Connect::Dst

Inherits:
Object
  • Object
show all
Defined in:
lib/em/wssh/connect.rb

Constant Summary collapse

Connect =
Module.nesting[1]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ Dst

Returns a new instance of Dst.



41
42
43
44
# File 'lib/em/wssh/connect.rb', line 41

def initialize(http)
  self.http=http
  @count=self.class.count
end

Instance Attribute Details

#httpObject

Returns the value of attribute http.



32
33
34
# File 'lib/em/wssh/connect.rb', line 32

def http
  @http
end

Class Method Details

.countObject



36
37
38
39
# File 'lib/em/wssh/connect.rb', line 36

def self.count
  @n||=0
  @n+=1
end

Instance Method Details

#byeObject



92
93
94
95
96
97
# File 'lib/em/wssh/connect.rb', line 92

def bye
  http.close_connection if http
  @ws.close if @ws
  @timer.cancel if @timer
  instance_variables.each{|v| remove_instance_variable v if '@count'!=v.to_s}
end

#connect!(host) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/em/wssh/connect.rb', line 54

def connect! host
  log "Redirect to", uri="#{Connect.options[:uri]}/#{host}"

  http.onbody

  @ws = Faye::WebSocket::Client.new uri

  @ws.on :open do |event| onopen end
  @ws.on :message do |event| onmessage event.data end
  @ws.on :close do |event| onclose end
  @ws.on :error do |error| onerror error end
end

#log(*msg) ⇒ Object



46
47
48
# File 'lib/em/wssh/connect.rb', line 46

def log *msg
  Connect.log "<#{@count}>", *msg
end

#oncloseObject



87
88
89
90
# File 'lib/em/wssh/connect.rb', line 87

def onclose
  log "Websocket closed"
  bye
end

#onerror(error) ⇒ Object



82
83
84
85
# File 'lib/em/wssh/connect.rb', line 82

def onerror error
  log "Websocket error", error
  bye
end

#onmessage(data) ⇒ Object



78
79
80
# File 'lib/em/wssh/connect.rb', line 78

def onmessage data
  http.send_data Array===data ? data.pack('C*') : data
end

#onopenObject



67
68
69
70
71
# File 'lib/em/wssh/connect.rb', line 67

def onopen
  log "Connected to WSSHD"
  http.onopen
  pinger
end

#pingerObject



73
74
75
76
# File 'lib/em/wssh/connect.rb', line 73

def pinger
  return unless t=Connect.options[:ping]
  @timer=EM::PeriodicTimer.new(t){@ws.ping if @ws}
end

#send(data) ⇒ Object



50
51
52
# File 'lib/em/wssh/connect.rb', line 50

def send data
  @ws.send data.unpack 'C*' if data.length>0
end