Class: RubyAPI::WebSocket
- Inherits:
-
Object
- Object
- RubyAPI::WebSocket
- Defined in:
- lib/fastrb/websocket.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #close(code = 1000, reason = "") ⇒ Object
- #closed? ⇒ Boolean
- #hijack? ⇒ Boolean
-
#initialize(env, &handler) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #on_close(&block) ⇒ Object
- #on_message(&block) ⇒ Object
- #on_open(&block) ⇒ Object
- #process! ⇒ Object
- #receive ⇒ Object
- #send(data) ⇒ Object
Constructor Details
#initialize(env, &handler) ⇒ WebSocket
Returns a new instance of WebSocket.
5 6 7 8 9 10 11 12 13 |
# File 'lib/fastrb/websocket.rb', line 5 def initialize(env, &handler) @env = env @handler = handler @messages = Queue.new @closed = false @on_message = nil @on_close = nil @on_open = nil end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
3 4 5 |
# File 'lib/fastrb/websocket.rb', line 3 def env @env end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/fastrb/websocket.rb', line 3 def path @path end |
Class Method Details
.upgrade(env) ⇒ Object
58 59 60 61 62 |
# File 'lib/fastrb/websocket.rb', line 58 def self.upgrade(env) io = env["rack.hijack"]&.call return nil unless io io end |
Instance Method Details
#close(code = 1000, reason = "") ⇒ Object
31 32 33 34 |
# File 'lib/fastrb/websocket.rb', line 31 def close(code = 1000, reason = "") @messages << { type: :close, code: code, reason: reason } @closed = true end |
#on_close(&block) ⇒ Object
23 24 25 |
# File 'lib/fastrb/websocket.rb', line 23 def on_close(&block) @on_close = block end |
#on_message(&block) ⇒ Object
19 20 21 |
# File 'lib/fastrb/websocket.rb', line 19 def (&block) @on_message = block end |
#on_open(&block) ⇒ Object
15 16 17 |
# File 'lib/fastrb/websocket.rb', line 15 def on_open(&block) @on_open = block end |
#process! ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/fastrb/websocket.rb', line 49 def process! @on_open&.call(self) @handler.call(self) if @handler rescue => e @on_close&.call(e) if @on_close ensure @on_close&.call(nil) unless @closed end |
#receive ⇒ Object
40 41 42 43 |
# File 'lib/fastrb/websocket.rb', line 40 def receive msg = @messages.pop msg end |
#send(data) ⇒ Object
27 28 29 |
# File 'lib/fastrb/websocket.rb', line 27 def send(data) @messages << { type: :send, data: data } end |