Class: WebRepl::Messenger
- Inherits:
-
Object
- Object
- WebRepl::Messenger
- Defined in:
- lib/web-repl/messenger.rb
Overview
Handles sending and receiving messages to/from the socket
Instance Method Summary collapse
-
#in(raw_message) {|hash| ... } ⇒ Hash
Handle an inputted message.
-
#initialize(socket, options = {}) ⇒ Messenger
constructor
A new instance of Messenger.
-
#new_timestamp ⇒ Fixnum
Generate a new timestamp in js format.
-
#out(message) ⇒ String?
Send a message over the socket.
Constructor Details
#initialize(socket, options = {}) ⇒ Messenger
Returns a new instance of Messenger.
9 10 11 12 |
# File 'lib/web-repl/messenger.rb', line 9 def initialize(socket, = {}) @socket = socket @debug = [:debug] end |
Instance Method Details
#in(raw_message) {|hash| ... } ⇒ Hash
Handle an inputted message
17 18 19 20 21 22 |
# File 'lib/web-repl/messenger.rb', line 17 def in(, &block) hash = JSON.parse(, :symbolize_names => true) hash[:timestamp] = Time.at(hash[:timestamp].to_i / 1000) if !hash[:timestamp].nil? yield(hash) if block_given? hash end |
#new_timestamp ⇒ Fixnum
Generate a new timestamp in js format
26 27 28 |
# File 'lib/web-repl/messenger.rb', line 26 def Time.now.to_i * 1000 # javascript time int format end |
#out(message) ⇒ String?
Send a message over the socket
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/web-repl/messenger.rb', line 33 def out() if !@socket.nil? [:timestamp] ||= json = .to_json @debug.puts "Sending message: #{json}" if @debug @socket.send(json) json else @debug.puts "Warning: No connection" if @debug nil end end |