Sha256: 9ae75e0aee26653a85844168c3c72f203a090cf5deb472b1d85c0927a237fa4e

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# # frozen_string_literal: true
require 'faye/websocket'
require 'eventmachine'
require 'json'
require 'io/console'

module MKIt
  class ConsoleWebSocketClient

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

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

        ws.on :open do |_event|
          puts "Connected to WebSocket 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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mkit-0.8.0 lib/mkit/client/console_websocket_client.rb