aboutsummaryrefslogtreecommitdiffstats
path: root/actioncable/lib/action_cable/connection/web_socket.rb
blob: 169b683b8cb8c6b3f7c65c7e6bc475fbc05c7af5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
require 'faye/websocket'

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