# File lib/em-websocket/connection.rb, line 130
      def send(data)
        # If we're using Ruby 1.9, be pedantic about encodings
        if data.respond_to?(:force_encoding)
          # Also accept ascii only data in other encodings for convenience
          unless (data.encoding == Encoding.find("UTF-8") && data.valid_encoding?) || data.ascii_only?
            raise WebSocketError, "Data sent to WebSocket must be valid UTF-8 but was #{data.encoding} (valid: #{data.valid_encoding?})"
          end
          # This labels the encoding as binary so that it can be combined with
          # the BINARY framing
          data.force_encoding("BINARY")
        else
          # TODO: Check that data is valid UTF-8
        end

        if @handler
          @handler.send_text_frame(data)
        else
          raise WebSocketError, "Cannot send data before onopen callback"
        end
      end