A
A
ap4nova2018-01-22 01:02:58
ruby
ap4nova, 2018-01-22 01:02:58

ruby. How to set up a connection to the site through the websocket protocol?

There is a program that connects to the server and receives some data from it + signals every 15 or 60 seconds.

require 'faye/websocket'
require 'eventmachine'

data = []
count = 0

EM.run {
ws = Faye::WebSocket::Client.new('wss://olymptrade.com/ws2')


ws.on :open do |event|
p [:open]
ws.send('{"uuid":"JCBQ7XBRMYSL0JB4N5","pair":"Bitcoin","size":60}')
end

ws.on :message do |event|
p [:message, event.data]
    data << event.data
    data_servertime = data[0].gsub(/[^\d]/, '').to_i
    data.delete_at(0)

    if ((data_servertime % 15) == 0)
        puts "Прошло 15 секунд"
    elsif ((data_servertime % 60) == 0)
        puts "Прошло 60 секунд"
    end
end

ws.on :close do |event|
p [:close, event.code, event.reason]

ws = nil
end

}

When launched, it constantly prints the received data to the console:
[:message, "{\"pair\":\"Bitcoin\",\"time\":1516567298,\"open\":11146.938,\"low\":11146.938,\"high\":11146.938,\"close\":11146.938}"]

[:message, "{\"servertime\":1516567298}"]

Questions:
  1. How do I put the rest of the data (except servertime) into the array, i.e. pair,time,open,low,high,close
  2. How to make it so that all this information is not constantly displayed on the screen, but only what I display with puts is displayed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Demidenko, 2018-01-23
@Dem1

1) Parse the data and get it from the hash

require 'json'

parsed_data = JSON.parse(event.data)
parsed_data['pair']
parsed_data['open']

2) The data is constantly displayed, because you display them yourself here:
p [:message, event.data]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question