def send_frame opcode, payload="", masked=false
      payload = payload.dup.force_encoding("ASCII-8BIT") if payload.respond_to? :force_encoding
      payload_length = payload.bytesize

      # We don't support continuations (yet), so always send fin
      fin_byte = 0x80
      send_data [fin_byte | opcode].pack("C")

      # We shouldn't be sending mask, we're a server only
      masked_byte = masked ? 0x80 : 0x00

      if payload_length <= 125
        send_data [masked_byte | payload_length].pack("C")

      elsif payload_length < 2 ** 16
        send_data [masked_byte | 126].pack("C")
        send_data [payload_length].pack("n")

      else
        send_data [masked_byte | 127].pack("C")
        send_data [payload_length / (2 ** 32), payload_length % (2 ** 32)].pack("NN")
      end

      if payload_length
        if masked
          mask_key = Array.new(4) { rand(256) }.pack("C*")
          send_data mask_key
          payload = mask payload, mask_key
        end

        send_data payload
      end
    end
def send_frame opcode, payload="", masked=false
      payload = payload.dup.force_encoding("ASCII-8BIT") if payload.respond_to? :force_encoding
      payload_length = payload.bytesize

      # We don't support continuations (yet), so always send fin
      fin_byte = 0x80
      send_data [fin_byte | opcode].pack("C")

      # We shouldn't be sending mask, we're a server only
      masked_byte = masked ? 0x80 : 0x00

      if payload_length <= 125
        send_data [masked_byte | payload_length].pack("C")

      elsif payload_length < 2 ** 16
        send_data [masked_byte | 126].pack("C")
        send_data [payload_length].pack("n")

      else
        send_data [masked_byte | 127].pack("C")
        send_data [payload_length / (2 ** 32), payload_length % (2 ** 32)].pack("NN")
      end

      if payload_length
        if masked
          mask_key = Array.new(4) { rand(256) }.pack("C*")
          send_data mask_key
          payload = mask payload, mask_key
        end

        send_data payload
      end
    end
This is for post-hixie-76 versions only