Module: NineteenNinetynine::Core
- Included in:
- NineteenNinetynine
- Defined in:
- lib/nineteen_ninetynine/core.rb
Instance Method Summary collapse
- #_init ⇒ Object
- #create_last_data_received_at ⇒ Object
- #create_notes ⇒ Object
- #create_users ⇒ Object
- #create_ws_client ⇒ Object
- #error(e) ⇒ Object
- #last_data_received_at=(time) ⇒ Object
- #start ⇒ Object
- #start_stream ⇒ Object
- #stop_stream ⇒ Object
- #users ⇒ Object
Instance Method Details
#_init ⇒ Object
3 4 5 |
# File 'lib/nineteen_ninetynine/core.rb', line 3 def _init inits.each { |block| class_eval(&block) } end |
#create_last_data_received_at ⇒ Object
64 65 66 |
# File 'lib/nineteen_ninetynine/core.rb', line 64 def create_last_data_received_at @last_data_received_at ||= Time.now end |
#create_notes ⇒ Object
56 57 58 |
# File 'lib/nineteen_ninetynine/core.rb', line 56 def create_notes @notes ||= [] end |
#create_users ⇒ Object
60 61 62 |
# File 'lib/nineteen_ninetynine/core.rb', line 60 def create_users @users ||= [] end |
#create_ws_client ⇒ Object
52 53 54 |
# File 'lib/nineteen_ninetynine/core.rb', line 52 def create_ws_client @ws ||= WebSocket::Client::Simple.connect "wss://relay-jp.nostr.wirednet.jp" end |
#error(e) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/nineteen_ninetynine/core.rb', line 72 def error(e) case e when Exception insert "[ERROR] #{e.}\n #{e.backtrace.join("\n ")}".c(:notice) else insert "[ERROR] #{e}".c(:notice) end end |
#last_data_received_at=(time) ⇒ Object
68 69 70 |
# File 'lib/nineteen_ninetynine/core.rb', line 68 def last_data_received_at=(time) @last_data_received_at = time end |
#start ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/nineteen_ninetynine/core.rb', line 10 def start _init EM.run do Thread.start do while buf = Readline.readline("Nostr: ", true) unless Readline::HISTORY.count == 1 Readline::HISTORY.pop if buf.empty? || Readline::HISTORY[-1] == Readline::HISTORY[-2] end #sync { # reload unless config[:reload] == false # store_history # input(buf.strip) #} puts buf.strip end # unexpected #stop end EM.add_periodic_timer(1) do # Reconnect # if @last_data_received_at && Time.now - @last_data_received_at > 30 # # stop_stream # start_stream # end mutex = Mutex.new mutex.synchronize do output end end start_stream end end |
#start_stream ⇒ Object
81 82 83 84 85 86 87 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 135 |
# File 'lib/nineteen_ninetynine/core.rb', line 81 def start_stream users = create_users notes = create_notes create_last_data_received_at ws = create_ws_client ws.on :message do |msg| next if msg.data.empty? payload = JSON.parse(msg.data) case payload[0] when "EOSE" # puts "Start time line: #{Time.now}" when "EVENT" case payload[1] when "content" note = Event::Note.new(payload[2]) user = users.find { |u| u.pubkey == payload[2]["pubkey"] } if user.nil? ws.send JSON.generate(["REQ", "user", { kinds: [0], authors: [payload[2]["pubkey"]] }]) while user = users.find { |u| u.pubkey == payload[2]["pubkey"] } note.user = user sleep 0.1 end else note.user = user end notes.push note when "user" # user = User.new(JSON.parse(payload[2]["content"])["name"], payload[2]["pubkey"]) profile = Event::Profile.new(payload[2]) users.push profile end end rescue JSON::ParserError => e # ignore end ws.on :open do ws.send JSON.generate(["REQ", "content", { kinds: [1], since: Time.now.to_i }]) ws.send JSON.generate(["REQ", "user", { kinds: [0] }]) end ws.on :close do |e| end ws.on :error do |e| puts e puts e.backtrace end end |
#stop_stream ⇒ Object
47 48 49 50 |
# File 'lib/nineteen_ninetynine/core.rb', line 47 def stop_stream @ws.close if @ws.open? @ws = nil end |
#users ⇒ Object
6 7 8 |
# File 'lib/nineteen_ninetynine/core.rb', line 6 def users @users ||= [] end |