From 321d04ff56e2f17ef7285141252dba8ff5cdecca Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 27 Jun 2015 16:50:05 +0200 Subject: Add WebSocket decorator --- lib/action_cable/connection/web_socket.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 lib/action_cable/connection/web_socket.rb (limited to 'lib/action_cable/connection/web_socket.rb') diff --git a/lib/action_cable/connection/web_socket.rb b/lib/action_cable/connection/web_socket.rb new file mode 100644 index 0000000000..135a28cfe4 --- /dev/null +++ b/lib/action_cable/connection/web_socket.rb @@ -0,0 +1,27 @@ +module ActionCable + module Connection + # Decorate the Faye::WebSocket with helpers we need. + class WebSocket + delegate :rack_response, :close, :on, to: :websocket + + def initialize(env) + @websocket = Faye::WebSocket.websocket?(env) ? Faye::WebSocket.new(env) : nil + end + + def possible? + websocket + end + + def alive? + websocket && websocket.ready_state == Faye::WebSocket::API::OPEN + end + + def transmit(data) + websocket.send data + end + + private + attr_reader :websocket + end + end +end -- cgit v1.2.3