Class: Clacky::Server::HttpServer::WebSocketConnection
- Inherits:
-
Object
- Object
- Clacky::Server::HttpServer::WebSocketConnection
- Defined in:
- lib/clacky/server/http_server.rb
Overview
Wraps a raw TCP socket, providing thread-safe WebSocket frame sending.
Instance Attribute Summary collapse
-
#session_id ⇒ Object
Returns the value of attribute session_id.
Instance Method Summary collapse
-
#closed? ⇒ Boolean
Returns true if the underlying socket has been detected as dead.
-
#initialize(socket, version) ⇒ WebSocketConnection
constructor
A new instance of WebSocketConnection.
-
#send_json(data) ⇒ Object
Send a JSON-serializable object over the WebSocket.
-
#send_raw(type, data) ⇒ Object
Send a raw WebSocket frame.
Constructor Details
#initialize(socket, version) ⇒ WebSocketConnection
Returns a new instance of WebSocketConnection.
2339 2340 2341 2342 2343 2344 |
# File 'lib/clacky/server/http_server.rb', line 2339 def initialize(socket, version) @socket = socket @version = version @send_mutex = Mutex.new @closed = false end |
Instance Attribute Details
#session_id ⇒ Object
Returns the value of attribute session_id.
2337 2338 2339 |
# File 'lib/clacky/server/http_server.rb', line 2337 def session_id @session_id end |
Instance Method Details
#closed? ⇒ Boolean
Returns true if the underlying socket has been detected as dead.
2347 2348 2349 |
# File 'lib/clacky/server/http_server.rb', line 2347 def closed? @closed end |
#send_json(data) ⇒ Object
Send a JSON-serializable object over the WebSocket. Returns true on success, false if the connection is dead.
2353 2354 2355 2356 2357 2358 |
# File 'lib/clacky/server/http_server.rb', line 2353 def send_json(data) send_raw(:text, JSON.generate(data)) rescue => e Clacky::Logger.debug("WS send error (connection dead): #{e.}") false end |
#send_raw(type, data) ⇒ Object
Send a raw WebSocket frame. Returns true on success, false on broken/closed socket.
2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 |
# File 'lib/clacky/server/http_server.rb', line 2362 def send_raw(type, data) @send_mutex.synchronize do return false if @closed outgoing = WebSocket::Frame::Outgoing::Server.new( version: @version, data: data, type: type ) @socket.write(outgoing.to_s) end true rescue Errno::EPIPE, Errno::ECONNRESET, IOError, Errno::EBADF => e @closed = true Clacky::Logger.debug("WS send_raw error (client disconnected): #{e.}") false rescue => e @closed = true Clacky::Logger.debug("WS send_raw unexpected error: #{e.}") false end |