Class: DRb::WebSocket::SocketPool
- Inherits:
-
Object
- Object
- DRb::WebSocket::SocketPool
- Defined in:
- lib/opal/drb/websocket.rb
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
-
#ws ⇒ Object
readonly
Returns the value of attribute ws.
Class Method Summary collapse
Instance Method Summary collapse
- #[](uri) ⇒ Object
-
#initialize(uri, ws) ⇒ SocketPool
constructor
A new instance of SocketPool.
- #send(data, &block) ⇒ Object
Constructor Details
#initialize(uri, ws) ⇒ SocketPool
Returns a new instance of SocketPool.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/opal/drb/websocket.rb', line 90 def initialize(uri, ws) @uri = uri @ws = ws @handlers = {} ws. do |event| = event.data.to_s sender_id = .slice(0, 36) = .slice(36, .length - 36) @handlers.delete(sender_id).call() end end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
88 89 90 |
# File 'lib/opal/drb/websocket.rb', line 88 def uri @uri end |
#ws ⇒ Object (readonly)
Returns the value of attribute ws.
88 89 90 |
# File 'lib/opal/drb/websocket.rb', line 88 def ws @ws end |
Class Method Details
.new_connection(uri) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/opal/drb/websocket.rb', line 108 def self.new_connection(uri) ws = ::WebSocket.new(uri) ws.onclose do @sockets[uri] = new_connection(uri) end self.new(uri, ws) end |
.open(uri) ⇒ Object
103 104 105 106 |
# File 'lib/opal/drb/websocket.rb', line 103 def self.open(uri) @sockets ||= {} @sockets[uri] ||= new_connection(uri) end |
Instance Method Details
#[](uri) ⇒ Object
133 134 135 |
# File 'lib/opal/drb/websocket.rb', line 133 def [](uri) @sockets[uri].ws end |
#send(data, &block) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/opal/drb/websocket.rb', line 118 def send(data, &block) sender_id = SecureRandom.uuid @handlers[sender_id] = block byte_data = sender_id.bytes.each_slice(2).map(&:first) byte_data += data.bytes.each_slice(2).map(&:first) if @ws.connecting? @ws.onopen do @ws.send(`new Uint8Array(#{byte_data}).buffer`) end else @ws.send(`new Uint8Array(#{byte_data}).buffer`) end end |