Answer the question
In order to leave comments, you need to log in
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
}
[:message, "{\"pair\":\"Bitcoin\",\"time\":1516567298,\"open\":11146.938,\"low\":11146.938,\"high\":11146.938,\"close\":11146.938}"]
[:message, "{\"servertime\":1516567298}"]
Answer the question
In order to leave comments, you need to log in
1) Parse the data and get it from the hash
require 'json'
parsed_data = JSON.parse(event.data)
parsed_data['pair']
parsed_data['open']
p [:message, event.data]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question