Class: NemWebsocketClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nem_websocket_client/client.rb

Constant Summary collapse

@@block_path =
"/blocks"
@@block_height_path =
"/blocks/new"
@@owned_namespace_path =
"/account/namespace/owned/"
@@owned_mosaic_path =
"/account/mosaic/owned/"
@@owned_mosaic_definition_path =
"/account/mosaic/owned/definition/"
@@transaction_path =
"/transactions/"
@@unconfirmed_transaction_path =
"/unconfirmed/"
@@recenttransactions_path =
"/recenttransactions/"
@@account_path =
"/account/"
@@send_owned_namespace_path =
"/w/api/account/namespace/owned"
@@send_owned_mosaic_path =
"/w/api/account/mosaic/owned"
@@send_owned_mosaic_definition_path =
"/w/api/account/mosaic/owned/definition"
@@send_recenttransactions_path =
"/w/api/account/transfers/all"
@@send_account_path =
"/w/api/account/get"

Instance Method Summary collapse

Constructor Details

#initialize(url, port) ⇒ Client

Returns a new instance of Client.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nem_websocket_client/client.rb', line 26

def initialize(url,port)
  host = url
  port = port
  path = "/w/messages/"
  server = format("%0#{3}d", SecureRandom.random_number(10**3))
  strings = [('a'..'z'), ('0'..'5')].map { |i| i.to_a }.flatten
  session = (0...8).map { strings[rand(strings.length)] }.join
  @endpoint = host + ":" + port.to_s + path + server.to_s + "/" +session + "/websocket/"
  @is_connected = false
  @sub_id_count = 0
end

Instance Method Details

#closeObject



119
120
121
# File 'lib/nem_websocket_client/client.rb', line 119

def close
  @ws.close
end

#closed(&fun) ⇒ Object



50
51
52
# File 'lib/nem_websocket_client/client.rb', line 50

def closed(&fun)
  @ws.closed_func = fun
end

#connectObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/nem_websocket_client/client.rb', line 123

def connect
    @ws = WebSocket::Client::Simple.connect(@endpoint)
    ws_init
    @ws.on :error do |e|
      errors_func.call(e)
    end
    
    @ws.on :close do |e|
      closed_func.call(e)
    end

    @ws.on :open do
      connect_header = {"accept-version":"1.1,1.0","heart-beat":"10000,10000"}
      stomp_connect = '["' + StompParser::Frame.new("CONNECT", connect_header,"").to_str + '"]'
      send(stomp_connect)
    end

    @ws.on :message do |msg|
      if msg.data[0] == "o"

      end

      if msg.data[0] == "h"
        heartbeat_func.call unless heartbeat_func.nil?
      end
      if msg.data[0] == "a"
        data = msg.data
        data.slice!(0,data.index("[")+2)
        data.slice!(data.rindex("]")-1,data.length)
        data.gsub!("\\n","\n")
        data.gsub!("\\r","\r")
        data.gsub!("\\/","\/")
        data.gsub!("\\u0000","\u0000")
        data.gsub!("\\\"","\"")
        parser = StompParser::Parser.new(1024 * 100)
        parser.parse(data) do |frame|
        if frame.command == "CONNECTED" 
          connected_func.call unless connected_func.nil?
          @is_connected = true
          unless subscribe_stomps.empty?
            subscribe_stomps.each do |stomp|
              send(stomp)
            end
          end
          unless send_stomps.empty?
            send_stomps.each do |stomp|
              send(stomp)
            end
          end
        end
        if frame.command == "MESSAGE"
          result = JSON.parse(frame.body)
          destination = frame.headers["destination"]
          address = destination.match(/\w{40}/)
          destination.slice!(address.to_s) unless address.nil?
          case destination
          when @@block_path
            subscribe_block_func.call(result)
          when @@block_height_path
            subscribe_block_height_func.call(result) 
          when @@unconfirmed_transaction_path
            subscribe_unconfirmed_transaction_func.call(result,address) 
          when @@recenttransactions_path
            subscribe_recenttransactions_func.call(result,address) 
          when @@account_path
            .call(result,address) 
          when @@owned_namespace_path
            subscribe_owned_namespace_func.call(result,address) 
          when @@owned_mosaic_path
            subscribe_owned_mosaic_func.call(result,address) 
          when @@owned_mosaic_definition_path
            subscribe_owned_mosaic_definition_func.call(result,address) 
          when @@transaction_path
            subscribe_owned_mosaic_func.call(result,address) 
          end
        end
      end
    end
  end
end

#connected(&fun) ⇒ Object



38
39
40
# File 'lib/nem_websocket_client/client.rb', line 38

def connected(&fun)
  @ws.connected_func = fun
end

#errors(&fun) ⇒ Object



46
47
48
# File 'lib/nem_websocket_client/client.rb', line 46

def errors(&fun)
  @ws.errors_func = fun
end

#heartbeat(&fun) ⇒ Object



42
43
44
# File 'lib/nem_websocket_client/client.rb', line 42

def heartbeat(&fun)
  @ws.heartbeat_func = fun
end

#request_account(address) ⇒ Object



99
100
101
# File 'lib/nem_websocket_client/client.rb', line 99

def (address)
  send_send_stomp(,address)
end

#request_owned_mosaic(address) ⇒ Object



107
108
109
# File 'lib/nem_websocket_client/client.rb', line 107

def request_owned_mosaic(address)
  send_send_stomp(@@send_owned_mosaic_path,address)
end

#request_owned_mosaic_definition(address) ⇒ Object



111
112
113
# File 'lib/nem_websocket_client/client.rb', line 111

def request_owned_mosaic_definition(address)
  send_send_stomp(@@send_owned_mosaic_definition_path,address)
end

#request_owned_namespace(address) ⇒ Object



103
104
105
# File 'lib/nem_websocket_client/client.rb', line 103

def request_owned_namespace(address)
  send_send_stomp(@@send_owned_namespace_path,address)
end

#request_recenttransactions(address) ⇒ Object



115
116
117
# File 'lib/nem_websocket_client/client.rb', line 115

def request_recenttransactions(address)
  send_send_stomp(@@send_recenttransactions_path,address)
end

#subscribe_account(address, &fun) ⇒ Object



94
95
96
97
# File 'lib/nem_websocket_client/client.rb', line 94

def (address,&fun)
  @ws. = fun
  send_subscribe_stomp(@@account_path + address)
end

#subscribe_block(&fun) ⇒ Object



54
55
56
57
# File 'lib/nem_websocket_client/client.rb', line 54

def subscribe_block(&fun)
  @ws.subscribe_block_func = fun
  send_subscribe_stomp(@@block_path)
end

#subscribe_block_height(&fun) ⇒ Object



59
60
61
62
# File 'lib/nem_websocket_client/client.rb', line 59

def subscribe_block_height(&fun)
  @ws.subscribe_block_height_func = fun
  send_subscribe_stomp(@@block_height_path)
end

#subscribe_owned_mosaic(address, &fun) ⇒ Object



69
70
71
72
# File 'lib/nem_websocket_client/client.rb', line 69

def subscribe_owned_mosaic(address,&fun)
  @ws.subscribe_owned_mosaic_func = fun
  send_subscribe_stomp(@@owned_mosaic_path + address)
end

#subscribe_owned_mosaic_definition(address, &fun) ⇒ Object



74
75
76
77
# File 'lib/nem_websocket_client/client.rb', line 74

def subscribe_owned_mosaic_definition(address,&fun)
  @ws.subscribe_owned_mosaic_definition_func = fun
  send_subscribe_stomp(@@owned_mosaic_definition_path + address)
end

#subscribe_owned_namespace(address, &fun) ⇒ Object



64
65
66
67
# File 'lib/nem_websocket_client/client.rb', line 64

def subscribe_owned_namespace(address,&fun)
  @ws.subscribe_owned_namespace_func = fun
  send_subscribe_stomp(@@owned_namespace_path + address)
end

#subscribe_recenttransactions(address, &fun) ⇒ Object



89
90
91
92
# File 'lib/nem_websocket_client/client.rb', line 89

def subscribe_recenttransactions(address,&fun)
  @ws.subscribe_recenttransactions_func = fun
  send_subscribe_stomp(@@recenttransactions_path + address)
end

#subscribe_transaction(address, &fun) ⇒ Object



79
80
81
82
# File 'lib/nem_websocket_client/client.rb', line 79

def subscribe_transaction(address,&fun)
  @ws.subscribe_transaction_func = fun
  send_subscribe_stomp(@@transaction_path + address)
end

#subscribe_unconfirmed_transaction(address, &fun) ⇒ Object



84
85
86
87
# File 'lib/nem_websocket_client/client.rb', line 84

def subscribe_unconfirmed_transaction(address,&fun)
  @ws.subscribe_unconfirmed_transaction_func = fun
  send_subscribe_stomp(@@unconfirmed_transaction_path + address)
end