Module: Peatio::Ranger

Defined in:
lib/peatio/ranger.rb

Defined Under Namespace

Classes: Connection

Class Method Summary collapse

Class Method Details

.run!(jwt_public_key) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/peatio/ranger.rb', line 88

def self.run!(jwt_public_key)
  host = ENV["RANGER_HOST"] || "0.0.0.0"
  port = ENV["RANGER_PORT"] || "8081"

  authenticator = Peatio::Auth::JWTAuthenticator.new(jwt_public_key)

  logger = Peatio::Logger.logger
  logger.info "Starting the server on port #{port}"

  EM.run do
    Peatio::MQ::Client.new
    Peatio::MQ::Client.connect!
    Peatio::MQ::Client.create_channel!

    Peatio::MQ::Events.subscribe!

    EM::WebSocket.start(
      host: host,
      port: port,
      secure: false
    ) do |socket|
      connection = Connection.new(authenticator, socket, logger)

      socket.onopen do |hs|
        connection.handshake(hs)
      end

      socket.onmessage do |msg|
        connection.handle(msg)
      end

      socket.onping do |value|
        logger.info "Received ping: #{value}"
      end

      socket.onclose do
        logger.info "ranger: websocket connection closed"
      end

      socket.onerror do |e|
        logger.error "ranger: WebSocket Error: #{e.message}"
      end
    end

    yield if block_given?
  end
end