class WebSocket::Driver::Hybi

def emit_frame(buffer)

def emit_frame(buffer)
  frame    = @frame
  opcode   = frame.opcode
  payload  = frame.payload = Mask.mask(buffer, @frame.masking_key)
  bytesize = payload.bytesize
  bytes    = payload.bytes.to_a
  @frame = nil
  case opcode
    when OPCODES[:continuation] then
      return fail(:protocol_error, 'Received unexpected continuation frame') unless @message
      @message << frame
    when OPCODES[:text], OPCODES[:binary] then
      @message = Message.new
      @message << frame
    when OPCODES[:close] then
      code   = (bytesize >= 2) ? payload.unpack(PACK_FORMATS[2]).first : nil
      reason = (bytesize > 2)  ? Driver.encode(bytes[2..-1] || [], UNICODE) : nil
      unless (bytesize == 0) or
             (code && code >= MIN_RESERVED_ERROR && code <= MAX_RESERVED_ERROR) or
             ERROR_CODES.include?(code)
        code = ERRORS[:protocol_error]
      end
      if bytesize > 125 or (bytesize > 2 and reason.nil?)
        code = ERRORS[:protocol_error]
      end
      shutdown(code || DEFAULT_ERROR_CODE, reason || '')
    when OPCODES[:ping] then
      frame(payload, :pong)
    when OPCODES[:pong] then
      message = Driver.encode(payload, UNICODE)
      callback = @ping_callbacks[message]
      @ping_callbacks.delete(message)
      callback.call if callback
  end
  emit_message if frame.final and MESSAGE_OPCODES.include?(opcode)
end