Class: RubstApi::WebSocket
- Inherits:
-
Object
- Object
- RubstApi::WebSocket
- Defined in:
- lib/rubst_api/websocket.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#path_params ⇒ Object
readonly
Returns the value of attribute path_params.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #accept(subprotocol: nil, headers: {}) ⇒ Object
- #close(code: 1000, reason: nil) ⇒ Object
- #cookies ⇒ Object
- #headers ⇒ Object
-
#initialize(env, path_params: {}) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #query_params ⇒ Object
- #receive ⇒ Object
- #receive_json ⇒ Object
- #receive_text ⇒ Object
- #send_json(data) ⇒ Object
- #send_text(data) ⇒ Object
Constructor Details
#initialize(env, path_params: {}) ⇒ WebSocket
Returns a new instance of WebSocket.
6 7 8 9 |
# File 'lib/rubst_api/websocket.rb', line 6 def initialize(env, path_params: {}) @env, @path_params, @state = env, path_params, {} @socket = env["rack.websocket"] end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
5 6 7 |
# File 'lib/rubst_api/websocket.rb', line 5 def env @env end |
#path_params ⇒ Object (readonly)
Returns the value of attribute path_params.
5 6 7 |
# File 'lib/rubst_api/websocket.rb', line 5 def path_params @path_params end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
5 6 7 |
# File 'lib/rubst_api/websocket.rb', line 5 def state @state end |
Instance Method Details
#accept(subprotocol: nil, headers: {}) ⇒ Object
10 |
# File 'lib/rubst_api/websocket.rb', line 10 def accept(subprotocol: nil, headers: {}) = @socket&.accept(subprotocol:, headers:) |
#close(code: 1000, reason: nil) ⇒ Object
16 |
# File 'lib/rubst_api/websocket.rb', line 16 def close(code: 1000, reason: nil) = @socket&.close(code, reason) |
#headers ⇒ Object
18 |
# File 'lib/rubst_api/websocket.rb', line 18 def headers = Request.new(env).headers |
#query_params ⇒ Object
17 |
# File 'lib/rubst_api/websocket.rb', line 17 def query_params = Request.new(env).query_params |
#receive ⇒ Object
11 |
# File 'lib/rubst_api/websocket.rb', line 11 def receive = @socket&.receive |
#receive_json ⇒ Object
13 |
# File 'lib/rubst_api/websocket.rb', line 13 def receive_json = JSON.parse(receive_text) |
#receive_text ⇒ Object
12 |
# File 'lib/rubst_api/websocket.rb', line 12 def receive_text = receive.to_s |
#send_json(data) ⇒ Object
15 |
# File 'lib/rubst_api/websocket.rb', line 15 def send_json(data) = send_text(JSON.generate(Serializer.dump(data))) |
#send_text(data) ⇒ Object
14 |
# File 'lib/rubst_api/websocket.rb', line 14 def send_text(data) = @socket&.send(data.to_s) |