Class: EventMachine::Wssh::Client::Ws

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

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Ws

Returns a new instance of Ws.



26
27
28
29
30
31
32
33
34
35
# File 'lib/em/wssh/client.rb', line 26

def initialize uri
  @buf=[]

  @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

Instance Method Details

#byeObject



62
63
64
65
66
67
# File 'lib/em/wssh/client.rb', line 62

def bye
  @ws.close if @ws
  @ws=nil
  @buf=nil
  EM.stop_event_loop
end

#oncloseObject



54
55
56
# File 'lib/em/wssh/client.rb', line 54

def onclose
  bye
end

#onerror(error) ⇒ Object



58
59
60
# File 'lib/em/wssh/client.rb', line 58

def onerror error
  bye
end

#onmessage(data) ⇒ Object



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

def onmessage data
  print data.pack 'C*'
end

#onopenObject



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

def onopen
  @buf.each{|data| @ws.send data}
  @buf=nil
end

#queue(data) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/em/wssh/client.rb', line 37

def queue data
  if @buf
    @buf << data
  else
    @ws.send data
  end
end