Class: EventMachine::Wssh::Connect::Dst
- Inherits:
-
Object
- Object
- EventMachine::Wssh::Connect::Dst
- Defined in:
- lib/em/wssh/connect.rb
Constant Summary collapse
- Connect =
Module.nesting[1]
Instance Attribute Summary collapse
-
#http ⇒ Object
Returns the value of attribute http.
Class Method Summary collapse
Instance Method Summary collapse
- #bye ⇒ Object
- #connect!(host) ⇒ Object
-
#initialize(http) ⇒ Dst
constructor
A new instance of Dst.
- #log(*msg) ⇒ Object
- #onclose ⇒ Object
- #onerror(error) ⇒ Object
- #onmessage(data) ⇒ Object
- #onopen ⇒ Object
- #pinger ⇒ Object
- #send(data) ⇒ Object
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
#http ⇒ Object
Returns the value of attribute http.
32 33 34 |
# File 'lib/em/wssh/connect.rb', line 32 def http @http end |
Class Method Details
.count ⇒ Object
36 37 38 39 |
# File 'lib/em/wssh/connect.rb', line 36 def self.count @n||=0 @n+=1 end |
Instance Method Details
#bye ⇒ Object
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.[:uri]}/#{host}" http.onbody @ws = Faye::WebSocket::Client.new uri @ws.on :open do |event| onopen end @ws.on :message do |event| 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 |
#onclose ⇒ Object
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 data http.send_data Array===data ? data.pack('C*') : data end |
#onopen ⇒ Object
67 68 69 70 71 |
# File 'lib/em/wssh/connect.rb', line 67 def onopen log "Connected to WSSHD" http.onopen pinger end |
#pinger ⇒ Object
73 74 75 76 |
# File 'lib/em/wssh/connect.rb', line 73 def pinger return unless t=Connect.[: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 |