Class: Jimmu::WebSocketConnection
- Inherits:
-
Object
- Object
- Jimmu::WebSocketConnection
- Defined in:
- lib/jimmu.rb
Overview
Passed as self into a websocket "/path" do ... end block (via
instance_exec), and as self into the on_message / on_close
handler blocks registered from inside it. This is what lets those
blocks call bare send(...), close, params, session, etc.
websocket "/room/[name]" do
log "opened: #{params[:name]}"
{ |msg| send("echo: #{msg}") }
on_close { log "closed" }
end
Instance Attribute Summary collapse
-
#cookie ⇒ Object
readonly
Returns the value of attribute cookie.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
- #close(code = 1000, reason = '') ⇒ Object
- #db ⇒ Object
-
#initialize(application, socket, request, route_params, cookie_jar, session_hash) ⇒ WebSocketConnection
constructor
A new instance of WebSocketConnection.
- #log(message) ⇒ Object
- #on_close(&block) ⇒ Object
- #on_message(&block) ⇒ Object
- #open? ⇒ Boolean
-
#run_loop ⇒ Object
Runs the blocking read loop for this connection's lifetime.
-
#send(message) ⇒ Object
Sends a text frame to this client.
-
#send_binary(bytes) ⇒ Object
Sends a binary frame to this client.
Constructor Details
#initialize(application, socket, request, route_params, cookie_jar, session_hash) ⇒ WebSocketConnection
Returns a new instance of WebSocketConnection.
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 |
# File 'lib/jimmu.rb', line 1119 def initialize(application, socket, request, route_params, , session_hash) @application = application @socket = socket @request = request @params = route_params @cookie = @session = session_hash @message_handler = nil @close_handler = nil @open = true end |
Instance Attribute Details
#cookie ⇒ Object (readonly)
Returns the value of attribute cookie.
1117 1118 1119 |
# File 'lib/jimmu.rb', line 1117 def @cookie end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
1117 1118 1119 |
# File 'lib/jimmu.rb', line 1117 def params @params end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
1117 1118 1119 |
# File 'lib/jimmu.rb', line 1117 def request @request end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
1117 1118 1119 |
# File 'lib/jimmu.rb', line 1117 def session @session end |
Instance Method Details
#close(code = 1000, reason = '') ⇒ Object
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 |
# File 'lib/jimmu.rb', line 1174 def close(code = 1000, reason = '') return unless @open payload = [code].pack('n') + reason.to_s begin WebSocketProtocol.write_frame(@socket, 0x8, payload) rescue IOError, Errno::EPIPE, Errno::ECONNRESET nil end @open = false begin @socket.close rescue IOError nil end nil end |
#db ⇒ Object
1150 1151 1152 |
# File 'lib/jimmu.rb', line 1150 def db @application.default_db end |
#log(message) ⇒ Object
1145 1146 1147 1148 |
# File 'lib/jimmu.rb', line 1145 def log() @application.log() nil end |
#on_close(&block) ⇒ Object
1140 1141 1142 1143 |
# File 'lib/jimmu.rb', line 1140 def on_close(&block) @close_handler = block nil end |
#on_message(&block) ⇒ Object
1135 1136 1137 1138 |
# File 'lib/jimmu.rb', line 1135 def (&block) @message_handler = block nil end |
#open? ⇒ Boolean
1131 1132 1133 |
# File 'lib/jimmu.rb', line 1131 def open? @open end |
#run_loop ⇒ Object
Runs the blocking read loop for this connection's lifetime.
Called by Application after the block passed to websocket has
been instance_exec'd (so on_message/on_close are registered).
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 |
# File 'lib/jimmu.rb', line 1194 def run_loop loop do break unless @open = WebSocketProtocol.(@socket) break if .nil? case [:opcode] when 0x1, 0x2 safely { @message_handler&.call([:payload]) } when 0x8 close(1000, '') break when 0x9 WebSocketProtocol.write_frame(@socket, 0xA, [:payload]) rescue nil when 0xA nil # pong received, nothing to do end end rescue IOError, Errno::ECONNRESET, Errno::EPIPE, Errno::ETIMEDOUT nil ensure @open = false safely { @close_handler&.call } begin @socket.close rescue IOError nil end end |
#send(message) ⇒ Object
Sends a text frame to this client.
1155 1156 1157 1158 1159 1160 1161 1162 |
# File 'lib/jimmu.rb', line 1155 def send() return false unless @open WebSocketProtocol.write_frame(@socket, 0x1, .to_s) true rescue IOError, Errno::EPIPE, Errno::ECONNRESET @open = false false end |
#send_binary(bytes) ⇒ Object
Sends a binary frame to this client.
1165 1166 1167 1168 1169 1170 1171 1172 |
# File 'lib/jimmu.rb', line 1165 def send_binary(bytes) return false unless @open WebSocketProtocol.write_frame(@socket, 0x2, bytes) true rescue IOError, Errno::EPIPE, Errno::ECONNRESET @open = false false end |