Class: RubstApi::WebSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/rubst_api/websocket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/rubst_api/websocket.rb', line 5

def env
  @env
end

#path_paramsObject (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

#stateObject (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)

#cookiesObject



19
# File 'lib/rubst_api/websocket.rb', line 19

def cookies = Request.new(env).cookies

#headersObject



18
# File 'lib/rubst_api/websocket.rb', line 18

def headers = Request.new(env).headers

#query_paramsObject



17
# File 'lib/rubst_api/websocket.rb', line 17

def query_params = Request.new(env).query_params

#receiveObject



11
# File 'lib/rubst_api/websocket.rb', line 11

def receive = @socket&.receive

#receive_jsonObject



13
# File 'lib/rubst_api/websocket.rb', line 13

def receive_json = JSON.parse(receive_text)

#receive_textObject



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)