Class: MKIt::ConsoleWebSocketClient

Inherits:
Object
  • Object
show all
Defined in:
lib/mkit/client/console_websocket_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri, options) ⇒ ConsoleWebSocketClient

Returns a new instance of ConsoleWebSocketClient.



10
11
12
13
# File 'lib/mkit/client/console_websocket_client.rb', line 10

def initialize(uri, options)
  @uri = uri
  @options = options
end

Instance Method Details

#doItObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mkit/client/console_websocket_client.rb', line 15

def doIt
  EM.run {
    ws = Faye::WebSocket::Client.new(@uri, nil, @options)

    ws.on :open do |_event|
      puts "Connected to remote server"
      puts "\r\n"
    end

    ws.on :message do |event|
      print event.data
    end

    ws.on :error do |event|
      p [:error, event.message]
      ws = nil
      EventMachine.stop
    end

    ws.on :close do |_event|
      ws = nil
      puts "\r\n"
      EventMachine.stop
    end

    Thread.new do
      STDIN.raw do
        loop do
          input = STDIN.getc.chr
          # if input == "\u0003" # Ctrl+C
          #   puts "bye..."
          #   EventMachine.stop
          #   break
          # else
            ws.send(input)
          # end
        end
      end
    end
  }
end