Class: Peatio::Ranger::Connection
- Inherits:
-
Object
- Object
- Peatio::Ranger::Connection
- Defined in:
- lib/peatio/ranger/connection.rb
Instance Attribute Summary collapse
-
#authorized ⇒ Object
readonly
Returns the value of attribute authorized.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#streams ⇒ Object
readonly
Returns the value of attribute streams.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #authenticate(authenticator, jwt) ⇒ Object
- #handle(msg) ⇒ Object
- #handshake(authenticator, hs) ⇒ Object
-
#initialize(router, socket, logger) ⇒ Connection
constructor
A new instance of Connection.
- #inspect ⇒ Object
- #send(method, data) ⇒ Object
- #send_raw(payload) ⇒ Object
- #subscribe(subscribed_streams) ⇒ Object
- #unsubscribe(unsubscribed_streams) ⇒ Object
Constructor Details
#initialize(router, socket, logger) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 11 12 13 14 15 |
# File 'lib/peatio/ranger/connection.rb', line 7 def initialize(router, socket, logger) @id = SecureRandom.hex(10) @router = router @socket = socket @logger = logger @user = nil @authorized = false @streams = {} end |
Instance Attribute Details
#authorized ⇒ Object (readonly)
Returns the value of attribute authorized.
5 6 7 |
# File 'lib/peatio/ranger/connection.rb', line 5 def @authorized end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/peatio/ranger/connection.rb', line 5 def id @id end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
5 6 7 |
# File 'lib/peatio/ranger/connection.rb', line 5 def logger @logger end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
5 6 7 |
# File 'lib/peatio/ranger/connection.rb', line 5 def socket @socket end |
#streams ⇒ Object (readonly)
Returns the value of attribute streams.
5 6 7 |
# File 'lib/peatio/ranger/connection.rb', line 5 def streams @streams end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
5 6 7 |
# File 'lib/peatio/ranger/connection.rb', line 5 def user @user end |
Instance Method Details
#authenticate(authenticator, jwt) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/peatio/ranger/connection.rb', line 35 def authenticate(authenticator, jwt) payload = {} = false begin payload = authenticator.authenticate!(jwt) = true rescue Peatio::Auth::Error => e logger.warn e. end [, payload] end |
#handle(msg) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/peatio/ranger/connection.rb', line 77 def handle(msg) return if msg.to_s.empty? data = JSON.parse(msg) case data["event"] when "subscribe" subscribe(data["streams"]) when "unsubscribe" unsubscribe(data["streams"]) end rescue JSON::ParserError => e logger.debug { "#{e}, msg: `#{msg}`" } end |
#handshake(authenticator, hs) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/peatio/ranger/connection.rb', line 91 def handshake(authenticator, hs) query = URI.decode_www_form(hs.query_string) subscribe(query.map {|item| item.last if item.first == "stream" }) logger.debug "WebSocket connection openned" headers = hs.headers_downcased return unless headers.key?("authorization") , payload = authenticate(authenticator, headers["authorization"]) if ! logger.debug "Authentication failed for UID:#{payload[:uid]}" raise EM::WebSocket::HandshakeError, "Authorization failed" else @user = payload[:uid] @authorized = true logger.debug "User #{@user} authenticated #{@streams}" end end |
#inspect ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/peatio/ranger/connection.rb', line 17 def inspect if "<Connection id=#{id} user=#{user}>" else "<Connection id=#{id}>" end end |
#send(method, data) ⇒ Object
30 31 32 33 |
# File 'lib/peatio/ranger/connection.rb', line 30 def send(method, data) payload = JSON.dump(method => data) send_raw(payload) end |
#send_raw(payload) ⇒ Object
25 26 27 28 |
# File 'lib/peatio/ranger/connection.rb', line 25 def send_raw(payload) logger.debug { "sending to user #{user.inspect} payload: #{payload}" } @socket.send(payload) end |
#subscribe(subscribed_streams) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/peatio/ranger/connection.rb', line 47 def subscribe(subscribed_streams) raise "Streams must be an array of strings" unless subscribed_streams.is_a?(Array) subscribed_streams.each do |stream| stream = stream.to_s next if stream.empty? unless @streams[stream] @streams[stream] = true @router.on_subscribe(self, stream) end end send(:success, message: "subscribed", streams: streams.keys) end |
#unsubscribe(unsubscribed_streams) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/peatio/ranger/connection.rb', line 62 def unsubscribe(unsubscribed_streams) raise "Streams must be an array of strings" unless unsubscribed_streams.is_a?(Array) unsubscribed_streams.each do |stream| stream = stream.to_s next if stream.empty? if @streams[stream] @streams.delete(stream) @router.on_unsubscribe(self, stream) end end send(:success, message: "unsubscribed", streams: streams.keys) end |