Class: AnyCable::Socket
- Inherits:
-
Object
- Object
- AnyCable::Socket
- Defined in:
- lib/anycable/socket.rb
Overview
Socket mock to be used with application connection
Defined Under Namespace
Classes: State
Instance Attribute Summary collapse
-
#presence ⇒ Object
readonly
Returns the value of attribute presence.
-
#transmissions ⇒ Object
readonly
Returns the value of attribute transmissions.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #cstate ⇒ Object
- #env ⇒ Object
-
#initialize(env:) ⇒ Socket
constructor
A new instance of Socket.
- #istate ⇒ Object
- #presence_join(broadcasting, id, info) ⇒ Object
- #presence_leave(id) ⇒ Object
- #session ⇒ Object
- #session=(val) ⇒ Object
- #stop_streams? ⇒ Boolean
- #streams ⇒ Object
- #subscribe(_channel, broadcasting) ⇒ Object
- #transmit(websocket_message) ⇒ Object
- #unsubscribe(_channel, broadcasting) ⇒ Object
- #unsubscribe_from_all(_channel) ⇒ Object
- #whisper(_channel, broadcasting) ⇒ Object
Constructor Details
#initialize(env:) ⇒ Socket
Returns a new instance of Socket.
52 53 54 55 |
# File 'lib/anycable/socket.rb', line 52 def initialize(env:) @transmissions = [] @request_env = env end |
Instance Attribute Details
#presence ⇒ Object (readonly)
Returns the value of attribute presence.
50 51 52 |
# File 'lib/anycable/socket.rb', line 50 def presence @presence end |
#transmissions ⇒ Object (readonly)
Returns the value of attribute transmissions.
50 51 52 |
# File 'lib/anycable/socket.rb', line 50 def transmissions @transmissions end |
Instance Method Details
#close ⇒ Object
103 104 105 106 107 |
# File 'lib/anycable/socket.rb', line 103 def close @closed = true @streams&.clear @stop_all_streams = true end |
#closed? ⇒ Boolean
109 110 111 |
# File 'lib/anycable/socket.rb', line 109 def closed? @closed == true end |
#cstate ⇒ Object
137 138 139 140 141 |
# File 'lib/anycable/socket.rb', line 137 def cstate return @cstate if defined?(@cstate) @cstate = env["anycable.cstate"] = State.new(env["anycable.raw_cstate"]) end |
#env ⇒ Object
125 126 127 128 129 |
# File 'lib/anycable/socket.rb', line 125 def env return @env if defined?(@env) @env = build_rack_env end |
#istate ⇒ Object
131 132 133 134 135 |
# File 'lib/anycable/socket.rb', line 131 def istate return @istate if defined?(@istate) @istate = env["anycable.istate"] = State.new(env["anycable.raw_istate"]) end |
#presence_join(broadcasting, id, info) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/anycable/socket.rb', line 77 def presence_join(broadcasting, id, info) raise ArgumentError, "ID is required for presence tracking" unless id subscribe("", broadcasting) istate.write(PRESENCE_KEY, broadcasting) @presence = {type: "join", id: id, info: info}.compact end |
#presence_leave(id) ⇒ Object
86 87 88 89 90 |
# File 'lib/anycable/socket.rb', line 86 def presence_leave(id) raise ArgumentError, "ID is required for presence tracking" unless id @presence = {type: "leave", id: id} end |
#session ⇒ Object
117 118 119 |
# File 'lib/anycable/socket.rb', line 117 def session cstate.read(SESSION_KEY) end |
#session=(val) ⇒ Object
121 122 123 |
# File 'lib/anycable/socket.rb', line 121 def session=(val) cstate.write(SESSION_KEY, val) end |
#stop_streams? ⇒ Boolean
113 114 115 |
# File 'lib/anycable/socket.rb', line 113 def stop_streams? @stop_all_streams == true end |
#streams ⇒ Object
99 100 101 |
# File 'lib/anycable/socket.rb', line 99 def streams @streams ||= {start: [], stop: []} end |
#subscribe(_channel, broadcasting) ⇒ Object
61 62 63 |
# File 'lib/anycable/socket.rb', line 61 def subscribe(_channel, broadcasting) streams[:start] << broadcasting unless streams[:start].include?(broadcasting) end |
#transmit(websocket_message) ⇒ Object
57 58 59 |
# File 'lib/anycable/socket.rb', line 57 def transmit() transmissions << end |
#unsubscribe(_channel, broadcasting) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/anycable/socket.rb', line 65 def unsubscribe(_channel, broadcasting) streams[:stop] << broadcasting unless streams[:stop].include?(broadcasting) if istate.read(WHISPER_KEY) == broadcasting istate.write(WHISPER_KEY, "") end end |
#unsubscribe_from_all(_channel) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/anycable/socket.rb', line 92 def unsubscribe_from_all(_channel) @stop_all_streams = true if istate.read(WHISPER_KEY) istate.write(WHISPER_KEY, "") end end |
#whisper(_channel, broadcasting) ⇒ Object
73 74 75 |
# File 'lib/anycable/socket.rb', line 73 def whisper(_channel, broadcasting) istate.write(WHISPER_KEY, broadcasting) end |