Class: CitrusRpc::RpcClient::WsMailBox
- Inherits:
-
Object
- Object
- CitrusRpc::RpcClient::WsMailBox
- Includes:
- Utils::EventEmitter
- Defined in:
- lib/citrus-rpc/rpc-client/mailboxes/ws_mailbox.rb
Overview
WsMailBox
Instance Method Summary collapse
-
#close ⇒ Object
Close the mail box.
-
#connect ⇒ Object
Connect to remote server.
-
#initialize(server, args = {}) ⇒ WsMailBox
constructor
Create a new websocket mailbox.
-
#send(msg, opts, block) ⇒ Object
Send message to remote server.
Methods included from Utils::EventEmitter
Constructor Details
#initialize(server, args = {}) ⇒ WsMailBox
Create a new websocket mailbox
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/citrus-rpc/rpc-client/mailboxes/ws_mailbox.rb', line 28 def initialize server, args={} @cur_id = 0 @id = server[:id] @host = server[:host] @port = server[:port] @requests = {} @timeout = {} @queue = [] @buffer_msg = args[:buffer_msg] @interval = args[:interval] || Constants::DefaultParams::Interval @timeout_value = args[:timeout] || Constants::DefaultParams::Timeout @connected = false @closed = false @args = args end |
Instance Method Details
#close ⇒ Object
Close the mail box
79 80 81 82 83 |
# File 'lib/citrus-rpc/rpc-client/mailboxes/ws_mailbox.rb', line 79 def close return if @closed @closed = true @ws.close end |
#connect ⇒ Object
Connect to remote server
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/citrus-rpc/rpc-client/mailboxes/ws_mailbox.rb', line 50 def connect if @connected block_given? and yield Exception.new 'mailbox has already connected' return end begin @ws = WebSocket::EventMachine::Client.connect :uri => 'ws://' + @host + ':' + @port.to_s @ws.onopen { return if @connected @connected = true @timer = EM.add_periodic_timer(@interval) { flush } if @buffer_msg block_given? and yield } @ws. { |msg, type| process_msg msg, type } @ws.onerror { |err| } @ws.onclose { |code, reason| emit :close, @id } rescue => err block_given? and yield err end end |
#send(msg, opts, block) ⇒ Object
Send message to remote server
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/citrus-rpc/rpc-client/mailboxes/ws_mailbox.rb', line 90 def send msg, opts, block unless @connected block.call Exception.new 'websocket mailbox has not connected' return end if @closed block.call Exception.new 'websocket mailbox has already closed' return end id = @cur_id @cur_id += 1 @requests[id] = block pkg = { :id => id, :msg => msg } if @buffer_msg enqueue pkg else @ws.send pkg.to_json end end |